You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by sebb <se...@gmail.com> on 2007/09/03 16:14:51 UTC

Using XPath with namespace defined in child element

I'm using xalan-j 2.7.0; testing using the ApplyXPathDOM sample code.
====

Sample XML document:

===
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope
 xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
 <soapenv:Body
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ns:registerOfferResponse  xmlns:ns="http://biz.aol.com/schema/2006-12-18">
   <ns:result>0</ns:result>
  </ns:registerOfferResponse>
 </soapenv:Body>
</soapenv:Envelope>
===

Using the XPath

//ns:result

generates the error:

"Prefix must resolve to a namespace: ns"

If I move the declaration to the top-level element in the xml document, as in

===
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:ns="http://biz.aol.com/schema/2006-12-18"
 xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
 <soapenv:Body
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ns:registerOfferResponse>
   <ns:result>0</ns:result>
  </ns:registerOfferResponse>
 </soapenv:Body>
</soapenv:Envelope>
===

then the XPath //ns:result works fine.

I get the same error as before if "ns" is declared in the Body element.

It apears that the XPath processing requires the namespace to be
defined only in the top-level element, whereas AIUI XML allows the
namespace to be defined on first use of an namespace.

Is this correct?

If so, is there any way to tell xalan to use the namespace
declarations from child elements?

BTW, I have seen a work-round, which is to use the query:

//*[local-name()='result' and
namespace-uri()='http://biz.aol.com/schema/2006-12-18' ]

but that is really messy.

S///

Re: Using XPath with namespace defined in child element

Posted by sebb <se...@gmail.com>.
On 03/09/07, Mukul Gandhi <ga...@gmail.com> wrote:
> Sorry, I misunderstood your question. Now I can see, there is a sample
> ApplyXPathDOM.java bundled with Xalan-J 2.7.0.
>
> This sample tests the DOM L3 XPath API.

Yes

> I can see, that when the XML document doesn't use namespaces, then
> this sample works fine, and is infact a very useful utility.
>
> In fact, I think, the sample works fine when XML document uses namespaces too.
>
> You are running the utility as, java ApplyXPathDOM file.xml //ns:result
>
> And you expect, that all the element nodes matching the XPath
> expression ns:result will be returned. But you can't get the expected
> result by simply specifying the XPath expression as, //ns:result.
> There is no namespace binding present for prefix ns:.

Yes, there is a binding, but in the first case it is a local binding
for only part of the xml document. If there was no binding then the
document would not be valid, surely?

> And you are
> getting the expected error from the utility. because there doesn't
> exist any context in this case, which supplies the namespace binding.
>
> The workaround you tried (and which works),
> //*[local-name()='result' and
> namespace-uri()='http://biz.aol.com/schema/2006-12-18' ]
>
> works, because the XPath expression is complete.
>
> I suggest, please use this workaround for your purpose.

As the expression has to be entered by the user, that is not going to
be popular.

