开发者

XSLT dumping junk at the end of my file

开发者 https://www.devze.com 2023-01-06 05:26 出处:网络
I\'m using XSLT to ma开发者_运维百科nipulate 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 da

I'm using XSLT to ma开发者_运维百科nipulate 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


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.


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>


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="*" />  
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号