Rupesh_Kr
Rupesh_Kr

Reputation: 3435

Change in `<Output>' element with if condition

I have two input files like:

a.xml

<a>
   <data>...</data>
</a>

b.xml

<b>
   <data>...</data>
</b>

my xslt

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

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

I want to check first root element, if it is <a> than <xsl:output> is

<xsl:output method="xml" encoding="UTF-8" indent="yes"/>

else

<xsl:output method="xml" encoding="us-ascii" indent="no"/>

Thanks in advance.

Upvotes: 0

Views: 39

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167611

Well, XSLT is XML so you can of course use XSLT to create another XSLT with the desired output encoding and you can then run it in a separate step.

As an alternative you could check the root element not being an a element and then delegate outputting to xsl:result-document where you can change the encoding:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="document-node()[*[not(self::a)]]">
        <xsl:result-document encoding="US-ASCII">
            <xsl:apply-templates/>
        </xsl:result-document>
    </xsl:template>

</xsl:stylesheet>

http://xsltfiddle.liberty-development.net/3Nqn5Y8

Upvotes: 1

Related Questions