1

I want to create a alphabetically ordered list from an xml file. In the Xml file I have many <index-elements> which should be part of the list, I simply select them all by //index-elements. I also want to add another kind of elements, called <name>, to the same list at theire right position (alphabetically sorted). Usually I use the <xsl:for-each-group group-by="..." select="..."> loop in combination with the <xsl:sort lang="lang-code"> function, but I cannot select for more than one node-set. This is the first time, where I need to add, two different elements in one sorted result. At the moment I've no Idea how to solve this.

XML

<section>
    <child>
        <index-elements>Gamma</index-elements>
    </child>
    <child>
        <index-elements>Zeta</index-elements>
    </child>
</section>

<section>
    <child>
        <index-elements>Alpha</index-elements>
    </child>
    <child>
        <new-element>
            <index-elements>Delta</index-elements>
        </new-element>            
    </child>
</section>    

<section>
    <some-element>
        <name>Epsilon</name>
    </some-element>
    <some-element>
        <name>Beta</name>
    </some-element>
</section>  

Expected Output

Alpha Beta Gamma Delta Epsilon Zeta

Ole
  • 161
  • 1
  • 3
  • 12
  • No idea why you mention `for-each-group` if you want to sort a sequence, sorting in XSLT 2 or 3 can be done with `perform-sort` https://www.w3.org/TR/xslt-30/#creating-sorted-sequence e.g. ``. XSLT/XPath 3 also has a `sort` function https://www.w3.org/TR/xpath-functions/#func-sort which in its easiest form `sort((//index-elements, //figures))` might suffice or, when higher-order functions are supported, allows you to define the sort keys with a function. – Martin Honnen Jul 03 '18 at 14:30

1 Answers1

1

I solved it, by extending the for-each sequence for all //index-elements and //name. I was not aware about using two x-path search results for the select argument.

<xsl:for-each select="//index-elements, //name"> <xsl:sort lang="{$langCode}"/> <xsl:value-of select="current()/text()"/> </xsl:for-each>

Ole
  • 161
  • 1
  • 3
  • 12