> On 9/3/07, sebb <se...@gmail.com> wrote:
> > Thanks, but I'm not using a stylesheet,
> >
> > The application is JMeter - we have an XPath extractor which can be
> > used to extract items from an XML document sample - there is no
> > stylesheet involved, just the XML document and the XPath query.
> >
> > S///
> > On 03/09/07, Mukul Gandhi <ga...@gmail.com> wrote:
> > > You need to provide the namespace declaration,
> > > xmlns:ns="http://biz.aol.com/schema/2006-12-18" in the stylesheet
> > > also. Then it should work.
> > >
> > > For e.g., this stylesheet
> > >
> > > <?xml version="1.0"?>
> > > <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> > >                        xmlns:ns="http://biz.aol.com/schema/2006-12-18"
> > >                        version="1.0">
> > >
> > >  <xsl:output method="text" />
> > >
> > >  <xsl:template match="/">
> > >   <xsl:value-of select="count(//ns:result)" />
> > >  </xsl:template>
> > >
> > > </xsl:stylesheet>
> > >
> > > When applied to your source document, works fine.
> > >
> > > On 9/3/07, sebb <se...@gmail.com> wrote:
> > > > I'm using xalan-j 2.7.0; testing using the ApplyXPathDOM sample code.
> > > > ====
> > > >
> > > > Sample XML document:
> > > >
> > > > ===
> > > > <?xml version="1.0" encoding="utf-8"?>
> > > > <soapenv:Envelope
> > > >  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
> > > >  <soapenv:Body
> > > >  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> > > >  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
> > > >  <ns:registerOfferResponse  xmlns:ns="http://biz.aol.com/schema/2006-12-18">
> > > >   <ns:result>0</ns:result>
> > > >  </ns:registerOfferResponse>
> > > >  </soapenv:Body>
> > > > </soapenv:Envelope>
> > > > ===
> > > >
> > > > Using the XPath
> > > >
> > > > //ns:result
> > > >
> > > > generates the error:
> > > >
> > > > "Prefix must resolve to a namespace: ns"
> > > >
> > > > If I move the declaration to the top-level element in the xml document, as in
> > > >
> > > > ===
> > > > <?xml version="1.0" encoding="utf-8"?>
> > > > <soapenv:Envelope xmlns:ns="http://biz.aol.com/schema/2006-12-18"
> > > >  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
> > > >  <soapenv:Body
> > > >  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> > > >  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
> > > >  <ns:registerOfferResponse>
> > > >   <ns:result>0</ns:result>
> > > >  </ns:registerOfferResponse>
> > > >  </soapenv:Body>
> > > > </soapenv:Envelope>
> > > > ===
> > > >
> > > > then the XPath //ns:result works fine.
> > > >
> > > > I get the same error as before if "ns" is declared in the Body element.
> > > >
> > > > It apears that the XPath processing requires the namespace to be
> > > > defined only in the top-level element, whereas AIUI XML allows the
> > > > namespace to be defined on first use of an namespace.
> > > >
> > > > Is this correct?
> > > >
> > > > If so, is there any way to tell xalan to use the namespace
> > > > declarations from child elements?
> > > >
> > > > BTW, I have seen a work-round, which is to use the query:
> > > >
> > > > //*[local-name()='result' and
> > > > namespace-uri()='http://biz.aol.com/schema/2006-12-18' ]
> > > >
> > > > but that is really messy.
> > > >
> > > > S///
> > > >
> > >
> > >
> > > --
> > > Regards,
> > > Mukul Gandhi
> > >
> >
>
>
> --
> Regards,
> Mukul Gandhi
>

Re: Using XPath with namespace defined in child element

Posted by Mukul Gandhi <ga...@gmail.com>.
Sorry, I misunderstood your question. Now I can see, there is a sample
ApplyXPathDOM.java bundled with Xalan-J 2.7.0.

This sample tests the DOM L3 XPath API.

I can see, that when the XML document doesn't use namespaces, then
this sample works fine, and is infact a very useful utility.

In fact, I think, the sample works fine when XML document uses namespaces too.

You are running the utility as, java ApplyXPathDOM file.xml //ns:result

And you expect, that all the element nodes matching the XPath
expression ns:result will be returned. But you can't get the expected
result by simply specifying the XPath expression as, //ns:result.
There is no namespace binding present for prefix ns:. And you are
getting the expected error from the utility. because there doesn't
exist any context in this case, which supplies the namespace binding.

The workaround you tried (and which works),
//*[local-name()='result' and
namespace-uri()='http://biz.aol.com/schema/2006-12-18' ]

works, because the XPath expression is complete.

I suggest, please use this workaround for your purpose.

