1

In javascript, trying to transform a dynamically created XML data island, sorting it using a XSL file, but the result is the sorted data all on one line, without XML formatting or proper indenting. It looks like the is not being used at all. I need the XML tags and indenting to be generated in the resulting transformNode().

javascript code:

var sourceXML = document.getElementById(XMLViewID); //textArea containing XML
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(sourceXML.value);

var xslDoc = new ActiveXObject("Microsoft.XMLDOM");
xslDoc.async=false;
xslDoc.load("xsl.xsl");

// This should be the sorted, formatted XML data, in tree and indented format?
var sorted = xmlDoc.transformNode(xslDoc);

XML DATA:

    <table>
    <row>
        <A>j</A>
        <B>0</B>
    </row>
    <row>
        <A>c</A>
        <B>4</B>
    </row>
    <row>
        <A>f</A>
        <B>6</B>
    </row>
</table>

xsl.xsl:

<xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>

<xsl:template match="/">
    <xsl:apply-templates select="table/row">
        <xsl:sort select="A" order="ascending"/>
    </xsl:apply-templates>
</xsl:template>

<xsl:template match="row">
    <xsl:value-of select="A"/>
    <xsl:value-of select="B"/>
</xsl:template>

I assume with 'indent=yes' and 'omit-xml-declaration=no' that the resulting transformation should be with indenting and formatting:

 <?xml version="1.0" encoding="UTF-16"?>
     <table>
       <row>
         <tr>
           <A>j</A>
           <B>0</B>
         </tr>
         <tr>
           <A>c</A>
           <B>4</B>
         </tr>
         <tr>
           <A>f</A>
           <B>6</B>
         </tr>
       </row>
     </table>

But instead it is: c4f6j0 in one line, no formatting, no XML tags...

1 Answer 1

1

So far your XSLT produces nothing but text nodes, if you want XML with elements then you need to create them with code like

<xsl:template match="table">
  <xsl:copy>
    <xsl:apply-templates select="row">
      <xsl:sort select="A"/>
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>
Sign up to request clarification or add additional context in comments.

3 Comments

The template with match="@* | node() is the identity transform. It makes sure that nodes and attributes not mentioned in other templates are copied as is. more
That worked great to get the xml tags in there. I think I already know the answer to my next question, but is there a way for the indented formatting to be put into the sorted string as well? The result I get back is everything expected but without the indentation. My guess is that is up to whatever is displaying the resulting xml, but it would be great if there were a way for the xslt to add that automatically.
Try to figure it out by looking at the answers of stackoverflow.com/search?q=[xslt]+white+space+preserve .

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.