Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML 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 November 30th, 2004, 01:54 PM
Authorized User
 
Join Date: Jun 2004
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default XPath - Selecting nodes based on attribute values

Hi,

My problem exists when the attribute is prefixed with a namespace.

For example, consider the following node saved in a file called test.xml.

<test xmlns="http://www.testsite.ca" xmlns:abc="http://www.abc.ca/test" identifier="XYZ"></test>

Now consider the following code (using VB 6.0 and MSXML 4.0)

Code:
'*********START**************
Set XmlLoadDocument = New MSXML2.DOMDocument40
'Set namespace properties
Call XmlLoadDocument.SetProperty("SelectionNamespaces", "xmlns:testNS='http://www.testsite.ca'" )
'Load test.xml into a XML document called test
Call XmlLoadDocument.Load(test.xml) 
'Use XPath to retrieve the node
Set tempNode = XmlLoadDocument.selectSingleNode("testNS:test[@testNS:abc = 'http://www.abc.ca/test']")
'*************END*************
The node tempNode is null (Nothing). No nodes are returned.

I have also tried executing,

Code:
Set tempNode = XmlLoadDocument.selectSingleNode("testNS:test[@abc = 'http://www.abc.ca/test']")
and

Code:
Set tempNode = XmlLoadDocument.selectSingleNode("testNS:test[@* = 'http://www.abc.ca/test']")
none of which return any nodes.

However, if I execute

Code:
Set tempNode = XmlLoadDocument.selectSingleNode("testNS:test[@identifier = 'XYZ']")
then I get the proper results. A node is returned.

This leads me to believe that the problem exists when attributes are prefixed with a namespace.

Anyone know how to get past this problem????

 
Old November 30th, 2004, 08:31 PM
Authorized User
 
Join Date: Nov 2004
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to jkmyoung
Default

The problem is that anything prefixed "xmlns:" is seen as a namespace declaration, not an actual attribute to be processed.

From www.w3c.org http://www.w3.org/TR/1999/REC-xslt-1...#built-in-rule
"The built-in template rule for namespace nodes is also to do nothing. There is no pattern that can match a namespace node; so, the built-in template rule is the only template rule that is applied for namespace nodes."
So you really can't reference the namespace declaration at all.

 
Old December 1st, 2004, 07:30 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

xmlns:abc="http://www.abc.ca/test" is a namespace declaration, it doesn't affect the test element at all in your example. To select the test element you need, after setting the Selectionnamespaces as you have done:
Code:
Set tempNode = XmlLoadDocument.selectSingleNode("testNS:test")
There are no prefixed attributes in your example, if "identifier" were prefixed, i.e.
Code:
<test xmlns="http://www.testsite.ca" abc:identifier="XYZ" xmlns:abc="http://www.abc.ca/test"></test>
then you could do:
Code:
Set tempNode = XmlLoadDocument.selectSingleNode("testNS:test[abc:identifier = 'xyz']")
In that case you would have to declare two namespace associations:
Code:
Call XmlLoadDocument.SetProperty("SelectionNamespaces", "xmlns:testNS='http://www.testsite.ca' xmlns:abc='http://www.abc.ca/test'" )


--

Joe (Microsoft MVP - XML)
 
Old December 1st, 2004, 05:54 PM
Authorized User
 
Join Date: Jun 2004
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What I really want to do is check if a namespace exists in the <test> tag. That is,

Given the xml:

Code:
<test xmlns="http://www.testsite.ca" xmlns:abc="http://www.abc.ca/test" identifier="XYZ"></test>
if namespace "http://www.abc.ca/test" exists then do something.

 
Old December 1st, 2004, 06:12 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Use the namespace axis.

xsl:if test="namespace::*[. = 'http://www.abc.ca/test']"


Michael Kay
http://www.saxonica.com/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Xpath - Selecting attributes bonekrusher XSLT 3 October 1st, 2008 12:27 PM
selecting nodes that lack certain attributes rmers XSLT 3 October 4th, 2006 03:44 PM
Selecting nodes of various namespace spencer.clark XSLT 8 August 5th, 2005 07:53 AM
Rules based on attribute values... jacob XML 2 July 6th, 2005 01:50 PM
XPath not finding nodes, need help Alderian72 XSLT 1 June 16th, 2005 10:49 AM





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