XSLT and line feeds

I am using XSLT to reformat some XML to (a) rotate the elements into attributes and (b) sort everything alphabetically. It works to EXCEPT attributes all on one line. I realize this is usually preferred, 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 strings with xsl: text, but that's not what I want. The value-of separator attribute does not work (this is in the .NET XSL library). I want, for example, "non-XML" strings. a view that a non-XML editor will interpret to display each attribute on its own line. Any ideas?

+1


a source to share


3 answers


I don't think you can do this with XSLT. What you are looking for is a nice looking XML printer. Why aren't you doing XSLT processing? I usually stream things like this through " xmllint --format -

", but that doesn't do the formatting I want. How hard can you roll yourself? Especially if you only need to execute a subset of the XML you are using (this stuff gets easier if you don't need namespaces, etc.).



+1


a source


It's not clear what you mean with "XML" and "non-XML" line feeds, but this is what you want:



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

    <xsl:template match="/">
      <node>
        <xsl:text>&#010;</xsl:text>
      </node>
    </xsl:template>
</xsl:stylesheet>

      

0


a source


Are you referring to newlines between attributes, or within attribute values? If the latter, it is simply impossible to do, due to the XML attribute normalization requirements :

This is because XML 1.0 requires newline characters in attribute values ​​to be normalized to spaces, but requires newline references not to be normalized. Attribute values ​​in the data model represent the value of the attribute after normalization. If a newline occurring in an attribute value in a tree was outputted as a newline character rather than as a character reference, then the attribute value in the tree generated by reprocessing the XML would contain a space rather than a newline, which would mean that the tree is not displayed correctly.

0


a source







All Articles