Hello

I have an XML file that uses <Document Element>

I was wondering if it is possible to sort my XML file by sortOrder?

Here is an example of my code:

<?xml version="1.0" standalone="yes"?>
<DocumentElement>
  <Item DataField="A03" Header="RDAW TEST" UseDefaults="Yes" SortOrder="1"/>
  <Item DataField="A01" Header="Eg: Assignment 1" UseDefaults="Yes" SortOrder="2" />
  <Item DataField="A04" Header="RDAW TEST #2" UseDefaults="No" SortOrder="4" />
  <Item DataField="A02" Header="Eg: Assignment 2" UseDefaults="No" SortOrder="3" />
</DocumentElement>

I just want SortOrder to be sorted ascending when you view the XML file.

Thanks

Recommended Answers

All 7 Replies

If you use an xslt to transform it, then you can use xsl:sort. This link shows how.

If you use an xslt to transform it, then you can use xsl:sort. This link shows how.

Thank you i'll look into this.

Does it still work for my XML file as it's <DocumentElement> and then Items?

Just the XML file they are using as an example is slighlty different.

Thanks

It is just a name. Replace the tag references with yours.

Hello, i've tried it now but can't seem to get it working.

My XML code:

<?xml-stylesheet type="text/xsl" href="Y10_CCF_SPORT.xsl" version="1.0" standalone="yes"?>
<DocumentElement>
  <Item DataField="A03" Header="RDAW TEST" UseDefaults="Yes" SortOrder="1"/>
  <Item DataField="A01" Header="Eg: Assignment 1" UseDefaults="Yes" SortOrder="2" />
  <Item DataField="A04" Header="RDAW TEST #2" UseDefaults="No" SortOrder="4" />
  <Item DataField="A02" Header="Eg: Assignment 2" UseDefaults="No" SortOrder="3" />
</DocumentElement>

My XSL code:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     version="1.0">

  <xsl:output method="text"/>

  <xsl:template match="DocumentElement">
    <xsl:apply-templates>
      <xsl:sort select="SortOrder"/>
    </xsl:apply-templates>
  </xsl:template>

</xsl:stylesheet>

You are using an attribute, not a tag. Try @SortOrder

I've tried @SortOrder but still no luck it's just blank when I open the XML document?

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     version="1.0">

  <xsl:output method="text"/>

  <xsl:template match="DocumentElement">
    <xsl:apply-templates>
      <xsl:sort select="@SortOrder"/>
    </xsl:apply-templates>
  </xsl:template>

</xsl:stylesheet>

Where is you template output for Item ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.