svz
svz

Reputation: 4588

XSL transformation going wild

Something wrong is going on with my xsl transformation and I can't see the problem (yeah, newbie).

I have an xml:

<REG_REQUEST>
    <header>
        <version/>
        <!-- many tags here -->

        <ref_dtls>
            <ref_doc_num>000111222</ref_doc_num>
            <ref_doc_date>01.01.2000</ref_doc_date>
            <ref_name>Hello world!</ref_name>
        </ref_dtls>
    </header>

    <general_info>
        <dfiState>AGRM</dfiState>
    </general_info>

    <add_info>some additional info here</add_info>
    <!-- many more tags here -->
</REG_REQUEST>

and an xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">  
    <xsl:output method="html" encoding="utf-8"/>

    <xsl:template match="header">
         <!-- template contents -->
    </xsl:template>
</xsl:stylesheet>

The template itself prints fine but after that it also prints a list of all the values of xml attributes. The template contains nothing more than some html code output. I can't figure out why this is happening.

Upvotes: 1

Views: 65

Answers (1)

Tim C
Tim C

Reputation: 70598

This is because of in-built templates which are used when a specific template is not provided. These are as follows:

  • For an element, process its node children
  • For a text node, give its value

You have not provided a template for the REG_REQUEST and so the built-in template will process its children. You've got a template for header and so that will work as expected, but no template for the other children of REG_REQUEST. This means the default behviour kicks in, and ultimately text values of elements and attributes get output.

The solution is to add a template to match the other children of REG_REQUEST and just ignore them, so that no further processing is done for the elements

<xsl:template match="REG_REQUEST/*[not(self::header)]" />

This will cause all the other elements, other than header to be ignored.

Upvotes: 4

Related Questions