XSLT 1.0 - Create a Unique, Ordered List

I'm trying to create a transform where I generate an ordered unique list (where a hyphen separates two values.

I have source

<?xml version="1.0"?>
<results>
    <result>
        <Name>Blue</Name>
        <Author>Hat</Author>
        <TrackNum>5</TrackNum>
    </result>
    <result>
        <Name>Red</Name>
        <Author>Car</Author>
        <TrackNum>2</TrackNum>
    </result>
    <result>
        <Name>Blue</Name>
        <Author>Hat</Author>
        <TrackNum>345</TrackNum>
    </result>
</results>

And want the output (ordered by 'Name')

:: Blue - Hat
:: Red - Car

XLST - This currently returns nothing (I know it's missing the second, hyphenated/concatenated value too)

<?xml version="1.0" encoding="UTF-8"?>
  <xsl:stylesheet
   version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="UTF-8" indent="yes"/>

<xsl:key name="Name" match="/results/result/Name/text()" use="." />

<xsl:template match="/">

  <xsl:for-each select="/results/results/Name/text()[generate-id()
                                       = generate-id(key('Name',.)[1])]">
    <li>
      <xsl:value-of select="."/>
    </li>
  </xsl:for-each>

</xsl:template>

</xsl:stylesheet>

Answers


You were on the right track. You had a type-o in the XPath for your xsl:for-each select: /results/results should be /results/result. I made a slight adjustment to the key for what it matched against and used.

The following produces a unique list of Name and Author combinations, sorted by Name and then Author.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>

    <xsl:key name="Name-Author" match="results/result" use="concat(Name, ' - ', Author)" />

    <xsl:template match="/">

        <xsl:for-each select="/results/result[generate-id()
            = generate-id(key('Name-Author',concat(Name, ' - ', Author))[1])]">
            <xsl:sort select="Name"/>
            <xsl:sort select="Author"/>
            <li>
                <xsl:value-of select="concat(Name, ' - ', Author)"/>
            </li>
        </xsl:for-each>

    </xsl:template>

</xsl:stylesheet>

Need Your Help

Automated website interaction - Mechanzie - Rails

ruby-on-rails ruby mechanize-ruby

I'm using the Mechanize gem to automate interaction with a website form.

Converting a float in chars to float (PYTHON)

python floating-point byte valueconverter

I have a program in python in which I want to receive a frame with some values. This values are sent by a xbee.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.