IMPORTANT:
this is not a Support Forum! Experienced users might answer from time to time questions posted here. If you need a professional and reliable answer, or if you want to report a bug, please contact Altova Support instead.

xsl:template: Output contains all elements Options · View
feiyang
Posted: Wednesday, October 14, 2009 12:24:47 AM
Rank: Newbie

Joined: 6/24/2008
Posts: 9
Hello,

currently I am stuck with the following strange behavior of an XSL Transformation in XMLSpy. I use a simplified example to illustrate the issue. When using the template statement I would expect that only the value of the subelement <lastName> is used as output. However the values for "FirstElement" and "SecondElement" are written to the output too (outside of an element tag, marked bold in the output structure).
My real XML file contains more than 100 elements on subnode level and I want to avoid typing the whole path for each select statement, so I would like to use the template statement to set the context to a lower level. Obviously this does not work as expected. When debugging I have seen that a "built in template" is used too. Could this be the reason? How can I avoid that behavior? Thank you very much for your advice!


Source XML:

<exampleXML>
<name>
<firstName>myFirstName</firstName>
<lastName>myLastName</lastName>
</name>
<otherElements>
<Element1>FirstElement</Element1>
<Element2>SecondElement</Element2>
</otherElements>
</exampleXML>

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/exampleXML/name">
<output><xsl:value-of select="firstName"/></output>
</xsl:template>
</xsl:stylesheet>

Output:

<?xml version="1.0" encoding="UTF-8"?><output>myFirstName</output>FirstElementSecondElement
rip
Posted: Wednesday, October 14, 2009 4:04:49 AM
Rank: Advanced Member

Joined: 7/17/2008
Posts: 185
Location: Minutiae, Triviality
Add this to your stylesheet, for example between the last <xsl:template> and </xsl:stylesheet>

Code:

  <xsl:template match="/">
    <xsl:apply-templates select="/exampleXML/name"/>
  </xsl:template>[/b]


XMLSpy XSLT has a built-in library of standard "what to do when" templates. By not supplying a what to do when, the default template is run instead. The behavior of that template is to dump the data into the result document. If you use the XSLT debugger and step through your stylesheet, you'll see this happening.

Code:

<?xml version="1.0" encoding="UTF-8"?>
<output>myFirstName</output>
feiyang
Posted: Wednesday, October 14, 2009 5:55:21 PM
Rank: Newbie

Joined: 6/24/2008
Posts: 9
Great! Thank you very much for that helpful advise which actually solved my issue.
Is this normal behavior of an XSLT processor or just a special feature of XMLSpy when running XSLT stylesheets? Is there any way to deactivate those built in templates?
Martin Honnen
Posted: Thursday, October 15, 2009 10:12:43 AM
Rank: Advanced Member

Joined: 6/10/2007
Posts: 36
The built-in templates are specified in the XSLT specification: http://www.w3.org/TR/xslt/#built-in-rule

So what you experienced is the normal behaviour of an XSLT processor, not something specific to Altova's XSLT processor.

The only way to deactivate those templates is to override them with your own templates as those will have precedence.
Users browsing this topic
guest

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Use of the Altova User Forum(s) is governed by the Altova Terms of Use.