user6440081
user6440081

Reputation:

XSLT 1.0: ignore all unmatched elements of template

how can I prevent the default behaviour of XSLT to output all elements? Simply said, I want to ignore all elements which are not matched by my template.

I have the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<SDHttpMessage>
<Headers>
    <Parameter name="Type">text/xml;charset=UTF-8</Parameter>
</Headers>
<Charset>UTF-8</Charset>
<Message>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
        <soapenv:Body>
            <Content>
                <MyContent>
                   <A>Text A</A>
                   <B>Text B</B>
                   <C>Text C</C>
                </MyContent>
            </Content>
        </soapenv:Body>
    </soapenv:Envelope>
</Message>

And the following XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            exclude-result-prefixes="soapenv xsd xsi">

<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*" />

<xsl:template match="Content/MyContent">
   <A><xsl:value-of select="A" /></A>
   <B><xsl:value-of select="B" /></B>
   <C><xsl:value-of select="C" /></C>
</xsl:template>

Desired output:

<A>Text A</A>
<B>Text B</B>
<C>Text C</C>

Actual output:

text/xml;charset=UTF-8UTF-8
<A>Text A</A>
<B>Text B</B>
<C>Text C</C>

I was thinking to simply call my template inside a root-element template:

<xsl:template match="/">
    <xsl:call-template name="callMyTemplate" />
</xsl:template>

<xsl:template match="Content/MyContent" name="callMyTemplate">
   <A><xsl:value-of select="A" /></A>
   <B><xsl:value-of select="B" /></B>
   <C><xsl:value-of select="C" /></C>
</xsl:template>

But it does not match any of the elements then.

So what would be the best approach if I just want to ignore all unmatched elements?

Thanks in advance.

Upvotes: 3

Views: 2287

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117018

You could add a template to bypass the other branches:

<xsl:template match="/SDHttpMessage">
    <xsl:apply-templates select="Message/soapenv:Envelope/soapenv:Body/Content/MyContent"/>
</xsl:template>

or override the built-in-in template:

<xsl:template match="text()"/>

Or you could do simply:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
exclude-result-prefixes="soapenv">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*" />

<xsl:template match="/SDHttpMessage">
    <xsl:for-each select="Message/soapenv:Envelope/soapenv:Body/Content/MyContent/*">
        <xsl:element name="{name()}">
            <xsl:value-of select="." />
        </xsl:element>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

Upvotes: 4

Related Questions