How to add a new line to document by XSLT?
I have 开发者_如何学Goto transform a simple XML file via XSLT. The result should be the original XML input file plus a generated String. The string is generated out of the value from the XML file.
Generating is easy, but how do i insert the complete original XML content in my output xml?
Here a complete copy of a XML with XSLT 1.0. Change your output encoding (it is UTF-8 in this sample) and your need for indent (=yes) as you like.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- Default: copy everything -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
And if you want to add one line of text at the end use this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- add a line of text at the end of the xml -->
<xsl:template match="/">
<xsl:apply-templates/>
<xsl:text>your line of text</xsl:text>
</xsl:template>
<!-- Default: copy everything -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Keep in mind that the last solution (with the text line) does not create a valid XML!
You probably want to use the copy-of
element either on the base node or the xml file directly.
For example,
<xsl:copy-of select="document('style.xml')/"/>
精彩评论