从xml子集获取唯一值

时间:2010-11-11 10:42:30

标签: xslt

我试图从一个xml文件中获取几个唯一的列表。

xml格式是

<Response>
  <Tracks>
        <Track>
              <Name>Track1</Name>
              <Contributors>
                    <Contributor>
                          <Role>Recording Artist</Role>
                          <Name>aaa</Name>
                    </Contributor>
                    <Contributor>
                          <Role>Recording Artist</Role>
                          <Name>bbb</Name>
                    </Contributor>
                    <Contributor>
                          <Role>Recording Artist</Role>
                          <Name>aaa</Name>
                    </Contributor>
              </Contributors>
        </Track>
        <Track>
              <Name>Track2</Name>
              <Contributors>
                    <Contributor>
                          <Role>Recording Artist</Role>
                          <Name>ddd</Name>
                    </Contributor>
                    <Contributor>
                          <Role>ReMixer</Role>
                          <Name>bbb</Name>
                    </Contributor>
                    <Contributor>
                          <Role>Recording Artist</Role>
                          <Name>ddd</Name>
                    </Contributor>
              </Contributors>
        </Track>
        <Track>
              <Name>Track3</Name>
              <Contributors>
                    <Contributor>
                          <Role>Recording Artist</Role>
                          <Name>ccc</Name>
                    </Contributor>
                    <Contributor>
                          <Role>Recording Artist</Role>
                          <Name>ccc</Name>
                    </Contributor>
                    <Contributor>
                          <Role>Recording Artist</Role>
                          <Name>aaa</Name>
                    </Contributor>
              </Contributors>
        </Track>
  </Tracks>

所以我需要以曲目名称的格式显示数据,并使用与该曲目相关的独特录音艺术家

我看过钥匙和兄弟姐妹的事情,但似乎没有足够好的解释我可以应用我的问题

任何帮助表示赞赏!

- 我也在使用xslt 1.0

1 个答案:

答案 0 :(得分:1)

此样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="kContributorByListAndName"
             match="Contributor"
             use="concat(generate-id(..),'++',Name)"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="Contributor
                            [count(.|key('kContributorByListAndName',
                                         concat(generate-id(..),'++',Name))[1])
                             !=1]"/>
</xsl:stylesheet>

输出:

<Response>
    <Tracks>
        <Track>
            <Name>Track1</Name>
            <Contributors>
                <Contributor>
                    <Role>Recording Artist</Role>
                    <Name>aaa</Name>
                </Contributor>
                <Contributor>
                    <Role>Recording Artist</Role>
                    <Name>bbb</Name>
                </Contributor>
            </Contributors>
        </Track>
        <Track>
            <Name>Track2</Name>
            <Contributors>
                <Contributor>
                    <Role>Recording Artist</Role>
                    <Name>ddd</Name>
                </Contributor>
                <Contributor>
                    <Role>ReMixer</Role>
                    <Name>bbb</Name>
                </Contributor>
            </Contributors>
        </Track>
        <Track>
            <Name>Track3</Name>
            <Contributors>
                <Contributor>
                    <Role>Recording Artist</Role>
                    <Name>ccc</Name>
                </Contributor>
                <Contributor>
                    <Role>Recording Artist</Role>
                    <Name>aaa</Name>
                </Contributor>
            </Contributors>
        </Track>
    </Tracks>
</Response>