Consider bla.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<rooty>
<person>
<age>42</age>
<name>bob</name>
</person>
<person>
<age>53</age>
<name>doggo</name>
</person>
</rooty>
And transform moo.xslt
, adding to each person a cow:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="no" indent="yes" encoding="UTF-8"/>
<!-- Identity transform -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="person">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:copy-of select="node()"/>
<cow>Bessy</cow>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Running it with xsltproc moo.xslt bla.xml
seems to miss a newline (after cow
):
<?xml version="1.0" encoding="UTF-8"?>
<rooty>
<person>
<age>42</age>
<name>bob</name>
<cow>Bessy</cow></person>
<person>
<age>53</age>
<name>doggo</name>
<cow>Bessy</cow></person>
</rooty>
I managed to work around this by manually injecting spaces using things like <xsl:text>
 </xsl:text>
, but I'm sure this is not the way. I assume my XSLT is doing something stupid, but I can't figure out what.
xsl:output indent="yes"
work best with<xsl:strip-space elements="*"/>
to give a consistent indentation. I have checked your example with xsltproc, however. – Martin Honnen Sep 6 '18 at 14:27