On 03/09/07, Mukul Gandhi <[EMAIL PROTECTED]> 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 <[EMAIL PROTECTED]> 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 <[EMAIL PROTECTED]> 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 <[EMAIL PROTECTED]> 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 >