0

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>   
1
  • desciption ≠ description Commented Aug 27, 2015 at 14:17

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

Comments

1

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.

3 Comments

I need XML Output. I basicly want to sort 2 big XML files and then compare and merge them.
When I apply the xslt, there are no tags anymore. How can I configure it to keep the XML tags?
I have a feeling you haven't really mastered the basics. Asking questions on SO when you're still on the first rung of the ladder is not very productive, because you probably won't understand the answers. Get a good book, go through the exercises in the introductory chapters, and come back here when you get stuck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.