On 9/3/07, sebb <se...@gmail.com> wrote:
> Thanks, but I'm not using a stylesheet,
>
> The application is JMeter - we have an XPath extractor which can be
> used to extract items from an XML document sample - there is no
> stylesheet involved, just the XML document and the XPath query.
>
> S///
> On 03/09/07, Mukul Gandhi <ga...@gmail.com> wrote:
> > You need to provide the namespace declaration,
> > xmlns:ns="http://biz.aol.com/schema/2006-12-18" in the stylesheet
> > also. Then it should work.
> >
> > For e.g., this stylesheet
> >
> > <?xml version="1.0"?>
> > <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> >                        xmlns:ns="http://biz.aol.com/schema/2006-12-18"
> >                        version="1.0">
> >
> >  <xsl:output method="text" />
> >
> >  <xsl:template match="/">
> >   <xsl:value-of select="count(//ns:result)" />
> >  </xsl:template>
> >
> > </xsl:stylesheet>
> >
> > When applied to your source document, works fine.
> >
> > On 9/3/07, sebb <se...@gmail.com> wrote:
> > > I'm using xalan-j 2.7.0; testing using the ApplyXPathDOM sample code.
> > > ====
> > >
> > > Sample XML document:
> > >
> > > ===
> > > <?xml version="1.0" encoding="utf-8"?>
> > > <soapenv:Envelope
> > >  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
> > >  <soapenv:Body
> > >  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> > >  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
> > >  <ns:registerOfferResponse  xmlns:ns="http://biz.aol.com/schema/2006-12-18">
> > >   <ns:result>0</ns:result>
> > >  </ns:registerOfferResponse>
> > >  </soapenv:Body>
> > > </soapenv:Envelope>
> > > ===
> > >
> > > Using the XPath
> > >
> > > //ns:result
> > >
> > > generates the error:
> > >
> > > "Prefix must resolve to a namespace: ns"
> > >
> > > If I move the declaration to the top-level element in the xml document, as in
> > >
> > > ===
> > > <?xml version="1.0" encoding="utf-8"?>
> > > <soapenv:Envelope xmlns:ns="http://biz.aol.com/schema/2006-12-18"
> > >  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
> > >  <soapenv:Body
> > >  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> > >  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
> > >  <ns:registerOfferResponse>
> > >   <ns:result>0</ns:result>
> > >  </ns:registerOfferResponse>
> > >  </soapenv:Body>
> > > </soapenv:Envelope>
> > > ===
> > >
> > > then the XPath //ns:result works fine.
> > >
> > > I get the same error as before if "ns" is declared in the Body element.
> > >
> > > It apears that the XPath processing requires the namespace to be
> > > defined only in the top-level element, whereas AIUI XML allows the
> > > namespace to be defined on first use of an namespace.
> > >
> > > Is this correct?
> > >
> > > If so, is there any way to tell xalan to use the namespace
> > > declarations from child elements?
> > >
> > > BTW, I have seen a work-round, which is to use the query:
> > >
> > > //*[local-name()='result' and
> > > namespace-uri()='http://biz.aol.com/schema/2006-12-18' ]
> > >
> > > but that is really messy.
> > >
> > > S///
> > >
> >
> >
> > --
> > Regards,
> > Mukul Gandhi
> >
>


-- 
Regards,
Mukul Gandhi

Re: Using XPath with namespace defined in child element

Posted by mahesh rana <ma...@gmail.com>.
Hi,
I want to configure my Thread Properties through XML file, but every time
Jmeter throwing me error " XPathFileContainer has no nodes:
D:/apache-jmeter-2.6/apache-jmeter-2.6/bin/LSMSXML/LSMSparams.xml 
//project/thread[@name='PeopleTab']/rampuptime[@name='rampuptime']/@value "
even though node is there.
Below is my XML file:

<?xml version="1.0"?>
<project name="LSMS">
  <thread name="DataBaseTestData" value="2">
     <rampuptime name="rampuptime" value="150"/>
   <holdload name="holdload" value="3600"/>
  </thread> 
  <thread name="PeopleTab" value="1">
    <rampuptime name="rampuptime" value="150"/>
   <holdload name="holdload" value="3600"/>
  </thread>
</project>

Thanks in Advance!

