Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 26th, 2010, 07:17 PM
Registered User
 
Join Date: May 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Red face template match

I am somewhat of an xslt newbie and I am sure that it is something very simple. The template match on the root element is not firing. I suspect it has something to do with the default namespace (xmlns="http://www.comp.com/94x/app") for the document as when I remove it the template fires.


The input.xml document:

<?xml version="1.0" encoding="US-ASCII"?>
<WORKORDER xmlns="http://www.comp.com/94x/app" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns0="http://www.w3.org/2001/XMLSchema-instance" ns0:schemaLocation="http://www.comp.com/94x/app ../94x/EFTUunisysLocal.xsd">
<WO-DATE/>
<WO-TIME/>
<SYSTEM-ID>ms00164b.comp.com</SYSTEM-ID>
<RUN-ID>unisys_eod</RUN-ID>
<SUBSYSTEM-ID>94x</SUBSYSTEM-ID>
<GFN>unisys_eod0526</GFN>
<CAT-DATE>20100526</CAT-DATE>
<CAT-TIME>162028</CAT-TIME>
</WORKORDER>


The xsl:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.comp.com/94x/app">
<xsl:output method="xml" version="1.0" encoding="US-ASCII" indent="yes"/>

<xsl:template match="WORKORDER">
workOrder
<xsl:apply-templates select="@*|node()"/>
<xsl:value-of select="namespace-uri()"></xsl:value-of>
<xsl:value-of select="local-name()"></xsl:value-of>
</xsl:template>

</xsl:stylesheet>


This is the resultant output from the above:
<?xml version="1.0" encoding="US-ASCII"?>



ms00164b.comp.com
unisys_eod
94x
unisys_eod0526
20100526
162028
 
Old May 27th, 2010, 03:46 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Try with the below
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.comp.com/94x/app">
<xsl:output method="xml" version="1.0" encoding="US-ASCII" indent="yes"/>
<xsl:template match="/">
workOrder
<xsl:apply-templates select="*[name(.) = 'WORKORDER']" mode="test"/>
</xsl:template>
 
<xsl:template match="*" mode="test">
<xsl:apply-templates select="@*|node()"/>
<xsl:value-of select="namespace-uri()"/>
<xsl:value-of select="local-name()"/>
</xsl:template>
</xsl:stylesheet>
__________________
Rummy
 
Old May 27th, 2010, 03:56 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Read all about matching nodes in namespaces here.

I don't think much of mrame's solution, which tries to ignore the fact that there is a namespace. Namespaces are used for a reason, and ignoring them is the wrong approach. Just declare a prefix and use it wherever you refer to elements in this namespace:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:w="http://www.comp.com/94x/app">
<xsl:output method="xml" version="1.0" encoding="US-ASCII" indent="yes"/>

<xsl:template match="w:WORKORDER">
workOrder
<xsl:apply-templates select="@*|node()"/>
<xsl:value-of select="namespace-uri()"></xsl:value-of>
<xsl:value-of select="local-name()"></xsl:value-of>
</xsl:template>

</xsl:stylesheet>

Alternatively, in XSLT 2.0 you can set default-xpath-namespace="http://www.comp.com/94x/app" and then unprefixed names in XPath expressions and match patterns will be taken as referring to this namespace.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old May 27th, 2010, 04:01 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Mike you are right, that was a mistake. Actually I tried declaring a prefix, but did a mistake by declaring it with a different namespace and not with http://www.comp.com/94x/app. Thanks again for pointing out.
__________________
Rummy





Similar Threads
Thread Thread Starter Forum Replies Last Post
Param in template match=" " iceandrews XSLT 2 May 7th, 2008 07:37 AM
value-of inside template match RoeZ XSLT 5 April 17th, 2008 12:09 PM
template match doesnt match the required node Tomi XSLT 2 March 12th, 2007 06:24 AM
template m,atch doesnt match Tomi XSLT 1 March 9th, 2007 07:56 AM
help with xsl template match enT XSLT 9 September 24th, 2003 06:21 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.