XML Sort addin

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Simplifier
    Visitor
    • Jan 2011
    • 3

    XML Sort addin

    I posted yesterday, but I guess I am not eligible yet.

    After some investigation, I realize the problem I have is with the xslt file. As written, it has no provision for sorting like-named elements. So, if you have 50 elements of the sort:
    <whatever name="zyx" />
    <whatever name="qrd" />
    ...
    <whatever name="abc" />

    They end up sorted in essentially random order.

    If I were expert enough in xslt (I am essentially a novice) I guess I could solve this myself.

    The XML I want to compare has multiple elements of the same type on multiple levels. I need an xslt that will sort elements of the same type by their name attribute, within an overall sort by element. (And, sorted by attribute within element)

    From a cursory Google, I gather this is a messy problem in the general case.

    Maybe an xslt guru will join in.
  • Simplifier
    Visitor
    • Jan 2011
    • 3

    #2
    Originally posted by Simplifier
    The XML I want to compare has multiple elements of the same type on multiple levels. I need an xslt that will sort elements of the same type by their name attribute, within an overall sort by element. (And, sorted by attribute within element)
    Once I realized I didn't really care what order things are, it turned out to be fairly easy to do the sort. What matters (for comparison purposes) is consistency, not the particular order of things. Therefore, I did not need a general "sort by name attribute within sort by element" solution.

    I created a new transform replacing:

    <xsl:apply-templates select="*">
    <xsl:sort order="ascending" data-type="text" select="local-name()" />
    </xsl:apply-templates>
    with:
    <xsl:apply-templates select="*[not(local-name()='Properties')]">
    <xsl:sort order="ascending" data-type="text" select="local-name()" />
    </xsl:apply-templates>
    <xsl:apply-templates select="Properties">
    <xsl:sort order="ascending" data-type="text" select="@name" />
    </xsl:apply-templates>
    My knowledge of xslt would fit easily inside a child's thimble, but amazingly, this sorts everything consistently with "Properties" elements on multiple levels. (Properties within Properties within Properties, or Properties within other elements.) At each level, all elements except Properties appear first, in sorted order, and all Properties elements appear after that, sorted by name attribute.

    xslt=amazing

    BC=Amazing Value!

    Comment

    • Aaron
      Team Scooter
      • Oct 2007
      • 16026

      #3
      Hello,

      Thanks for the feedback and example edit. I'm glad you were able to get that worked out and that with a small custom edit you were able to sort your XML structure.

      As for your initial posting, we have to manually approve your first post. Once that is done, all your future posts will be automatically approved. We do this to cut down on the spam that becomes visible to our customers.
      Aaron P Scooter Software

      Comment

      Working...