sebb-2-2 wrote:
> 
> On 04/09/07, Florent Georges <li...@fgeorges.org> wrote:
>> sebb wrote:
>>
>> > On 04/09/07, Florent Georges wrote:
>>
>> > >    bindings:
>> > >      u1=uri 1
>> > >      u2=uri 2
>>
>> > >    expression:
>> > >      /u1:elem1/u2:elem2
>>
>> > > to get the deepest element.
>>
>> > OK, I now understand that XPath requires that the
>> > namespace must be (unambiguously) pre-defined; this is not
>> > possible when the namespace is declared on a child
>> > element.
>>
>>  What child element?  I speak about an XPath expression and
>> the associated set of namespace bindings.  Input documents
>> are not relevant here.
> 
> I was referring to the input document, see below.
> 
>> > > http://xml.apache.org/xalan-j/xpath_apis.html#namespacecontext
>>
>> > I've had a look at that, but it is a bad example: it is
>> > actually not necessary to do any binding in that
>> > case.
>>
>>  ?  Of course it is necessary.  You use the prefixes foo
>> and bar.  They have to be bound in one way or another.
> 
> See below.
> 
>> > This can be seen by using the ApplyXPathDOM or ApplyXPath
>> > command-line tests - they work fine.
>>
>>  Yes.  Because it sets the right bindings when it calls
>> selectNodeIterator().  This method takes its binding from
>> the context node passed at its first parameter.  In the case
>> of ApplyXPath that it the root element of the file (where
>> the bindings are declared).
> 
> OK, now I understand.
> 
> I had not realised that the binding was done that way; it seemed to be
> automatic in the document processing.
> 
> The JMeter code uses XPathAPI.eval(doc, query).
> 
> I just checked the Javadoc
> 
> http://xml.apache.org/xalan-j/apidocs/org/apache/xpath/XPathAPI.html#eval(org.w3c.dom.Node,%20java.lang.String)
> 
> Which says: "... Using this method, XPath namespace prefixes will be
> resolved from the namespaceNode".
> 
> However, there is no such parameter.
> 
> Further digging shows that what the code actually does is to use the
> contextNode as the namespaceNode.
> 
>> > Also, there has to be a nicer way to declare namespaces -
>> > the example suggests that it is necessary to create ones
>> > own (hard-coded) classes.  Surely there is a utility class
>> > to do this?
>>
>>  I don't see the problem.  You put a two-column table in
>> the GUI, allowing the user to edit the various bindings (on
>> the left the prefix, on the right the associated URI).
> 
> That part is fine.
> 
>> Then you use something like the following:
>>
>>    public class JMeterNamespaceContext
>>        implements NamespaceContext
>>    {
>>        // Bindings: keys=prefixes, values=uris
>>        public JMeterNamespaceContext(Map<String, String> bindings)
>>        {
>>            myPrefixToURI = bindings;
>>        }
>>
>>        public String getNamespaceURI(String prefix)
>>        {
>>            return myBindings.get(prefix);
>>        }
>>
>>        public String getPrefix(String uri)
>>        {
>>            List<String> prefixes = getURIToPrefix().get(uri);
>>            if ( prefixes == null ) {
>>                return null;
>>            }
>>            else {
>>                return prefixes.get(0);
>>            }
>>        }
>>
>>        public Iterator getPrefixes(String uri)
>>        {
>>            List<String> prefixes = getURIToPrefix().get(uri);
>>            if ( prefixes == null ) {
>>                return null;
>>            }
>>            else {
>>                return prefixes.iterator();
>>            }
>>        }
>>
>>        private Map<String, List<String>> getURIToPrefix()
>>        {
>>            if ( myURIToPrefix == null ) {
>>                myURIToPrefix = new HashMap<String, List<String>>();
>>                for ( String p : bindings.keySet() ) {
>>                    List<String> prefixes = myURIToPrefix.get(p);
>>                    if ( prefixes == null ) {
>>                        prefixes = new ArrayList();
>>                        myURIToPrefix.put(p, prefixes);
>>                    }
>>                    prefixes.add(bindings.get(p));
>>                }
>>            }
>>            else {
>>                return myURIToPrefix;
>>            }
>>        }
>>
>>        private Map<String, String> myPrefixToURI = null;
>>        private Map<String, List<String>> myURIToPrefix = null;
>>    }
>>
> 
> What I was getting at is that the above code could (*should*) be
> provided as utility ...
> 
>>  This is not tested and you should refer to the javadoc of
> 
> ... and then it would only have to be tested once ...
> 
>> NamespaceContext to see what changes to do to enforce its
>> contract, but the (simple) idea is there, IMHO.
>>
>>  Did I miss something?
> 
> No, I think it's mainly my misconceptions ...
> 
>>  Regards,
> 
> Thanks for the help and information; I think I understand a lot more
> about this now.
> 
> 

-- 
View this message in context: http://old.nabble.com/Using-XPath-with-namespace-defined-in-child-element-tp12462394p34441678.html
Sent from the Xalan - J - Users mailing list archive at Nabble.com.


Re: Using XPath with namespace defined in child element

Posted by sebb <se...@gmail.com>.
On 04/09/07, Florent Georges <li...@fgeorges.org> wrote:
> sebb wrote:
>
> > On 04/09/07, Florent Georges wrote:
>
> > >    bindings:
> > >      u1=uri 1
> > >      u2=uri 2
>
> > >    expression:
> > >      /u1:elem1/u2:elem2
>
> > > to get the deepest element.
>
> > OK, I now understand that XPath requires that the
> > namespace must be (unambiguously) pre-defined; this is not
> > possible when the namespace is declared on a child
> > element.
>
>  What child element?  I speak about an XPath expression and
> the associated set of namespace bindings.  Input documents
> are not relevant here.

