html - How to know when to put closing tag on nested elements -


i'm converting following xml html:

<rule>     <condition type="and">         <condition type="and">             <condition type="not">             </condition>         </condition>     </condition> </rule> 

from structure, can infer condition can have other condition's (or not last condition).

i'm trying make same structure in html , not sure when/how/where put closing tags conditions have conditions inside of them. output i'm getting output above:

<rule>     <condition type="and">     </condition>          <condition type="and">         </condition>              <condition type="not">             </condition>  </rule> 

here's xsl:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">     <xsl:output method="html" />     <xsl:template match="/">         &lt;rule&gt;         <xsl:apply-templates select=".//condition" />         &lt;/rule&gt;     </xsl:template>     <xsl:template match="condition">         <xsl:variable name="margin-left"><xsl:number value="(count(ancestor::condition) + 1) * 20" />px</xsl:variable>         <ul class="list-unstyled" style="margin:0px;margin-left:{$margin-left}">             <li>                 &lt;condition type="<xsl:value-of select="@type"/>"&gt;<br />                 &lt;/condition&gt;             </li>         </ul>     </xsl:template> </xsl:stylesheet> 

the select=".//condition" has effect of flattening out hierarchy, you're selecting condition elements @ levels single list. instead, need start applying templates top-level condition:

<xsl:template match="/rule">     &lt;rule&gt;     <xsl:apply-templates select="condition" />     &lt;/rule&gt; </xsl:template> 

and have condition template handle recursion applying templates own children, e.g.:

<xsl:template match="condition">     <ul>         <li>             &lt;condition type="<xsl:value-of select="@type"/>"&gt;<br />             <xsl:apply-templates select="condition"/>             &lt;/condition&gt;         </li>     </ul> </xsl:template> 

(i've omitted styling part, can sort out in full stylesheet once have concept of using recursion - in fact, if use padding rather margin may not need whole counting of ancestors trick @ since each ul nests within previous one).


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -