0

这是我的简单 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>

谢谢

4

1 回答 1

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
于 2013-04-11T10:17:56.037 回答