I was referring to the input document, see below.

> > > http://xml.apache.org/xalan-j/xpath_apis.html#namespacecontext
>
> > I've had a look at that, but it is a bad example: it is
> > actually not necessary to do any binding in that
> > case.
>
>  ?  Of course it is necessary.  You use the prefixes foo
> and bar.  They have to be bound in one way or another.

See below.

> > This can be seen by using the ApplyXPathDOM or ApplyXPath
> > command-line tests - they work fine.
>
>  Yes.  Because it sets the right bindings when it calls
> selectNodeIterator().  This method takes its binding from
> the context node passed at its first parameter.  In the case
> of ApplyXPath that it the root element of the file (where
> the bindings are declared).

OK, now I understand.

I had not realised that the binding was done that way; it seemed to be
automatic in the document processing.

The JMeter code uses XPathAPI.eval(doc, query).

I just checked the Javadoc

http://xml.apache.org/xalan-j/apidocs/org/apache/xpath/XPathAPI.html#eval(org.w3c.dom.Node,%20java.lang.String)

Which says: "... Using this method, XPath namespace prefixes will be
resolved from the namespaceNode".

However, there is no such parameter.

Further digging shows that what the code actually does is to use the
contextNode as the namespaceNode.

> > Also, there has to be a nicer way to declare namespaces -
> > the example suggests that it is necessary to create ones
> > own (hard-coded) classes.  Surely there is a utility class
> > to do this?
>
>  I don't see the problem.  You put a two-column table in
> the GUI, allowing the user to edit the various bindings (on
> the left the prefix, on the right the associated URI).

That part is fine.

> Then you use something like the following:
>
>    public class JMeterNamespaceContext
>        implements NamespaceContext
>    {
>        // Bindings: keys=prefixes, values=uris
>        public JMeterNamespaceContext(Map<String, String> bindings)
>        {
>            myPrefixToURI = bindings;
>        }
>
>        public String getNamespaceURI(String prefix)
>        {
>            return myBindings.get(prefix);
>        }
>
>        public String getPrefix(String uri)
>        {
>            List<String> prefixes = getURIToPrefix().get(uri);
>            if ( prefixes == null ) {
>                return null;
>            }
>            else {
>                return prefixes.get(0);
>            }
>        }
>
>        public Iterator getPrefixes(String uri)
>        {
>            List<String> prefixes = getURIToPrefix().get(uri);
>            if ( prefixes == null ) {
>                return null;
>            }
>            else {
>                return prefixes.iterator();
>            }
>        }
>
>        private Map<String, List<String>> getURIToPrefix()
>        {
>            if ( myURIToPrefix == null ) {
>                myURIToPrefix = new HashMap<String, List<String>>();
>                for ( String p : bindings.keySet() ) {
>                    List<String> prefixes = myURIToPrefix.get(p);
>                    if ( prefixes == null ) {
>                        prefixes = new ArrayList();
>                        myURIToPrefix.put(p, prefixes);
>                    }
>                    prefixes.add(bindings.get(p));
>                }
>            }
>            else {
>                return myURIToPrefix;
>            }
>        }
>
>        private Map<String, String> myPrefixToURI = null;
>        private Map<String, List<String>> myURIToPrefix = null;
>    }
>

What I was getting at is that the above code could (*should*) be
provided as utility ...

>  This is not tested and you should refer to the javadoc of

... and then it would only have to be tested once ...

> NamespaceContext to see what changes to do to enforce its
> contract, but the (simple) idea is there, IMHO.
>
>  Did I miss something?

No, I think it's mainly my misconceptions ...

>  Regards,

Thanks for the help and information; I think I understand a lot more
about this now.

Re: Using XPath with namespace defined in child element

