Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to indent an xml document

Status
Not open for further replies.

ddiamond

Programmer
Apr 22, 2005
918
US
I am receiving a rather long soap response from a web service. The only problem is that the xml is all on one line. Is there anyway to easily transform it into a properly indented xml document? Possibly xslt? Or xml dom?
 
Check the indent attribute on the xsl:eek:utput element if you wish to use XSLT. You could use an identity transform to do the copying; see section 7.5 here.

Tom Morrison
 
I looked up the indent attribute as you suggested. I found the following example:
Code:
<xsl:output method="xml" omit-xml-declaration="no" indent="yes" encoding="US-ASCII"/>
Do I only need to include the indent attribute, or are the other attributes also important?

Oh, and thanks for the link for the identity transform. Haven't tried it yet, but it looks straight forward.

 
xml is the default method. encoding probably not useful. omit-xml-declaration controls the output of the <?xml... header.

Try this:
Code:
<xsl:output indent="yes" />

Tom Morrison
 
Ok it almost works. This is what I came up with:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
<xsl:output method='xml' version='1.0' encoding='UTF-8' indent='yes'/>

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>
The result is that each element is on a seperate line (a big improvement), but everything is still at the same indentation. I was hoping that child nodes would be indented from their parent nodes.
 
My guess is that this depends on your XML/XSLT processor. The XSLT specification is permissive, viz "specifies whether the XSLT processor may add additional whitespace when outputting the result tree." [small]emphasis added[/small] So, no actual indenting is required.

Tom Morrison
 
I'm using the following javascript function to do the translation:
Code:
<script type="text/javascript">

function Translate(XMLFileName, XSLFileName)
{
  // Load XML 
  var xml = new ActiveXObject("Microsoft.XMLDOM");
  xml.async = false;
  xml.load(XMLFileName); 
  
  // Load XSL
  var xsl = new ActiveXObject("Microsoft.XMLDOM");
  xsl.async = false;
  xsl.load(XSLFileName);
  
  // Transform
  return xml.transformNode(xsl);
}  

</script>
Does that mean Microsoft.XMLDOM is the xml/xslt processor that I'm using?
 
I would guess that it means that the processor is MSXML. You might check the version on your system, and check the Microsoft website for the most recent version.

Tom Morrison
 
I found a freeware program called Tidy.exe which does a pretty good job of indenting XML.
 
Looks like the transformNode method ignores the output element. The transformNodeToObject method works perfectly, however. See thread426-1379722.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top