这是xml文件: -
<?xml-stylesheet type="text/xsl" href="m.xsl"?>
<root>
<child_1 entity_id="1" value="Root Catalog" parent_id="0">
<child_2 entity_id="2" value="Navigate" parent_id="1">
<child_4 entity_id="4" value="Activities" parent_id="2">
<child_10066 entity_id="10066" value="Physical1" parent_id="4">
<child_10067 entity_id="10067" value="Cricket" parent_id="10066">
<child_10068 entity_id="10068" value="One Day" parent_id="10067"/>
</child_10067>
</child_10066>
<child_10069 entity_id="10069" value="Test2" parent_id="4"/>
<child_10070 entity_id="10070" value="Test3" parent_id="4"/>
<child_10071 entity_id="10071" value="Test4" parent_id="4"/>
<child_10072 entity_id="10072" value="Test5" parent_id="4"/>
<child_5 entity_id="5" value="Physical" parent_id="4"/>
</child_4>
<child_4331 entity_id="4331" value="Region" parent_id="2">
<child_4332 entity_id="4332" value="Asia" parent_id="4331">
<child_4333 entity_id="4333" value ="India" parent_id="4332">
<child_4334 entity_id ="4334" value = "Gujarat" parent_id="4333"/>
</child_4333>
</child_4332>
</child_4331>
</child_2>
</child_1>
</root>
这里我使用的是这种类型的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">
<li>
<xsl:value-of select="@value" />
<xsl:apply-templates select="current()[*]" />
</li>
</xsl:template>
<xsl:template match="*/*/*">
<ul>
<xsl:if test="local-name(parent::*) = 'root'"><xsl:attribute name="id"><xsl:text disable-output-escaping="yes">first</xsl:text> </xsl:attribute></xsl:if>
<xsl:apply-templates select="*[1] | node()[current()/ancestor::*[3]]" mode="item"/>
</ul>
</xsl:template>
</xsl:stylesheet>
这个xslt输出是: -
设置第一个id="frist"
如何才能使用xslt ... 感谢...
答案 0 :(得分:1)
更改您当前的模板:
<xsl:template match="*/*">
<ul>
<xsl:if test="local-name(parent::*) = 'root'"><xsl:attribute name="id"><xsl:text disable-output-escaping="yes">first</xsl:text> </xsl:attribute></xsl:if>
<xsl:apply-templates select="*[1] | node()[current()/ancestor::*[3]]" mode="item"/>
</ul>
</xsl:template>
这一个:
<xsl:template match="*/*/*">
<ul>
<xsl:if test="local-name(parent::*) = 'root'"><xsl:attribute name="id"><xsl:text disable-output-escaping="yes">first</xsl:text> </xsl:attribute></xsl:if>
<xsl:apply-templates select="*[1] | node()[current()/ancestor::*[3]]" mode="item"/>
</ul>
</xsl:template>