Posted by sebb <se...@gmail.com>.
On 04/09/07, Florent Georges <li...@fgeorges.org> wrote:
> sebb wrote:
>
>  Hi
>
> > The expression is entered by the user, which is why it is
> > a pain to use the work-round.
>
>  Ok, I misunderstood you.  In this case, you should ask the user,
> besides the XPath expression, to bound namespace prefixes he/she used
> within this expression.
>
>  You should never rely on prefixes outside the context of prefix
> binding.  XPath is very convenient and powerful, but relies on the
> hosting language for some stuff, as the namespace bindings.  Within
> XSLT or any other XML hosting language, it is very intuitive; within a
> GUI it has to be more explicitely thought about.
>
>  For instance, with the following document:
>
> > >    <pr:elem1 xmlns:pr="uri 1">
> > >      <pr:elem2 xmlns:pr="uri 2"/>
> > >    </pr:elem1>
>
> the user could say (plain emails are not the easiest way to draw GUI's
> windows, but you should see the overall principle):
>
>    bindings:
>      u1=uri 1
>      u2=uri 2
>
>    expression:
>      /u1:elem1/u2:elem2
>
> to get the deepest element.

OK, I now understand that XPath requires that the namespace must be
(unambiguously) pre-defined; this is not possible when the namespace
is declared on a child element.

>  See the following page to find a way to set on the Java object
> standing for the XPath expression the bindings that the user set on the
> GUI:
>
> http://xml.apache.org/xalan-j/xpath_apis.html#namespacecontext

I've had a look at that, but it is a bad example: it is actually not
necessary to do any binding in that case. This can be seen by using
the ApplyXPathDOM or ApplyXPath command-line tests - they work fine.

Presumably this is because the namespaces are declared at the top
level, allowing xalan to use the declarations for the whole document.

Also, there has to be a nicer way to declare namespaces - the example
suggests that it is necessary to create ones own (hard-coded) classes.
Surely there is a utility class to do this?

>  Regards,
>
> --drkm
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>      ______________________________________________________________________________
> Stockage illimité de vos mails avec Yahoo! Mail. Changez aujourd'hui de mail !
>
>

Re: Using XPath with namespace defined in child element

Posted by sebb <se...@gmail.com>.
On 03/09/07, Florent Georges <li...@fgeorges.org> wrote:
> sebb wrote:
>
>  Hi
>
> > The application is JMeter - we have an XPath extractor
> > which can be used to extract items from an XML document
> > sample - there is no stylesheet involved, just the XML
> > document and the XPath query.
>
>  If the expression is generated, I would suggest to use *[local-part()
> = '...' and namespace-uri() = '...'].  Using local-part() should not be
> an issue, neither for readibility since it is generated or for
> efficiency as pattern matching is not involved (but for this last point
> I'm just guessing).

The expression is entered by the user, which is why it is a pain to
use the work-round.

I suppose JMeter could parse ns:item to generate the expression
internally, but that could quickly get very tricky for more
complicated expressions.

>  But that way your expression will be more robust.  Be careful about
> cases like the following for instance (note the same prefix rebound):
>
>    <pr:elem1 xmlns:pr="uri 1">
>      <pr:elem2 xmlns:pr="uri 2"/>
>    </pr:elem1>

Indeed, that is one case where using the URI is better.

>  BTW, as it is a general XPath question, I suggest you to post on
> Mulberry's XSL List.  You'll get more advised ideas I think.

OK.

>  Regards,
>
> --drkm
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>      ______________________________________________________________________________
> Stockage illimité de vos mails avec Yahoo! Mail. Changez aujourd'hui de mail !
>
>

Re: Using XPath with namespace defined in child element

Posted by sebb <se...@gmail.com>.
Thanks, but I'm not using a stylesheet,

The application is JMeter - we have an XPath extractor which can be
used to extract items from an XML document sample - there is no
stylesheet involved, just the XML document and the XPath query.

S///
On 03/09/07, Mukul Gandhi <ga...@gmail.com> wrote:
> You need to provide the namespace declaration,
> xmlns:ns="http://biz.aol.com/schema/2006-12-18" in the stylesheet
> also. Then it should work.
>
> For e.g., this stylesheet
>
> <?xml version="1.0"?>
> <xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>                        xmlns:ns="http://biz.aol.com/schema/2006-12-18"
>                        version="1.0">
>
>  <xsl:output method="text" />
>
>  <xsl:template match="/">
>   <xsl:value-of select="count(//ns:result)" />
>  </xsl:template>
>
> </xsl:stylesheet>
>
> When applied to your source document, works fine.
>
> On 9/3/07, sebb <se...@gmail.com> wrote:
> > I'm using xalan-j 2.7.0; testing using the ApplyXPathDOM sample code.
> > ====
> >
> > Sample XML document:
> >
> > ===
> > <?xml version="1.0" encoding="utf-8"?>
> > <soapenv:Envelope
> >  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
> >  <soapenv:Body
> >  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> >  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
> >  <ns:registerOfferResponse  xmlns:ns="http://biz.aol.com/schema/2006-12-18">
> >   <ns:result>0</ns:result>
> >  </ns:registerOfferResponse>
> >  </soapenv:Body>
> > </soapenv:Envelope>
> > ===
> >
> > Using the XPath
> >
> > //ns:result
> >
> > generates the error:
> >
> > "Prefix must resolve to a namespace: ns"
> >
> > If I move the declaration to the top-level element in the xml document, as in
> >
> > ===
> > <?xml version="1.0" encoding="utf-8"?>
> > <soapenv:Envelope xmlns:ns="http://biz.aol.com/schema/2006-12-18"
> >  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
> >  <soapenv:Body
> >  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> >  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
> >  <ns:registerOfferResponse>
> >   <ns:result>0</ns:result>
> >  </ns:registerOfferResponse>
> >  </soapenv:Body>
> > </soapenv:Envelope>
> > ===
> >
> > then the XPath //ns:result works fine.
> >
> > I get the same error as before if "ns" is declared in the Body element.
> >
> > It apears that the XPath processing requires the namespace to be
> > defined only in the top-level element, whereas AIUI XML allows the
> > namespace to be defined on first use of an namespace.
> >
> > Is this correct?
> >
> > If so, is there any way to tell xalan to use the namespace
> > declarations from child elements?
> >
> > BTW, I have seen a work-round, which is to use the query:
> >
> > //*[local-name()='result' and
> > namespace-uri()='http://biz.aol.com/schema/2006-12-18' ]
> >
> > but that is really messy.
> >
> > S///
> >
>
>
> --
> Regards,
> Mukul Gandhi
>

