none
XSLT - How can i sort attributes in all tags by name?

    Question

  • I have following xml.

    <?xml version="1.0" encoding="utf-8"?>
    <Parameters c="1" d="3" a="5" b="3">
       <Parameters.Test c4="4" d2="2" a1="1" b8="8">
          <Test g="1" a="3"/>
       </Parameters.Test>
    </Parameters>

    I want to transform this xml to below xml.

    <?xml version="1.0" encoding="utf-8"?>
    <Parameters a="5" b="3" c="1" d="3">
       <Parameters.Test a1="1" b8="8" c4="4" d2="2">
          <Test a="3" g="1"/>
       </Parameters.Test>
    </Parameters>

    How can i do this using xslt?

    Tuesday, September 03, 2013 1:59 PM

Answers

  • Try the following XSLT:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
      <xsl:template match="*">
        <xsl:copy>
          <xsl:apply-templates select="@*">
            <xsl:sort select="name()"/>
          </xsl:apply-templates>
          <xsl:apply-templates/>
        </xsl:copy>
      </xsl:template>
      <xsl:template match="@*|comment()|processing-instruction()">
        <xsl:copy />     
      </xsl:template>
    </xsl:stylesheet>

    Morten la Cour


    • Edited by la Cour Monday, September 09, 2013 8:38 AM
    • Marked as answer by Tempeck81 Monday, September 09, 2013 8:40 AM
    Monday, September 09, 2013 8:35 AM

All replies

  • What for?

    See http://www.w3.org/TR/REC-xml/#sec-starttags. Note that the order of attribute specifications in a start-tag or empty-element tag is not significant.

    Tuesday, September 03, 2013 3:15 PM
  • I know there is no difference for computers but i have tags with more than 50 attributes and i want that xml to be human-searchable. If attributes were in alphabetic order, i can find attribute easily.
    Wednesday, September 04, 2013 6:01 AM
  • Hello,

    Thank you for posting your question to this forum.

    According to your description you want to know how to sort attributes in an xml element.

    In my opinion, the XSLT does not provide a sort function for attributes.

    For implementing what you want, I think it needs to rewrite the XML file.

    Please have a try with steps below:

    1: Create collections to collect attributes for every XML element.

    2: Remove these attributes in XML element.

    3: Sort for attributes in collections

    4: Add attributes sorted into the XML element.

    Best Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.



    Wednesday, September 04, 2013 9:55 AM
    Moderator
  • Try the following XSLT:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
      <xsl:template match="*">
        <xsl:copy>
          <xsl:apply-templates select="@*">
            <xsl:sort select="name()"/>
          </xsl:apply-templates>
          <xsl:apply-templates/>
        </xsl:copy>
      </xsl:template>
      <xsl:template match="@*|comment()|processing-instruction()">
        <xsl:copy />     
      </xsl:template>
    </xsl:stylesheet>

    Morten la Cour


    • Edited by la Cour Monday, September 09, 2013 8:38 AM
    • Marked as answer by Tempeck81 Monday, September 09, 2013 8:40 AM
    Monday, September 09, 2013 8:35 AM
  • Thank you very much :)
    Monday, September 09, 2013 8:41 AM