0

I'm trying to sort the following xml file by <title>. I have no experience with xml or xsl. I'd like to output a file in the same xml format but sorted. I've included the xsl I have so far. Would very much appreciate any advice or pointers to how I can achieve this. Thanks for any help David

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="j2014-sort.xsl"?>
<channel>   
    <item>
        <title>_zigfried j Myers (25000, )</title>
        <link>http://masortiolami.org/_zigfried-j-myers-25000/</link>
    </item>
    <item>
        <title>_Joe Blogs (500, )</title>
        <link>http://masortiolami.org/_joe-blogs-500/</link>
    </item>
    <item>
        <title>_Minni Mouse (0, 641)</title>
        <link>http://masortiolami.org/_minni-mouse-0-641/</link>
    </item>
    <item>
        <title>_Sitting Bull (10000, )</title>
        <link>http://masortiolami.org/_sitting-bull-10000/</link>
    </item>
</channel>

and this is the xsl:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:result-document href="j2014-new.xml" method="xml">
         <xsl:copy>
            <xsl:apply-templates/>
         </xsl:copy>
      </xsl:result-document> 
  <xsl:apply-templates>
    <xsl:sort select="title"/>
  </xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>

When I run this xsl in the browser it produces no output. Just a listing of the code.

0

2 Answers 2

1

To sort the list by title, you have to adjust your XSLT a bit. For testing purpose on online XSLT processor I adjusted it as follows:

<?xml version="1.0"?>
<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:template match="/*">
<channel>
  <xsl:apply-templates select="item">
    <xsl:sort select="title"/>
  </xsl:apply-templates>
</channel>
</xsl:template>
<xsl:template match="item">
  <xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>  

Result:

<?xml version="1.0" encoding="UTF-8"?>
<channel>  
  <item>
    <title>_Joe Blogs (500, )</title>
    <link>http://masortiolami.org/_joe-blogs-500/</link>
  </item>
  <item>
    <title>_Minni Mouse (0, 641)</title>
    <link>http://masortiolami.org/_minni-mouse-0-641/</link>
  </item>
  <item>
    <title>_Sitting Bull (10000, )</title>
    <link>http://masortiolami.org/_sitting-bull-10000/</link>
  </item>
  <item>
    <title>_zigfried j Myers (25000, )</title>
    <link>http://masortiolami.org/_zigfried-j-myers-25000/</link>
  </item>
</channel>

As you'll notice, there are only a few adjustments needed - first template matches all from the root node (match="/*"). In this template only the item-elements are applied, using xsl:sort with title as key for sorting. A second template matching item-elements just copies the sorted item-nodes with <xsl:copy-of/>.
And you should consider to edit your question - because of missing code formatting the <title> is not displayed in your sentence "I'm trying to sort the following xml file by ." Just noticed this looking at the question in edit-mode but wasn't able to change this as edits require to change at least 6 characters. There's also a minor issue with your XML - it starts with </channel> instead of <channel>, so it's not valid.

Another adjustment only necessary for the mentioned test with an online XSLT tool was the removal of xsl:result-document element.
As the xsl:result-document-element only works for XSLT 2.0, is should work for you when you readd your xsl:result-document and change to XSLT version 2.0 in your xsl:stylesheet-element, in case your XSLT-processor can handle XSLT 2.0. So the complete XSLT for you would be

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<xsl:result-document href="j2014-new.xml" method="xml">
<channel>
  <xsl:apply-templates select="item">
    <xsl:sort select="title"/>
  </xsl:apply-templates>
</channel>
</xsl:result-document>
</xsl:template>
<xsl:template match="item">
  <xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>

In case you're only able to work with XSLT 1.0, please update this in your question and provide information about how you process the XSLT and which XSLT processor you're using (e.g. if it's Saxon or else). Depending on your environment/settings, it's e.g. possible to adjust the output-filename in the specific configuration.

For reference: http://www.saxonica.com/documentation/xsl-elements/result-document.html and http://www.saxonica.com/documentation/xsl-elements/output.html

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much for this code and detailed explanation, but there's no mention of input or output xml file names, so how would I run the XSLT to get a new sorted XML file, I assume the online XSlT processor is the browser.
0

When I run this xsl in the browser it produces no output.

Running an XSL transformation in a browser does not produce a new file. All a browser will do is display the result on-screen.

Note also that xsl:result-document is a XSLT 2.0 instruction. There is no browser that supports XSLT 2.0.

In any case, if you want your transformation to produce a new file, you must run it using another tool - e.g. directly from the command line. In such case you can specify the resulting file's name and path at the same time you initiate the transformation.

1 Comment

Thanks for all the support, pointing me in the right direction. Good to have someone to talk to, hopefully I'll be able to progress on my own from here.

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.