这是我的简单xml文件: -
<child_2 entity_id="2" value="Root" 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_2>
我希望获得entity_id 4
的所有子值
用逗号分隔,
并保存为文本文件
这是我的o / p: -
10066,10067,10068,10069,10070,10071
这是我的尝试: -
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8"/>
<xsl:key name="kChild" match="*[@entity_id]" use="../@entity_id"/>
<xsl:param name="region" select="'4'" />
<xsl:template match="/">
<xsl:apply-templates select="key('kChild', $region)" />
</xsl:template>
<xsl:template match="*">
<xsl:value-of select="@entity_id"/>
<xsl:text>,</xsl:text>
</xsl:template>
</xsl:stylesheet>
感谢
答案 0 :(得分:1)
获取@entity_id
属性值为@entity_id
的元素的每个后代的$region
属性值:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8"/>
<xsl:param name="region" select="'4'"/>
<xsl:template match="/">
<xsl:apply-templates select="*/*[@entity_id = $region]//*"/>
</xsl:template>
<xsl:template match="*">
<xsl:value-of select="@entity_id"/>
<!-- Don't add comma after last number -->
<xsl:if test="not(position() = last())">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
<child_2 entity_id="2" value="Root" 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_2>
10066,10067,10068,10069,10070,10071,10072,5