1
$\begingroup$

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.

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.