views:

424

answers:

3

I am using XSLT to reformat some XML, to (a) turn elements into attributes and (b) sort everything alphabetically. It works EXCEPT the attributes are all on one line. I understand this is usually preferable but in this case I need each attribute to appear on a separate line when I open the file in Notepad++ (or whatever).

I can add XML-style linefeeds with xsl:text, but that's not what I want. The value-of separator attribute doesn't work (this is under .NET's XSL library). I want 'non-XML' line feeds eg. the kind that a non-XML-aware editor will interpret to display each attribute on its own line. Any ideas?

+1  A: 

I don't think you can do this with XSLT. What you are looking for is a pretty printer for XML. Why don't you post-process the output of XSLT? I generally pipe such stuff through "xmllint --format -", but that does not do the formatting you want. How hard can it be to roll your own? Especially, if it only needs to do the subset of XML you are using (this stuff gets easier if you don't care about namespaces etc.).

Daren Thomas
Yeah, I think you are right. Not that hard... it's just that XSL seems to support just about everything already, I was hoping it would do this too! Cheers and thanks.
A: 

It is not clear what you mean with "XML" vs. "non-XML" line feeds, but is that what you want:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
      <node>
        <xsl:text>&#010;</xsl:text>
      </node>
    </xsl:template>
</xsl:stylesheet>
0xA3
Thank you, but that actually puts the #010 into the output. Whereas I want a CRLF character in the output so when I open the file in a non-XML aware editor, there are line breaks.
actually, that is not what he want's - he wants to use <xsl:attribute/> and have those start on a new line, as opposed to starting on the same line as the node they are being attached to...
Daren Thomas
A: 

Are you referring to newlines between attributes or within attribute values? If the latter, it simply can't be done, due to the XML attribute normalization requirements:

This is because XML 1.0 requires newline characters in attribute values to be normalized into spaces but requires character references to newline characters not to be normalized. The attribute values in the data model represent the attribute value after normalization. If a newline occurring in an attribute value in the tree were output as a newline character rather than as character reference, then the attribute value in the tree created by reparsing the XML would contain a space not a newline, which would mean that the tree had not been output correctly.

Ben Blank
I am referring to between, so the attributes appear in a generic editor with one attribute per line.