janor
janor

Reputation: 179

Sort a specific XML File with XSLT by multiple attributes

Here is the XML structure. I have to sort/order by category name first, then by preference name and then sort the value list itself. I am new to xslt. How to achieve multiple ordering?

<?xml version="1.0" encoding="ISO-8859-1"?>
<preferences version="10.0">
  <category name="Administration.Access Manager">
   <category_description></category_description>
    <preference name="ADA_admin_notifier_list" type="String" array="true" disabled="false" protectionScope="Site" envEnabled="false">
     <preference_description>Text</preference_description>
    </preference>
    <preference name="ADA_allow_gov_classification_propagation" type="Logical" array="false" disabled="false" protectionScope="Site" envEnabled="false">
     <preference_description>Text</preference_description>
     <context name="Teamcenter">
      <value>b</value>
      <value>c</value>
      <value>a</value>
     </context>
   </preference>
   <preference name="ADA_saveas_propagated_license_types" type="String" array="true" disabled="false" protectionScope="Site" envEnabled="false">
    <preference_description>TEXT</preference_description>
     <context name="Teamcenter">
      <value>IP_License</value>
      <value>ITAR_License</value>
      <value>Exclude_License</value>
    </context>
  </preference>
 </category>
</preferences>

How can I apply a xslt via Browser. How to reference the xslt in the XML?

Edit: I now tried something like the following. First Problem there are missing some attributes in the Output like the descriptions. I think I don't understand the apply-template right. Second Problem is that the values are not sorted. The category name and the Preference name is sorted

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


 <xsl:strip-space elements="*"/>


<xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>


    <xsl:template match="preferences">
     <xsl:copy>
     <xsl:apply-templates select="@*"/>
        <xsl:apply-templates select="category">
            <xsl:sort select="@name"/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

   <xsl:template match="category">
     <xsl:copy>
    <xsl:apply-templates select="@*"/>
        <xsl:apply-templates select="preference">
            <xsl:sort select="@name"/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

 <xsl:template match="preference">
     <xsl:copy>
     <xsl:apply-templates select="@*"/>
     <xsl:apply-templates select="preference_desciption"/>
        <xsl:apply-templates select="context">
            <xsl:sort select="value" data-type="text"/>
        </xsl:apply-templates>
   </xsl:copy>
</xsl:template>

</xsl:stylesheet>   

Upvotes: 0

Views: 537

Answers (2)

michael.hor257k
michael.hor257k

Reputation: 116992

Try it this way:

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

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="/preferences">
     <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates select="category">
            <xsl:sort select="@name"/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

<xsl:template match="category">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates select="preference">
            <xsl:sort select="@name"/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

<xsl:template match="context">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates select="value">
            <xsl:sort select="."/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Upvotes: 1

Michael Kay
Michael Kay

Reputation: 163322

You haven't explained your requirements very clearly: for example there's no hint what output you want to produce (is it HTML?). If you want to sort the preferences within a category, you can do

<xsl:template match="category">
  <xsl:apply-templates select="preference">
    <xsl:sort select="@name"/>
  </xsl:apply-templates>
</xsl:template>

and similarly for the other things you want to sort.

Upvotes: 1

Related Questions