Project

Profile

Help

xpath question

Added by Anonymous over 19 years ago

Legacy ID: #3006102 Legacy Poster: Jozef (xml4pharma)

Dear, I have the following XML instance: <?xml version="1.0" encoding="UTF-8"?> <A xmlns="http://www.myns.org/ns/myns/v1.1" at1="1" at2="2"> <B atb="3"/> <C atc="4"/> <D> <E/> </D> </A> I need to fiind an XPath expression to select those <E> nodes of which the <C> node has an attribute value of '4' for the @C attribute. It is somewhat complicater than usual as the XML has a default namespace. I got sofar: /[name()='A'][name(child::)='B']/[name()='D']/[name()='E'] selects the E node if the first child of A is B. The first problem I encounter is that name(child::) only seems to select the first child of A, so /[name()='A'][name(child::)='C']/[name()='D']/*[name()='E'] gives no hits. Secondly I do not see yet how I have to give the condition for the @atc attribute. I am testing using Exchanger XML which is based upon Saxon. In my real application I use Saxon to do the XPath query. Many thanks in advance, Jozef


Replies (2)

RE: xpath question - Added by Anonymous over 19 years ago

Legacy ID: #3006132 Legacy Poster: Michael Kay (mhkay)

It's a little bit difficult to answer this because you've only shown one instance document, and you haven't indicated how much the real documents can vary from this. You say "those E nodes", so presumably there may be more than one: but if there were others, where would they appear, and how would they be related to the C nodes containing the key value? Also, it's not clear what your constraints are. Are you unable to declare any namespaces in the static context for the XPath expression? Are you using XPath 1.0 or 2.0? Also, there are parts of the XPath expressions you show that don't seem related to any part of your stated requirements. Let's first try a guess at a conventional XPath to solve your problem: something like: /n:A[n:B][n:C[@atc='4']]/n:D/n:E As I say, this is just a guess because your requirements aren't clear. If you can't use namespace prefixes, this translates to /[local-name()='A' and namespace-uri()='uuuu'] [[local-name()='B' and namespace-uri()='uuuu']] [[local-name()='C' and namespace-uri()='uuuu'] [@atc='4']] /[local-name()='D' and namespace-uri()='uuuu'] /*[local-name()='E' and namespace-uri()='uuuu'] Michael Kay P.S.: questions about XSLT and XPath coding problems, if they aren't specific to Saxon, are best asked on the xsl-list at www.mulberrytech.com

RE: xpath question - Added by Anonymous over 19 years ago

Legacy ID: #3006316 Legacy Poster: Jozef (xml4pharma)

Dear Michael, Many thanks, especially for answering this on a Sunday ! Your second suggestion, using the local-name indeed solves it. I cannot use prefixes as the original document hasn't any (default namespacing). For clarity (and others reading this): <C> and <D> are always children of <A>, and there is only 1 <B> and 1 <C>. <B> and <C> come always in that order and before <D>. Yes, I can use XPath2. The XPath expression is formed dynamically in a Java program, so I can check for the namespace and add the namespace-uri() when necessary. Also many thank for the link to the mulberry list. If I have further questions, I will first look there and if necessary post there. Best regards Jozef

    (1-2/2)

    Please register to reply