tags:

views:

57

answers:

3

Hello,

I'm using XSLT to manipulate the data inside of an XML file (I'm taking the guts of one XML file and putting them into a new shell in another XML file). I'm only using some of the data in the first file, and the data from the parts that I don't use is being concatenated to the end of the new file. How do I keep XSLT from doing this?

Thanks!

EDIT: Here's some pseudocode, I can't post the actual code:

<xsl:output method="xml"/>
<xsl:template match="foo">
    <xsl:element name="bar">
        <!--... makes elements and traverses some of the other file ...-->
    </xsl:element>
</xsl:template>

And the output:

    <foo>
        <bar>
            <!-- ... -->
        </bar>
    </foo>

    <!-- junk at the end of the file that matches up with the content of the unused data tags -->

    0

    N
    N
    Y
    00000148
    ASDF
+4  A: 

XSLT includes certain default templates that are invoked when you do not write one to override it. For example, if you do not include a template matching the root element, it will do this:

<xsl:template match="/">
    <xsl:apply-templates/>
</xsl:template>

There is also a default template that will emit the text content of matched elements. This is probably the "junk" you're seeing in your output.

My guess is that you need to prevent this by including a root template and matching explicitly those elements that you're interested in.

harpo
+ Great explanation!
adam_0
+3  A: 

XSLT has a default root match pattern. To replace it, try something like this in your XSLT:

<xsl:template match="/">
  <xsl:apply-templates select="foo"/>
</xsl:template>    
<xsl:template match="foo">
    <xsl:element name="bar">
        <!--... makes elements and traverses some of the other file ...-->
    </xsl:element>
</xsl:template>
Jacob
+ Great example, thanks!
adam_0
@Jacob: I think you should add to your answer that the junk is the result of built-in template for text nodes, like harpo's answer.
Alejandro
A: 

Only to be complete - you do not need to match the root element if you don't want this. The other way to prevent using the built-in default template is to define an empty template that collects all the rest at the end of the XSLT file like this

<xsl:template match="*" />  
Andreas