user3734575
user3734575

Reputation:

Apply templates is not working as expected

I am trying to implement one XSL logic using template. For all Test element, I need to add one parent tag "Result"

<xsl:stylesheet exclude-result-prefixes="xs" version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:template match="Test">
    <RESULT>
        <xsl:copy-of select="."/>
    </RESULT>
    <xsl:apply-templates select="node()"/>
</xsl:template>
</xsl:stylesheet>

Input XML :

<?xml version="1.0" encoding="UTF-8"?>
<Test>
<goal>
    Books
</goal>
<secret>
    <Test>
        Noodles
    </Test>        
</secret>
</Test>

Expected output :

<Output>
<RESULT>
  <Test>
     <goal>Books</goal>
     <secret>
        <Test>Noodles</Test>
     </secret>
  </Test>
</RESULT>
<RESULT>
  <Test>Noodles</Test>
</RESULT>
</Output>

Actual Output :

<Output>
<RESULT>
  <Test>
     <goal>Books</goal>
     <secret>
        <Test>Noodles</Test>
     </secret>
  </Test>
</RESULT>
Books
<RESULT>
  <Test>Noodles</Test>
</RESULT>
Noodles
</Output>

I am getting one extra text in the output. Any help?

Upvotes: 0

Views: 79

Answers (1)

Daniel Haley
Daniel Haley

Reputation: 52888

You're getting the extra text output because of XSLT's built in rules.

You can override the built in rule for text by adding this template:

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

This will work fine if you're just using xsl:copy-of and/or xsl:value-of. If you're counting on the built in rules for text() output, you'll have to modify the override or change the select in your xsl:apply-templates (one option has already been given by Sivaa Nethaji).

Upvotes: 1

Related Questions