From: sandhya on 30 Nov 2009 05:35 Hi all, I have got to parse a xml file which is always in a defined schema.So this always comes with keyword "xmlns" at starting I have something like <SCL xmlns="aaa" xmlns:xsi="bbb" xsi:schemaLocation="ccc" <foo> <bar>trial</bar> </foo> </SCL> when I do set nodeList [$root selectNodes //SCL ] I get empty in variable "nodelist" Incase I change "<SCL xmlns="aaa" xmlns:xsi="bbb" xsi:schemaLocation="ccc"" to "<SCL>" then I get the root node address in variable nodeList. Is there a solution for this keyword problem "xmlns"
From: Donal K. Fellows on 30 Nov 2009 06:57 On 30 Nov, 10:35, sandhya <sandhyasap...(a)gmail.com> wrote: > Is there a solution for this keyword problem "xmlns" Your XPath query is without XML namespaces, so it can't match against a namespace'd document. A fairly good explanation of what is going on is: http://msdn.microsoft.com/en-us/library/ms950779.aspx There's a few ways to do deal with this problem. The easiest is with the -namespaces option to the selectNodes method. This lets you give a dictionary that specifies the mapping of prefixes to namespaces used _in the XPath expression_ (it does not need to be the one used in the document!) set nodes [$root selectNodes \ -namespaces {example http://aaa/whatever} \ //example:SCL] This is one of the ugliest things about using XPath: that there's no way to put the mapping of prefixes to URLs in the query itself. Blame the W3C. Donal.
From: sandhya on 30 Nov 2009 07:46 On Nov 30, 4:57 pm, "Donal K. Fellows" <donal.k.fell...(a)manchester.ac.uk> wrote: > On 30 Nov, 10:35, sandhya <sandhyasap...(a)gmail.com> wrote: > > > Is there a solution for this keyword problem "xmlns" > > Your XPath query is without XML namespaces, so it can't match against > a namespace'd document. A fairly good explanation of what is going on > is: > > http://msdn.microsoft.com/en-us/library/ms950779.aspx > > There's a few ways to do deal with this problem. The easiest is with > the -namespaces option to the selectNodes method. This lets you give a > dictionary that specifies the mapping of prefixes to namespaces used > _in the XPath expression_ (it does not need to be the one used in the > document!) > > set nodes [$root selectNodes \ > -namespaces {examplehttp://aaa/whatever} \ > //example:SCL] > > This is one of the ugliest things about using XPath: that there's no > way to put the mapping of prefixes to URLs in the query itself. Blame > the W3C. > > Donal. Thanks a lot ... though you say its ugliest way of doing it it worked for me.
From: Rolf Ade on 3 Dec 2009 08:47 In article <216d4a8e-d4de-47a1-a3d1-7c00d7796fe6(a)g27g2000yqn.googlegroups.com>, Donal K. Fellows <donal.k.fellows(a)manchester.ac.uk> wrote: >On 30 Nov, 10:35, sandhya <sandhyasap...(a)gmail.com> wrote: >> Is there a solution for this keyword problem "xmlns" > >Your XPath query is without XML namespaces, so it can't match against >a namespace'd document. A fairly good explanation of what is going on >is: > > http://msdn.microsoft.com/en-us/library/ms950779.aspx > >There's a few ways to do deal with this problem. The easiest is with >the -namespaces option to the selectNodes method. This lets you give a >dictionary that specifies the mapping of prefixes to namespaces used >_in the XPath expression_ (it does not need to be the one used in the >document!) > > set nodes [$root selectNodes \ > -namespaces {example http://aaa/whatever} \ > //example:SCL] While Donal is right, lemme add, that you are able to set up the prefix / namespace mapping used in selectNodes XPath expression once for the whole document with $doc selectNodesNamespaces { prefix1 http://aaa/whatever another http://bbb/whatever } After doing this, just use the defined prefixes in your XPath queries without further need to use the -namespaces options: set nodes [$root selectNodes //example:SCL] The resolution rule is: First, use the prefix namespace mapping out of the -namespaces option, if one is there. If there isn't a -namespaces option or the prefix was not found there look at what is set globally by the selectNodesNamespaces method. If the prefix still isn't found, look at the namespace declarations out of the XML Document in scope for the node and use that to resolve the prefix. Though, with that, you will not be able to select any namespaced node, if that node is namespaced not because of a prefix but of a default namespace in the XML document. And if another document uses another prefix for the same namespace, it will fail too. rolf
From: tom.rmadilo on 3 Dec 2009 11:04 On Dec 3, 5:47 am, points...(a)gmx.net (Rolf Ade) wrote: > While Donal is right, lemme add, that you are able to set up the > prefix / namespace mapping used in selectNodes XPath expression once > for the whole document with > > $doc selectNodesNamespaces { > prefix1http://aaa/whatever > anotherhttp://bbb/whatever > > } > > After doing this, just use the defined prefixes in your XPath queries > without further need to use the -namespaces options: > If the prefix still isn't found, look at the namespace declarations > out of the XML Document in scope for the node and use that to resolve > the prefix. Though, with that, you will not be able to select any > namespaced node, if that node is namespaced not because of a prefix > but of a default namespace in the XML document. And if another > document uses another prefix for the same namespace, it will fail too. Rolf, So how do you set the default namespace? Is it just { "" my:default:namespace} in the selectNodesNamespaces list? Also, does this work with the xslt stuff? We had a question several weeks ago on how to deal with namespaced nodes using the default namespace.
|
Next
|
Last
Pages: 1 2 Prev: Subtle change in 8.5 or is it tclkit ? Next: Upgrading from 8.4 to 8.5 |