简体   繁体   中英

How to add a new line to document by XSLT?

I have to 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')/"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM