Treemonkey
Treemonkey

Reputation: 2163

Getting Unique values from xml subset

I am attempting to get several unique lists out of one xml file.

The xml format is

<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>

So I am required to display the data in the format of track name with the unique recording artists ascosiated with that track

I've looked at keys and preceding sibling things but they dont seem to be explained well enough for me to apply to my problem

Any help appreciated!

-also I am using xslt 1.0

Upvotes: 0

Views: 256

Answers (1)

user357812
user357812

Reputation:

This stylesheet:

<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>

Output:

<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>

Upvotes: 1

Related Questions