Problem with XML output

Here should go questions about transforming XML with XSLT and FOP.
jmarks@salo.com
Posts: 3
Joined: Wed Mar 31, 2010 9:59 pm

Problem with XML output

Post by jmarks@salo.com »

I'm using XSLT to transform an XML document into a different XML document. The problem is that the transform seems to output stuff from the original document whether I want it in the new document or not.

Should I be using text output?

I hope this isn't a duplicate, because I just tried to submit this problem a little while ago
Radu
Posts: 9472
Joined: Fri Jul 09, 2004 5:18 pm

Re: Problem with XML output

Post by Radu »

Hi John,

The idea is that when an XSLT stylesheet gets transformed some default builtin templates are automatically added to it (if templates with the same match are not found in the original XSL). One of those templates for example outputs the text nodes from the XML document.

Have a look at the default templates:
http://www.w3.org/TR/xslt#built-in-rule

You can also use our debugger (which has backmapping from the output XML to the stylesheet) to see from where the XML tags are emitted.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
jmarks@salo.com
Posts: 3
Joined: Wed Mar 31, 2010 9:59 pm

Re: Problem with XML output

Post by jmarks@salo.com »

I saw which nodes the default templates process in the debugger, but have been unable to solve the problem of unwanted output. The following code is what I'm working with. It's a transform that turns XML describing a set of XML tables into a tree. I tried overriding built-in template rules (see below), but have been unsuccessful. I think I may be missing something fundamental about how this all works.

The processor complains about template ambiguity, which is understandable, but I really don't get what's happening.

Here's the XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match="/">
<root>
<xsl:apply-templates/>
</root>
</xsl:template>
<xsl:template match="text()|@*"/>
<xsl:template match="/root/customers/customer[@org='one']">
<xsl:element name="customer">
<xsl:attribute name="org"><xsl:value-of select="@org"/></xsl:attribute>
<xsl:call-template name="vlan">
<xsl:with-param name="customer" select="@org"/>
</xsl:call-template>
</xsl:element>
</xsl:template>
<xsl:template name="vlan">
<xsl:param name="customer"/>
<xsl:for-each select="/root/vlans/vlan[$customer='one']">
<xsl:element name="vlan">
<xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

and the XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<customers>
<customer org="one"/>
<customer org="two"/>
</customers>
<servers>
<server name="svr1" customer="one"/>
<server name="svr2" customer="one"/>
<server name="svr3" customer="one"/>
</servers>
<ipaddresses>
<ipaddress addr="1.1.1.1" server="svr1"/>
<ipaddress addr="1.1.1.2" server="svr1"/>
<ipaddress addr="1.1.1.3" server="svr2"/>
<ipaddress addr="1.1.1.4" server="svr2"/>
<ipaddress addr="1.1.1.5" server="svr3"/>
<ipaddress addr="1.1.1.6" server="svr3"/>
</ipaddresses>
<vlans>
<vlan name="vlan1" customer="one"/>
<vlan name="vlan2" customer="one"/>
</vlans>
<subnets>
<subnet mask="176.255.255.0" vlan="vlan1"/>
<subnet mask="192.255.255.0" vlan="vlan2"/>
</subnets>
</root>

It sti
Radu
Posts: 9472
Joined: Fri Jul 09, 2004 5:18 pm

Re: Problem with XML output

Post by Radu »

Hi,

And what is the wanted XML output?

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
jmarks@salo.com
Posts: 3
Joined: Wed Mar 31, 2010 9:59 pm

Re: Problem with XML output

Post by jmarks@salo.com »

I managed to solve the problem by using <xsl:call-template> with a named template instead of <xsl:apply-templates>. Thanks for your help on the built-in templates. I'll be sure to be a lot more careful when I use them in the future.
Post Reply