Note that there are some explanatory texts on larger screens.

plurals
  1. POGeneric XML sorting via XSLT applied by Java?
    primarykey
    data
    text
    <p>Basically, I want an exact copy of the XML, except I'd like to sort certain nodes by their ID attribute. Not all elements with the ID attribute should be sorted. </p> <p>I've kludged together a working stylesheet, but it requires me to hard-code the &lt;xsl:apply-template&gt;'s in for all sibling nodes to the nodes I'm sorting on. I've no formal schema, and I can't be certain that it won't change. I'd like to be able to create a more generic stylesheet which will output the non-sorted nodes without these explicit calls. </p> <p>Is this possible?</p> <p><strong>My Java code to transform the XML:</strong></p> <pre><code>String input = "C:\\presort.xml"; String output = "C:\\postsort.xml"; String xsl = "C:\\sort.xsl" Document dom; try { dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(input); Element e = dom.getDocumentElement(); Transformer transform = TransformerFactory.newInstance().newTransformer( new StreamSource( new File(xsl) )); StreamResult result = new StreamResult(new File(output)); transform.transform(new DOMSource(dom), result); } catch (Exception e) { e.printStackTrace(); } </code></pre> <p><strong>My XML:</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt; &lt;Person id="1"&gt; &lt;Base&gt;true&lt;/Base&gt; &lt;Description&gt;something&lt;/Description&gt; &lt;Name&gt;Vin Vinigar&lt;/Name&gt; &lt;Privileges&gt; &lt;Privilige id="340"&gt; &lt;Permission&gt;2&lt;/Permission&gt; &lt;Job id="dontsort" /&gt; &lt;/Privilige&gt; &lt;Privilige id="11"&gt; &lt;Permission&gt;3&lt;/Permission&gt; &lt;Job id="dontsort" /&gt; &lt;/Privilige&gt; &lt;Privilige id="1011"&gt; &lt;Permission&gt;1&lt;/Permission&gt; &lt;Job id="2342" /&gt; &lt;/Privilige&gt; &lt;/Privileges&gt; &lt;/Person&gt; &lt;Person id="f32"&gt; &lt;Base&gt;true&lt;/Base&gt; &lt;Description&gt;Here be dragons&lt;/Description&gt; &lt;Name&gt;John Doe&lt;/Name&gt; &lt;Privileges&gt; &lt;Privilige id="23a"&gt; &lt;Permission&gt;2&lt;/Permission&gt; &lt;Job id="a2a" /&gt; &lt;/Privilige&gt; &lt;/Privileges&gt; &lt;/Person&gt; &lt;Person id="22"&gt; &lt;PossibleUnknownTagHere&gt;something&lt;/PossibleUnknownTagHere&gt; &lt;Name&gt;Han Solo&lt;/Name&gt; &lt;Privileges&gt; &lt;Privilige id="23a"&gt; &lt;Permission&gt;3&lt;/Permission&gt; &lt;Job id="a2a" /&gt; &lt;/Privilige&gt; &lt;/Privileges&gt; &lt;/Person&gt; &lt;/root&gt; </code></pre> <p><strong>My stylesheet:</strong></p> <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt; &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:template match="root|@*"&gt; &lt;xsl:copy &gt; &lt;xsl:apply-templates select="@*" /&gt; &lt;xsl:apply-templates select="Person"&gt; &lt;xsl:sort select="@id" order="ascending" /&gt; &lt;/xsl:apply-templates&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;xsl:template match="Person"&gt; &lt;xsl:copy &gt; &lt;xsl:apply-templates select="@*" /&gt; &lt;!-- Works but requies a template for each sibling element --&gt; &lt;xsl:apply-templates select="Base" /&gt; &lt;xsl:apply-templates select="Description" /&gt; &lt;xsl:apply-templates select="Name" /&gt; &lt;!-- I'd like to do something like this --&gt; &lt;!-- &lt;xsl:choose&gt; &lt;xsl:when test="not(Privileges)"&gt; &lt;xsl:apply-templates select="." /&gt; &lt;/xsl:when&gt; &lt;/xsl:choose&gt; --&gt; &lt;xsl:apply-templates select="Privileges" /&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;!-- I'd like to remove the need for these 3 explicity copies --&gt; &lt;xsl:template match="Base"&gt; &lt;xsl:copy-of select="."/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="Description"&gt; &lt;xsl:copy-of select="."/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="Name"&gt; &lt;xsl:copy-of select="."/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="Privileges"&gt; &lt;xsl:copy &gt; &lt;xsl:apply-templates select="@*" /&gt; &lt;xsl:apply-templates select="Privilege"&gt; &lt;xsl:sort select="@id" order="ascending" /&gt; &lt;/xsl:apply-templates&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;xsl:template match="Privilege"&gt; &lt;xsl:copy-of select="."/&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload