Skip to Content

two xslt questions

Hi Gurus

i am starting to learn xslt mapping and the xslt language. here i have two questions on it:

1. if there are multiple "<xsl:template match=" in a program, how are they processed? what i was told was the second template will override the first one. however when i changed the sequence of template in stylus studio, the result was the same. for example:

the source xml is:

<country xmlns="http://india.com/states" version="1.0">

<state>

<city>

<name>

<namefirst>XU</namefirst>

</name>

<name1>Sandy</name1>

</city>

</state>

<state2>

<city2>

<name2>MUMBAI</name2>

</city2>

</state2>

</country>

while this is the first version of the xslt :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:x="http://india.com/states">

<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="*">

<xsl:element name="{name()}">

<xsl:apply-templates/>

</xsl:element>

</xsl:template>

<xsl:template match="*[ancestor-or-self::x:city]">

<xsl:apply-templates/>

</xsl:template>

</xsl:stylesheet>

from my understanding, the first template will copy all the element nodes of the source, while the second one will erase any nodes who have CITY as one of the ancestor nodes or who is CITY node itself. This part will override the same segment from the first template.

the result is:

<country>

<state>

XU

Sandy

</state>

<state2>

<city2>

<name2>MUMBAI</name2>

</city2>

</state2>

</country>

however when the sequence has been changed like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:x="http://india.com/states">

<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="*[ancestor-or-self::x:city]">

<xsl:apply-templates/>

</xsl:template>

<xsl:template match="*">

<xsl:element name="{name()}">

<xsl:apply-templates/>

</xsl:element>

</xsl:template>

</xsl:stylesheet>

the result is the same. if the second template will override the first one, the result of this one should be the copy of ALL nodes from the source xml, because the erased nodes in the first template here has been copied by the second template here.

if it is not the override simply what is the working process behind?

2. just to confirm that

<xsl:template match="*[ancestor-or-self::x:city]"> will get all the nodes who has an 'city' node as one of the ancestor element or the 'city' node itself. it does not match any node who is the ancestor of 'city' node or the 'city' node itself.

one of the evidences is when the following code was run in stylus studio:

<xsl:template match="*[ancestor-or-self::x:city]">

<xsl:element name="{name()}">

<xsl:apply-templates/>

</xsl:element>

</xsl:template>

just the nodes from 'city' and below were chosen:

<city>

<name>

<namefirst>XU</namefirst>

</name>

<name1>Sandy</name1>

</city>

thanks

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Jan 30, 2013 at 02:37 PM
    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Feb 01, 2013 at 04:20 AM

    reason has been found. if the priority attribute of template has been specified as a same value, the templates will be called/applied one by one. the latter one will override the former one if the same element has been processed by multiple templates.

    however, if this 'priority' attribute has not been specify, the proecess sequence is quite strange. so far i donot know it.

    @Maximiliano,

    thanks for your apply however it is too generic, i'd only set it as an helpful answer instead of the correct one for the convenience of up coming readers.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.