Re: Using XPath with namespace defined in child element

Posted by Mukul Gandhi <ga...@gmail.com>.
You need to provide the namespace declaration,
xmlns:ns="http://biz.aol.com/schema/2006-12-18" in the stylesheet
also. Then it should work.

For e.g., this stylesheet

<?xml version="1.0"?>
<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                        xmlns:ns="http://biz.aol.com/schema/2006-12-18"
                        version="1.0">

 <xsl:output method="text" />

 <xsl:template match="/">
   <xsl:value-of select="count(//ns:result)" />
 </xsl:template>

</xsl:stylesheet>

When applied to your source document, works fine.

On 9/3/07, sebb <se...@gmail.com> wrote:
> I'm using xalan-j 2.7.0; testing using the ApplyXPathDOM sample code.
> ====
>
> Sample XML document:
>
> ===
> <?xml version="1.0" encoding="utf-8"?>
> <soapenv:Envelope
>  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
>  <soapenv:Body
>  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
>  <ns:registerOfferResponse  xmlns:ns="http://biz.aol.com/schema/2006-12-18">
>   <ns:result>0</ns:result>
>  </ns:registerOfferResponse>
>  </soapenv:Body>
> </soapenv:Envelope>
> ===
>
> Using the XPath
>
> //ns:result
>
> generates the error:
>
> "Prefix must resolve to a namespace: ns"
>
> If I move the declaration to the top-level element in the xml document, as in
>
> ===
> <?xml version="1.0" encoding="utf-8"?>
> <soapenv:Envelope xmlns:ns="http://biz.aol.com/schema/2006-12-18"
>  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
>  <soapenv:Body
>  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
>  <ns:registerOfferResponse>
>   <ns:result>0</ns:result>
>  </ns:registerOfferResponse>
>  </soapenv:Body>
> </soapenv:Envelope>
> ===
>
> then the XPath //ns:result works fine.
>
> I get the same error as before if "ns" is declared in the Body element.
>
> It apears that the XPath processing requires the namespace to be
> defined only in the top-level element, whereas AIUI XML allows the
> namespace to be defined on first use of an namespace.
>
> Is this correct?
>
> If so, is there any way to tell xalan to use the namespace
> declarations from child elements?
>
> BTW, I have seen a work-round, which is to use the query:
>
> //*[local-name()='result' and
> namespace-uri()='http://biz.aol.com/schema/2006-12-18' ]
>
> but that is really messy.
>
> S///
>


-- 
Regards,
Mukul Gandhi