这是我的xslt: -
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="*" mode="item">
<xsl:choose>
<xsl:when test="self::node()/child::*">
<li>
<xsl:value-of select="@value"/>
<xsl:apply-templates select="current()[*]"/>
</li>
</xsl:when>
<xsl:otherwise>
<li onclick="final()">
<xsl:value-of select="@value"/>
<xsl:apply-templates select="current()[*]"/>
</li>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="*/*">
<ul>
<xsl:apply-templates select="*[1] | node()[current()/ancestor::*[3]]" mode="item"/>
</ul>
</xsl:template>
</xsl:stylesheet>
这是我的xml: -
<?xml-stylesheet type="text/xsl" href="m.xsl"?>
<root>
<child_1 entity_id = "1" value="Game" parent_id="0">
<child_2 entity_id="2" value="Activities" parent_id="1">
<child_3 entity_id="3" value="Physical1" parent_id="2">
<child_6 entity_id="6" value="Cricket" parent_id="3">
<child_7 entity_id="7" value="One Day" parent_id="6"/>
</child_6>
</child_3>
<child_4 entity_id="4" value="Test1" parent_id="1">
<child_8 entity_id="8" value="Test At Abc" parent_id="4"/>
</child_4>
<child_5 entity_id="5" value="Test2" parent_id="1">
<child_9 entity_id="9" value="Test At Xyz" parent_id="5"/>
</child_5>
</child_2>
<child_10 entity_id="10" value="Region" parent_id="1">
<child_11 entity_id="11" value="ABC" parent_id="10">
<child_12 entity_id="12" value="XYZ" parent_id="11">
<child_13 entity_id="13" value="ABC123" parent_id="12"/>
</child_12>
</child_11>
</child_10>
</child_1>
</root>
这是在xslt代码上执行时的输出: -
<ul>
<li>Activities
<ul>
<li>Physical1
<ul>
<li>Cricket
<ul><li onclick="final()">One Day</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
这里我要输出类似的东西: -
<ul>
<li>Activities
<ul>
<li>Physical1
<ul>
<li>Cricket
<ul><li onclick="final()">One Day</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<ul id="last"></ul>
</ul>
问题是: - 我想在最后ul
li
答案 0 :(得分:1)
请在最后一个li之后更改你的最后一个模板,以获得ul:
<xsl:template match="*/*">
<ul>
<xsl:apply-templates select="*[1] | node()[current()/ancestor::*[3]]" mode="item"/>
<xsl:if test="self::*/parent::*/name() ='root'"><ul id="last"></ul></xsl:if>
</ul>
</xsl:template>
完整的XSL是:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="*" mode="item">
<xsl:choose>
<xsl:when test="self::node()/child::*">
<li>
<xsl:value-of select="@value"/>
<xsl:apply-templates select="current()[*]"/>
</li>
</xsl:when>
<xsl:otherwise>
<li onclick="final()">
<xsl:value-of select="@value"/>
<xsl:apply-templates select="current()[*]"/>
</li>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="*/*">
<ul>
<xsl:apply-templates select="*[1] | node()[current()/ancestor::*[3]]" mode="item"/>
<xsl:if test="self::*/parent::*/name() ='root'"><ul id="last"></ul></xsl:if>
</ul>
</xsl:template>
</xsl:stylesheet>
答案 1 :(得分:1)
@Jack Php:您必须在上一个模板中添加一行,如下所示
<xsl:template match="*/*">
<ul>
<xsl:apply-templates select="*[1] | node()[current()/ancestor::*[3]]" mode="item"/>
<xsl:if test="local-name(parent::*) = 'root'"><ul id="last"></ul></xsl:if>
</ul>
</xsl:template>
您可以找到关于local-name(parent::*)