1

Consider we are generating a category bar with 2 levels.


This is an example about the XMLTemplate

template = XMLTemplate@"

   <ul>

   <wolfram:sequence values='#Root'>

 <li><wolfram:slot id=\"SequenceIndex\"/>: <wolfram:slot \

id=\"1\"/></li>

    </wolfram:sequence>

   <wolfram:sequence values='#Leaf'>

 <li><wolfram:slot id=\"SequenceIndex\"/>: <wolfram:slot \

id=\"1\"/></li>

    </wolfram:sequence>

   </ul>";

list = Thread[{a, b, c}~

   List~{{a1, a2, a3}, {b1, b2}, {c1, c2, c3, c4}}]

(*
    {{a,{a1,a2,a3}},{b,{b1,b2}},{c,{c1,c2,c3,c4}}}
*)

TemplateApply[template, 

    Association["Root" -> #[[1]], "Leaf" -> #[[2]]]] & /@ 

  list // StringJoin

(*
    <ul>   <li>1: a</li>    <li>1: a1</li>    <li>2: a2</li>    <li>3: a3</li> </ul><ul>   <li
>1: b</li>    <li>1: b1</li>    <li>2: b2</li> </ul><ul>   <li>1: c</li>    <li>1: c1</li>
    <li>2: c2</li>    <li>3: c3</li>    <li>4: c4</li> </ul>
*)

Here, actually, we used TemplateApply several times.

My question is:

is it possible to achieve the above effect and use TemplateApply only once?


The way below, I'm trying to achieve this by using TemplateApply twice, and If I can change the Root in the verbatim tag becomes "a","b" successfully.

str = "<wolfram:sequence values='#Root'>

<li>

 <wolfram:slot id=\"1\" /></li>

   <wolfram:verbatim>

   <seceond-level>

 <wolfram:slot id='Root' />

   </seceond-level>

  </wolfram:verbatim>

  </wolfram:sequence>";

TemplateApply[XMLTemplate[str], Association["Root" -> {"a", "b"}]]

(*
            <li>         a</li>       <seceond-level>         <wolfram:slot id='Root' />      
</seceond-level>        <li>         b</li>       <seceond-level>         <wolfram:slot id
='Root' />      </seceond-level>
*)

Without verbatim, a TemplateSlot will lose its function after TemplateApply, though we can use StringReplace which seems not the elegant way.

HyperGroups
  • 8,619
  • 1
  • 26
  • 63
  • How have you learned about id=\"SequenceIndex\"? – Kuba May 05 '15 at 07:03
  • I mean, how do you know you can write SequenceIndex and it works. I cn't find it in docs. – Kuba May 05 '15 at 08:16
  • @Kuba there are several examples in ref/XMLTemplate, that's `t = XMLTemplate["
      <wolfram:sequence values='#planets'>
    "]; TemplateApply[t, <|"planets" -> {"jupiter", "mars", "earth"}|>]`
    – HyperGroups May 05 '15 at 08:19
  • And where is id=\"SequenceIndex\" there? – Kuba May 05 '15 at 08:21
  • @Kuba Using named variables: `t = XMLTemplate["
      <wolfram:sequence values='#planets' slot='planet' index='counter'>
    • <wolfram:slot id='counter'/>: <wolfram:slot
      id='planet'/>
    • </wolfram:sequence>
    "]; TemplateApply[t, <|"planets" -> {"jupiter", "mars", "earth"}|>]` ·`SequenceIndex` seems just a DIY name.·
    – HyperGroups May 05 '15 at 08:34

0 Answers0