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="/"> <rule> <xsl:apply-templates select=".//condition" /> </rule> </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> <condition type="<xsl:value-of select="@type"/>"><br /> </condition> </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"> <rule> <xsl:apply-templates select="condition" /> </rule> </xsl:template>
and have condition
template handle recursion applying templates own children, e.g.:
<xsl:template match="condition"> <ul> <li> <condition type="<xsl:value-of select="@type"/>"><br /> <xsl:apply-templates select="condition"/> </condition> </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
Post a Comment