You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xmlbeans-dev@xml.apache.org by nn <nn...@comcast.net> on 2004/06/18 12:48:50 UTC

bug fix for XPath using Namespace

Hi,
I think it is important to remove the restriction for not allowing to use
namespace in complex XPath expression.
(s.t ./po:purchaseOrder/po:shipTo[po:name = 'Helen Zoe']" ).

Since otherwise, the useful XPath (which is provided by JAXEN) cannot be
used for most XML data associated with XMLSchema. I thought this may be a
restriction of JAXEN, but it wasn't.
This fix is relatively simple. I  hope this should be incorporated in the
release (after reviewed by commiters and tested throughly).

I'll attach the jar file.

nn

------

public class POTest {
    public static void filter(XmlObject[] result) throws Throwable {
        for (int i = 0; i < result.length; i++) {
     XmlObject obj = result[i];
     System.out.println(">> filter: "+obj.getClass()+", "+obj);
 }
    }
    private static String[] test_xpathes = new String[]{
  "//po:purchaseOrder",
  "./po:purchaseOrder/po:shipTo/po:name",
  "./po:purchaseOrder/po:shipTo[po:name = 'Helen Zoe']",
  };

    public static void test(PurchaseOrderDocument xmlObj, String xpath)
throws Throwable {
 System.out.println("\n===== test: xpath: "+xpath+" =====");
 XmlObject[] result = xmlObj.selectPath(xpath);
 filter(result);
    }

    public static void main(String argv[]) {
 try {
     File sourceID = new File(argv[0]);
     PurchaseOrderDocument xmlObj =
PurchaseOrderDocument.Factory.parse(sourceID);
     for (int i = 0; i < test_xpathes.length; i++) {
  test(xmlObj, test_xpathes[i]);
     }
 } catch (Throwable err) {
     err.printStackTrace();
 }
    }
}
-----
the result:

===== test: xpath: //po:purchaseOrder =====
>> addNamespaces: prefix: po, uri: http://example.com/po
>> filter: class com.example.po.impl.PurchaseOrderTypeImpl, <xml-fragment
xmlns:po="http://example.com/po">
  <po:shipTo type="USAddress">
    <po:name>Helen Zoe</po:name>
    <po:street>47 Eden Street</po:street>
    <po:city>Santa Clara</po:city>
    <po:state>CA</po:state>
    <po:zip>95054</po:zip>
  </po:shipTo>
  <po:billTo type="USAddress">
    <po:name>Robert Zoe</po:name>
    <po:street>47 Eden Street</po:street>
    <po:city>Santa Clara</po:city>
    <po:state>CA</po:state>
    <po:zip>95054</po:zip>
  </po:billTo>
  <po:itemsList>
    <po:item partNum="833-AA">
      <po:productName>Lapis necklace</po:productName>
      <po:quantity>1</po:quantity>
      <po:USPrice>120</po:USPrice>
      <po:comment>Want this for the holidays!</po:comment>
      <po:shipDate>2001-12-15</po:shipDate>
    </po:item>
    <po:item partNum="423-AA">
      <po:productName>Baby Sound Monitor</po:productName>
      <po:quantity>2</po:quantity>
      <po:USPrice>300</po:USPrice>
      <po:comment>Got twins!</po:comment>
      <po:shipDate>2001-12-17</po:shipDate>
    </po:item>
  </po:itemsList>
</xml-fragment>

===== test: xpath: ./po:purchaseOrder/po:shipTo/po:name =====
>> addNamespaces: prefix: po, uri: http://example.com/po
>> filter: class org.apache.xmlbeans.impl.values.XmlStringImpl,
<xml-fragment xmlns:po="http://example.com/po">Helen Zoe</xml-fragment>

===== test: xpath: ./po:purchaseOrder/po:shipTo[po:name = 'Helen Zoe'] =====
>> addNamespaces: prefix: po, uri: http://example.com/po
>> filter: class com.example.po.impl.USAddressImpl, <xml-fragment
type="USAddress" xmlns:po="http://example.com/po">
  <po:name>Helen Zoe</po:name>
  <po:street>47 Eden Street</po:street>
  <po:city>Santa Clara</po:city>
  <po:state>CA</po:state>
  <po:zip>95054</po:zip>
</xml-fragment>

-----
po.xml

?xml version="1.0"?>
<po:purchaseOrder xmlns:po="http://example.com/po">
   <po:shipTo type="USAddress">
      <po:name>Helen Zoe</po:name>
      <po:street>47 Eden Street</po:street>
      <po:city>Santa Clara</po:city>
      <po:state>CA</po:state>
      <po:zip>95054</po:zip>
   </po:shipTo>
   <po:billTo type="USAddress">
      <po:name>Robert Zoe</po:name>
      <po:street>47 Eden Street</po:street>
      <po:city>Santa Clara</po:city>
      <po:state>CA</po:state>
      <po:zip>95054</po:zip>
   </po:billTo>
   <po:itemsList>
        <po:item partNum="833-AA">
            <po:productName>Lapis necklace</po:productName>
            <po:quantity>1</po:quantity>
            <po:USPrice>120</po:USPrice>
            <po:comment>Want this for the holidays!</po:comment>
            <po:shipDate>2001-12-15</po:shipDate>
        </po:item>
        <po:item partNum="423-AA">
            <po:productName>Baby Sound Monitor</po:productName>
            <po:quantity>2</po:quantity>
            <po:USPrice>300</po:USPrice>
            <po:comment>Got twins!</po:comment>
            <po:shipDate>2001-12-17</po:shipDate>
        </po:item>
    </po:itemsList>
</po:purchaseOrder>

----
po.xsd
<xsd:schema targetNamespace="http://example.com/po"
      xmlns="http://example.com/po"
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      elementFormDefault="qualified"
      attributeFormDefault="qualified">

 <xsd:annotation>
  <xsd:documentation xml:lang="en">
   Purchase order schema for Example.com.
   Copyright 2000 Example.com. All rights reserved.
  </xsd:documentation>
 </xsd:annotation>

 <xsd:element name="purchaseOrder" type="PurchaseOrderType"/>

 <xsd:element name="comment" type="xsd:string"/>

 <xsd:complexType name="PurchaseOrderType">
  <xsd:sequence>
   <xsd:element name="shipTo" type="USAddress"/>
   <xsd:element name="billTo" type="USAddress"/>
   <xsd:element ref="comment" minOccurs="0"/>
   <xsd:element name="itemsList"  type="Items"/>
  </xsd:sequence>
  <xsd:attribute name="orderDate" type="xsd:date"/>
 </xsd:complexType>

 <xsd:complexType name="USAddress">
  <xsd:sequence>
   <xsd:element name="name"   type="xsd:string"/>
   <xsd:element name="street" type="xsd:string"/>
   <xsd:element name="city"   type="xsd:string"/>
   <xsd:element name="state"  type="xsd:string"/>
   <xsd:element name="zip"    type="xsd:decimal"/>
  </xsd:sequence>
  <xsd:attribute name="country" type="xsd:NMTOKEN"
     fixed="US"/>
 </xsd:complexType>

 <xsd:complexType name="Items">
  <xsd:sequence>
   <xsd:element name="item" minOccurs="0" maxOccurs="unbounded">
    <xsd:complexType>
     <xsd:sequence>
      <xsd:element name="productName" type="xsd:string"/>
      <xsd:element name="quantity">
       <xsd:simpleType>
        <xsd:restriction base="xsd:positiveInteger">
         <xsd:maxExclusive value="100"/>
        </xsd:restriction>
       </xsd:simpleType>
      </xsd:element>
      <xsd:element name="USPrice"  type="xsd:decimal"/>
      <xsd:element ref="comment"   minOccurs="0"/>
      <xsd:element name="shipDate" type="xsd:date" minOccurs="0"/>
     </xsd:sequence>
     <xsd:attribute name="partNum" type="SKU" use="required"/>
    </xsd:complexType>
   </xsd:element>
  </xsd:sequence>
 </xsd:complexType>

 <!-- Stock Keeping Unit, a code for identifying products -->
 <xsd:simpleType name="SKU">
  <xsd:restriction base="xsd:string">
   <xsd:pattern value="\d{3}-[A-Z]{2}"/>
  </xsd:restriction>
 </xsd:simpleType>

</xsd:schema>

---
here is the code. I modified XBeansXPath.java and XBeansXPathAdv.java, and
added XBeansNamespace.java

package org.apache.xmlbeans.impl.xpath.jaxen;
...
public class XBeansXPath extends BaseXPath
{
    ....
    public List selectNodes(Object node) throws JaxenException
    {
        XmlCursor xc;
 XmlObject xmlObj;
        if (node instanceof XmlObject)
        {
     xmlObj = (XmlObject)node;
     xc = xmlObj.newCursor();
        }
        else if (node instanceof XmlCursor)
 {
     xc = (XmlCursor)node;
     xmlObj = xc.getObject();
            xc = xc.newCursor();
        }
        else
            throw new IllegalArgumentException("node must be an XmlObject or
an XmlCursor, found: " + node.getClass());

 XBeansNamespace.addNamespaces(this, xmlObj); // nn (support xpath including
namespace)
        ((XBeansNavigator)getNavigator()).setCursor(xc);
        return new ListImpl(super.selectNodes(
XBeansNavigator.getBookmarkInThisPlace(xc) ));
    }
....
}
----
package org.apache.xmlbeans.impl.xpath.jaxen;
...
public class XBeansXPathAdv
    extends BaseXPath
    implements JaxenXBeansDelegate.SelectPathInterface
{
...
    public List selectNodes(Object node) throws JaxenException
    {
        XmlCursor xc;
        xc = ((XmlCursor)node);
 XmlObject xmlObj = xc.getObject();
 XBeansNamespace.addNamespaces(this, xmlObj);
        ((XBeansNavigator)getNavigator()).setCursor(xc);
        return super.selectNodes(
XBeansNavigator.getBookmarkInThisPlace(xc) );
    }
...
}
-----
package org.apache.xmlbeans.impl.xpath.jaxen;
import org.jaxen.BaseXPath;
import org.jaxen.JaxenException;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlCursor;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;

public class XBeansNamespace
{
    public static void addNamespaces(BaseXPath baseXPath, XmlObject doc)
throws JaxenException {
 HashMap nslist = getAdditionalNamespaces(getFirstChild(doc));
 for (Iterator iter = nslist.entrySet().iterator(); iter.hasNext(); ) {
     Map.Entry entry = (Map.Entry)iter.next();
     String prefix = (String)entry.getKey();
     String uri = (String)entry.getValue();
     System.out.println(">> addNamespaces: prefix: "+prefix+", uri: "+uri);
     baseXPath.addNamespace(prefix, uri);
 }
    }
    private static XmlObject getFirstChild(XmlObject xmlObj) {
 XmlCursor xc = xmlObj.newCursor();
 try {
     xc.toFirstChild();
     return xc.getObject();
 } finally {
     xc.dispose();
 }
    }
    private static HashMap getAdditionalNamespaces(XmlObject xmlObj) {
 HashMap nslist = new HashMap();
 XmlCursor xc = xmlObj.newCursor();
 try {
     xc.getAllNamespaces(nslist);
 } finally {
     xc.dispose();
 }
 return nslist;
    }
}



- ---------------------------------------------------------------------
To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/


Re: bug fix for XPath using Namespace

Posted by nn <nn...@comcast.net>.
Hi David,

Thank you for the example, though it seems too complex for me to
understand;-).
But I felt there are some misunderstanding for uasage case of selectPath in
XMLBeans applications.
Originally XPath has been used/developed for XSLT(I guess), so these API are
intended for such Language processor.
So the scoping of context are important aspect to support(I agree for this).

But for XML binding, I wonder how many people use selectPath in such context
sensitive way.
Normally selectPath is applied from outside of XML document, so no need to
handle context.
If this is the major case, default namespace handling should be optimized
for this usage.
(I think, XPath expression, both of "./po:item" and "./item" should be
allowed for po.xml in my previous mail)

Also these selectPath would be used like embedded SQL, so poeple would like
have jdbc like API.
I think it is useful to have a similar feature of supporting $1, $2
variables to bind values outside of XPath expression at runtime.
e.g,
xml.selectPath("./a/b[@c < $1]", x+1);

Since this can be handled as xml.selectPath("./a/b[@c < "+(x+1)+"]"), this
should be fasible.
But if JAXEN, SAXON support them as part of compilation, they can be
efficient also and XMLBeans should use such optimized APIs internally.

nn

----- Original Message ----- 
From: "David Waite" <ma...@akuma.org>
To: <xm...@xml.apache.org>
Sent: Friday, June 18, 2004 1:37 PM
Subject: Re: bug fix for XPath using Namespace


>
> On Jun 18, 2004, at 12:59 PM, nn wrote:
>
> > David,
> >> The specification says 'the context of the xpath expression', but
> >> doesn't define that. In XSLT, the context of an xpath expression is
> >> the
> >> location within the XSLT document that it is used - all namespaces
> >> declared in the element scope of the path attribute or text will
> >> provide prefix mappings for the expression. In code, that context
> >> needs
> >> to be given in some other manner.
> >
> > Do you mean that if selectPath is used to implement XSLT, it must get
> > context information from the environment of the XPath? It may be a
> > possible
> > case, but normally in XPath 1.0, it would be used only to access sub
> > nodes
> > of target document, how other namespace outside of target document
> > would be
> > relevent for it?
> > Can you give me a simple example to justify such a usage?
>
> sure, say you have a document like
>
> <foo xmlns="urn:foo">
>    <x:bar xmlns="urn:bar" id="10" />
> </foo>
>
> and
>
> <xsl:stylesheet ... xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> xmlns="http://www.w3.org/TR/xhtml1/strict"
>   xmlns:n1="urn:foo">
> <html>
> <body>The value is <xsl:value-of xmlns:n2="urn:bar"
> match="/n1:foo/n2:bar@id"/>. </body>
> </html>
> </xsl:stylesheet>
>
> The namespaces are in scope for the xsl:value-of by the xsl:stylesheet
> element and the value-of element itself. Because of this, the prefixes
> can be used for the xpath.  /foo/n2:bar wouldn't work (I believe)
> because the unprefixed namespace in scope is for xhtml, not urn:foo.
> /foo/x:bar won't work either (using the prefixes in the target document
> to match xpath expressions), which is a good thing - prefixes are
> pretty widely considered not to affect the content of the document.
>
> > Anyway probably, it would be good idea to allows to use XPath in
> > various
> > context.
> > but the most simple case, such namespace declaration is quite
> > redendent.
> > So maybe it would be useful to provide an option to use the target
> > namespace
> > as implicitly declared.
> >
> >>
> >> The odd cases are the default namespace and nonprefixed elements, I
> >> can't tell you how those are supposed to work off the top of my head
> >> :-)  My assumption is that nonprefixed parts of an xpath expression
> >> always correspond to the default namespace of the target document; if
> >> you have a non-prefixed namespace declaration on a child element, your
> >> xpath needs to map that to a namespace (probably using a local prefix)
> >> to match.
> > this behavior would be achieved using my approach, and it seems useful.
>
> Do people think that having a setXPathContext(...) method or methods on
> XmlOption would be appropriate? if so, the default could be 'map the
> namespace of the root element of this document to the default prefix -
> I also think this will make peoples' lives easier - most people do not
> know the.. fun-ness of namespaces and XPath :-).
>
> -David Waite
>
>
> - ---------------------------------------------------------------------
> To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
> Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/
>


- ---------------------------------------------------------------------
To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/


Re: bug fix for XPath using Namespace

Posted by nn <nn...@comcast.net>.
Hi David,

Thank you for the example, though it seems too complex for me to
understand;-).
But I felt there are some misunderstanding for uasage case of selectPath in
XMLBeans applications.
Originally XPath has been used/developed for XSLT(I guess), so these API are
intended for such Language processor.
So the scoping of context are important aspect to support(I agree for this).

But for XML binding, I wonder how many people use selectPath in such context
sensitive way.
Normally selectPath is applied from outside of XML document, so no need to
handle context.
If this is the major case, default namespace handling should be optimized
for this usage.
(I think, XPath expression, both of "./po:item" and "./item" should be
allowed for po.xml in my previous mail)

Also these selectPath would be used like embedded SQL, so poeple would like
have jdbc like API.
I think it is useful to have a similar feature of supporting $1, $2
variables to bind values outside of XPath expression at runtime.
e.g,
xml.selectPath("./a/b[@c < $1]", x+1);

Since this can be handled as xml.selectPath("./a/b[@c < "+(x+1)+"]"), this
should be fasible.
But if JAXEN, SAXON support them as part of compilation, they can be
efficient also and XMLBeans should use such optimized APIs internally.

nn

----- Original Message ----- 
From: "David Waite" <ma...@akuma.org>
To: <xm...@xml.apache.org>
Sent: Friday, June 18, 2004 1:37 PM
Subject: Re: bug fix for XPath using Namespace


>
> On Jun 18, 2004, at 12:59 PM, nn wrote:
>
> > David,
> >> The specification says 'the context of the xpath expression', but
> >> doesn't define that. In XSLT, the context of an xpath expression is
> >> the
> >> location within the XSLT document that it is used - all namespaces
> >> declared in the element scope of the path attribute or text will
> >> provide prefix mappings for the expression. In code, that context
> >> needs
> >> to be given in some other manner.
> >
> > Do you mean that if selectPath is used to implement XSLT, it must get
> > context information from the environment of the XPath? It may be a
> > possible
> > case, but normally in XPath 1.0, it would be used only to access sub
> > nodes
> > of target document, how other namespace outside of target document
> > would be
> > relevent for it?
> > Can you give me a simple example to justify such a usage?
>
> sure, say you have a document like
>
> <foo xmlns="urn:foo">
>    <x:bar xmlns="urn:bar" id="10" />
> </foo>
>
> and
>
> <xsl:stylesheet ... xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> xmlns="http://www.w3.org/TR/xhtml1/strict"
>   xmlns:n1="urn:foo">
> <html>
> <body>The value is <xsl:value-of xmlns:n2="urn:bar"
> match="/n1:foo/n2:bar@id"/>. </body>
> </html>
> </xsl:stylesheet>
>
> The namespaces are in scope for the xsl:value-of by the xsl:stylesheet
> element and the value-of element itself. Because of this, the prefixes
> can be used for the xpath.  /foo/n2:bar wouldn't work (I believe)
> because the unprefixed namespace in scope is for xhtml, not urn:foo.
> /foo/x:bar won't work either (using the prefixes in the target document
> to match xpath expressions), which is a good thing - prefixes are
> pretty widely considered not to affect the content of the document.
>
> > Anyway probably, it would be good idea to allows to use XPath in
> > various
> > context.
> > but the most simple case, such namespace declaration is quite
> > redendent.
> > So maybe it would be useful to provide an option to use the target
> > namespace
> > as implicitly declared.
> >
> >>
> >> The odd cases are the default namespace and nonprefixed elements, I
> >> can't tell you how those are supposed to work off the top of my head
> >> :-)  My assumption is that nonprefixed parts of an xpath expression
> >> always correspond to the default namespace of the target document; if
> >> you have a non-prefixed namespace declaration on a child element, your
> >> xpath needs to map that to a namespace (probably using a local prefix)
> >> to match.
> > this behavior would be achieved using my approach, and it seems useful.
>
> Do people think that having a setXPathContext(...) method or methods on
> XmlOption would be appropriate? if so, the default could be 'map the
> namespace of the root element of this document to the default prefix -
> I also think this will make peoples' lives easier - most people do not
> know the.. fun-ness of namespaces and XPath :-).
>
> -David Waite
>
>
> - ---------------------------------------------------------------------
> To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
> Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/
>


- ---------------------------------------------------------------------
To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/


Re: bug fix for XPath using Namespace

Posted by David Waite <ma...@akuma.org>.
On Jun 18, 2004, at 12:59 PM, nn wrote:

> David,
>> The specification says 'the context of the xpath expression', but
>> doesn't define that. In XSLT, the context of an xpath expression is 
>> the
>> location within the XSLT document that it is used - all namespaces
>> declared in the element scope of the path attribute or text will
>> provide prefix mappings for the expression. In code, that context 
>> needs
>> to be given in some other manner.
>
> Do you mean that if selectPath is used to implement XSLT, it must get
> context information from the environment of the XPath? It may be a 
> possible
> case, but normally in XPath 1.0, it would be used only to access sub 
> nodes
> of target document, how other namespace outside of target document 
> would be
> relevent for it?
> Can you give me a simple example to justify such a usage?

sure, say you have a document like

<foo xmlns="urn:foo">
   <x:bar xmlns="urn:bar" id="10" />
</foo>

and

<xsl:stylesheet ... xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns="http://www.w3.org/TR/xhtml1/strict"
  xmlns:n1="urn:foo">
<html>
	<body>The value is <xsl:value-of xmlns:n2="urn:bar" 
match="/n1:foo/n2:bar@id"/>. </body>
</html>
</xsl:stylesheet>

The namespaces are in scope for the xsl:value-of by the xsl:stylesheet 
element and the value-of element itself. Because of this, the prefixes 
can be used for the xpath.  /foo/n2:bar wouldn't work (I believe) 
because the unprefixed namespace in scope is for xhtml, not urn:foo. 
/foo/x:bar won't work either (using the prefixes in the target document 
to match xpath expressions), which is a good thing - prefixes are 
pretty widely considered not to affect the content of the document.

> Anyway probably, it would be good idea to allows to use XPath in 
> various
> context.
> but the most simple case, such namespace declaration is quite 
> redendent.
> So maybe it would be useful to provide an option to use the target 
> namespace
> as implicitly declared.
>
>>
>> The odd cases are the default namespace and nonprefixed elements, I
>> can't tell you how those are supposed to work off the top of my head
>> :-)  My assumption is that nonprefixed parts of an xpath expression
>> always correspond to the default namespace of the target document; if
>> you have a non-prefixed namespace declaration on a child element, your
>> xpath needs to map that to a namespace (probably using a local prefix)
>> to match.
> this behavior would be achieved using my approach, and it seems useful.

Do people think that having a setXPathContext(...) method or methods on 
XmlOption would be appropriate? if so, the default could be 'map the 
namespace of the root element of this document to the default prefix - 
I also think this will make peoples' lives easier - most people do not 
know the.. fun-ness of namespaces and XPath :-).

-David Waite


- ---------------------------------------------------------------------
To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/


Re: bug fix for XPath using Namespace

Posted by David Waite <ma...@akuma.org>.
On Jun 18, 2004, at 12:59 PM, nn wrote:

> David,
>> The specification says 'the context of the xpath expression', but
>> doesn't define that. In XSLT, the context of an xpath expression is 
>> the
>> location within the XSLT document that it is used - all namespaces
>> declared in the element scope of the path attribute or text will
>> provide prefix mappings for the expression. In code, that context 
>> needs
>> to be given in some other manner.
>
> Do you mean that if selectPath is used to implement XSLT, it must get
> context information from the environment of the XPath? It may be a 
> possible
> case, but normally in XPath 1.0, it would be used only to access sub 
> nodes
> of target document, how other namespace outside of target document 
> would be
> relevent for it?
> Can you give me a simple example to justify such a usage?

sure, say you have a document like

<foo xmlns="urn:foo">
   <x:bar xmlns="urn:bar" id="10" />
</foo>

and

<xsl:stylesheet ... xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns="http://www.w3.org/TR/xhtml1/strict"
  xmlns:n1="urn:foo">
<html>
	<body>The value is <xsl:value-of xmlns:n2="urn:bar" 
match="/n1:foo/n2:bar@id"/>. </body>
</html>
</xsl:stylesheet>

The namespaces are in scope for the xsl:value-of by the xsl:stylesheet 
element and the value-of element itself. Because of this, the prefixes 
can be used for the xpath.  /foo/n2:bar wouldn't work (I believe) 
because the unprefixed namespace in scope is for xhtml, not urn:foo. 
/foo/x:bar won't work either (using the prefixes in the target document 
to match xpath expressions), which is a good thing - prefixes are 
pretty widely considered not to affect the content of the document.

> Anyway probably, it would be good idea to allows to use XPath in 
> various
> context.
> but the most simple case, such namespace declaration is quite 
> redendent.
> So maybe it would be useful to provide an option to use the target 
> namespace
> as implicitly declared.
>
>>
>> The odd cases are the default namespace and nonprefixed elements, I
>> can't tell you how those are supposed to work off the top of my head
>> :-)  My assumption is that nonprefixed parts of an xpath expression
>> always correspond to the default namespace of the target document; if
>> you have a non-prefixed namespace declaration on a child element, your
>> xpath needs to map that to a namespace (probably using a local prefix)
>> to match.
> this behavior would be achieved using my approach, and it seems useful.

Do people think that having a setXPathContext(...) method or methods on 
XmlOption would be appropriate? if so, the default could be 'map the 
namespace of the root element of this document to the default prefix - 
I also think this will make peoples' lives easier - most people do not 
know the.. fun-ness of namespaces and XPath :-).

-David Waite


- ---------------------------------------------------------------------
To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/


Re: bug fix for XPath using Namespace

Posted by nn <nn...@comcast.net>.
David,
> The specification says 'the context of the xpath expression', but
> doesn't define that. In XSLT, the context of an xpath expression is the
> location within the XSLT document that it is used - all namespaces
> declared in the element scope of the path attribute or text will
> provide prefix mappings for the expression. In code, that context needs
> to be given in some other manner.

Do you mean that if selectPath is used to implement XSLT, it must get
context information from the environment of the XPath? It may be a possible
case, but normally in XPath 1.0, it would be used only to access sub nodes
of target document, how other namespace outside of target document would be
relevent for it?
Can you give me a simple example to justify such a usage?

Anyway probably, it would be good idea to allows to use XPath in various
context.
but the most simple case, such namespace declaration is quite redendent.
So maybe it would be useful to provide an option to use the target namespace
as implicitly declared.

>
> The odd cases are the default namespace and nonprefixed elements, I
> can't tell you how those are supposed to work off the top of my head
> :-)  My assumption is that nonprefixed parts of an xpath expression
> always correspond to the default namespace of the target document; if
> you have a non-prefixed namespace declaration on a child element, your
> xpath needs to map that to a namespace (probably using a local prefix)
> to match.
this behavior would be achieved using my approach, and it seems useful.

nn


>
> -David Waite
>
> On Jun 18, 2004, at 10:18 AM, nn wrote:
>
> >
> >> Namespace prefixes are determined by the context of the XPath
> >> expression, not that of the 'target' document.
> >
> > How do you define the context of XPath?
> > Is the syntax of current XMLBeans a part of XPath specification?
> >
> > Are there any meaningfull case where the namespace which is not used
> > in the
> > target document is useful?
> > I think the most simple use case, using namespace associated with
> > target
> > document as default namespace would be the most convenient. This kind
> > of
> > behavior seems not part of standard, but a part of embeding system.
> >
> >>
> >> I have a patch that uses XmlOptions (I think setSaveSuggestedPrefixes)
> >> to supply this information into XmlBeans for XPath usage. I'm fine
> >> with
> >> submitting this patch sometime this weekend - would a 'fresh'
> >> XmlOptions option be preferred over setSaveSuggestedPrefixes and the
> >> others present already?
> >
> > Anyway, as long as this problem are removed soon, it's OK.
> > This aspect was crutial for me.
> >
> > nn
> >
> >>
> >> -David Waite
> >>
> >> On Jun 18, 2004, at 5:48 AM, nn wrote:
> >>
> >>> Hi,
> >>> I think it is important to remove the restriction for not allowing to
> >>> use
> >>> namespace in complex XPath expression.
> >>> (s.t ./po:purchaseOrder/po:shipTo[po:name = 'Helen Zoe']" ).
> >>>
> >>> Since otherwise, the useful XPath (which is provided by JAXEN) cannot
> >>> be
> >>> used for most XML data associated with XMLSchema. I thought this may
> >>> be a
> >>> restriction of JAXEN, but it wasn't.
> >>> This fix is relatively simple. I  hope this should be incorporated in
> >>> the
> >>> release (after reviewed by commiters and tested throughly).
> >>>
> >>> I'll attach the jar file.
> >>>
> >>> nn
> >>>
> >>> ------
> >>>
> >>> public class POTest {
> >>>     public static void filter(XmlObject[] result) throws Throwable {
> >>>         for (int i = 0; i < result.length; i++) {
> >>>      XmlObject obj = result[i];
> >>>      System.out.println(">> filter: "+obj.getClass()+", "+obj);
> >>>  }
> >>>     }
> >>>     private static String[] test_xpathes = new String[]{
> >>>   "//po:purchaseOrder",
> >>>   "./po:purchaseOrder/po:shipTo/po:name",
> >>>   "./po:purchaseOrder/po:shipTo[po:name = 'Helen Zoe']",
> >>>   };
> >>>
> >>>     public static void test(PurchaseOrderDocument xmlObj, String
> >>> xpath)
> >>> throws Throwable {
> >>>  System.out.println("\n===== test: xpath: "+xpath+" =====");
> >>>  XmlObject[] result = xmlObj.selectPath(xpath);
> >>>  filter(result);
> >>>     }
> >>>
> >>>     public static void main(String argv[]) {
> >>>  try {
> >>>      File sourceID = new File(argv[0]);
> >>>      PurchaseOrderDocument xmlObj =
> >>> PurchaseOrderDocument.Factory.parse(sourceID);
> >>>      for (int i = 0; i < test_xpathes.length; i++) {
> >>>   test(xmlObj, test_xpathes[i]);
> >>>      }
> >>>  } catch (Throwable err) {
> >>>      err.printStackTrace();
> >>>  }
> >>>     }
> >>> }
> >>> -----
> >>> the result:
> >>>
> >>> ===== test: xpath: //po:purchaseOrder =====
> >>>>> addNamespaces: prefix: po, uri: http://example.com/po
> >>>>> filter: class com.example.po.impl.PurchaseOrderTypeImpl,
> >>>>> <xml-fragment
> >>> xmlns:po="http://example.com/po">
> >>>   <po:shipTo type="USAddress">
> >>>     <po:name>Helen Zoe</po:name>
> >>>     <po:street>47 Eden Street</po:street>
> >>>     <po:city>Santa Clara</po:city>
> >>>     <po:state>CA</po:state>
> >>>     <po:zip>95054</po:zip>
> >>>   </po:shipTo>
> >>>   <po:billTo type="USAddress">
> >>>     <po:name>Robert Zoe</po:name>
> >>>     <po:street>47 Eden Street</po:street>
> >>>     <po:city>Santa Clara</po:city>
> >>>     <po:state>CA</po:state>
> >>>     <po:zip>95054</po:zip>
> >>>   </po:billTo>
> >>>   <po:itemsList>
> >>>     <po:item partNum="833-AA">
> >>>       <po:productName>Lapis necklace</po:productName>
> >>>       <po:quantity>1</po:quantity>
> >>>       <po:USPrice>120</po:USPrice>
> >>>       <po:comment>Want this for the holidays!</po:comment>
> >>>       <po:shipDate>2001-12-15</po:shipDate>
> >>>     </po:item>
> >>>     <po:item partNum="423-AA">
> >>>       <po:productName>Baby Sound Monitor</po:productName>
> >>>       <po:quantity>2</po:quantity>
> >>>       <po:USPrice>300</po:USPrice>
> >>>       <po:comment>Got twins!</po:comment>
> >>>       <po:shipDate>2001-12-17</po:shipDate>
> >>>     </po:item>
> >>>   </po:itemsList>
> >>> </xml-fragment>
> >>>
> >>> ===== test: xpath: ./po:purchaseOrder/po:shipTo/po:name =====
> >>>>> addNamespaces: prefix: po, uri: http://example.com/po
> >>>>> filter: class org.apache.xmlbeans.impl.values.XmlStringImpl,
> >>> <xml-fragment xmlns:po="http://example.com/po">Helen
> >>> Zoe</xml-fragment>
> >>>
> >>> ===== test: xpath: ./po:purchaseOrder/po:shipTo[po:name = 'Helen
> >>> Zoe']
> >>> =====
> >>>>> addNamespaces: prefix: po, uri: http://example.com/po
> >>>>> filter: class com.example.po.impl.USAddressImpl, <xml-fragment
> >>> type="USAddress" xmlns:po="http://example.com/po">
> >>>   <po:name>Helen Zoe</po:name>
> >>>   <po:street>47 Eden Street</po:street>
> >>>   <po:city>Santa Clara</po:city>
> >>>   <po:state>CA</po:state>
> >>>   <po:zip>95054</po:zip>
> >>> </xml-fragment>
> >>>
> >>> -----
> >>> po.xml
> >>>
> >>> ?xml version="1.0"?>
> >>> <po:purchaseOrder xmlns:po="http://example.com/po">
> >>>    <po:shipTo type="USAddress">
> >>>       <po:name>Helen Zoe</po:name>
> >>>       <po:street>47 Eden Street</po:street>
> >>>       <po:city>Santa Clara</po:city>
> >>>       <po:state>CA</po:state>
> >>>       <po:zip>95054</po:zip>
> >>>    </po:shipTo>
> >>>    <po:billTo type="USAddress">
> >>>       <po:name>Robert Zoe</po:name>
> >>>       <po:street>47 Eden Street</po:street>
> >>>       <po:city>Santa Clara</po:city>
> >>>       <po:state>CA</po:state>
> >>>       <po:zip>95054</po:zip>
> >>>    </po:billTo>
> >>>    <po:itemsList>
> >>>         <po:item partNum="833-AA">
> >>>             <po:productName>Lapis necklace</po:productName>
> >>>             <po:quantity>1</po:quantity>
> >>>             <po:USPrice>120</po:USPrice>
> >>>             <po:comment>Want this for the holidays!</po:comment>
> >>>             <po:shipDate>2001-12-15</po:shipDate>
> >>>         </po:item>
> >>>         <po:item partNum="423-AA">
> >>>             <po:productName>Baby Sound Monitor</po:productName>
> >>>             <po:quantity>2</po:quantity>
> >>>             <po:USPrice>300</po:USPrice>
> >>>             <po:comment>Got twins!</po:comment>
> >>>             <po:shipDate>2001-12-17</po:shipDate>
> >>>         </po:item>
> >>>     </po:itemsList>
> >>> </po:purchaseOrder>
> >>>
> >>> ----
> >>> po.xsd
> >>> <xsd:schema targetNamespace="http://example.com/po"
> >>>       xmlns="http://example.com/po"
> >>>       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
> >>>       elementFormDefault="qualified"
> >>>       attributeFormDefault="qualified">
> >>>
> >>>  <xsd:annotation>
> >>>   <xsd:documentation xml:lang="en">
> >>>    Purchase order schema for Example.com.
> >>>    Copyright 2000 Example.com. All rights reserved.
> >>>   </xsd:documentation>
> >>>  </xsd:annotation>
> >>>
> >>>  <xsd:element name="purchaseOrder" type="PurchaseOrderType"/>
> >>>
> >>>  <xsd:element name="comment" type="xsd:string"/>
> >>>
> >>>  <xsd:complexType name="PurchaseOrderType">
> >>>   <xsd:sequence>
> >>>    <xsd:element name="shipTo" type="USAddress"/>
> >>>    <xsd:element name="billTo" type="USAddress"/>
> >>>    <xsd:element ref="comment" minOccurs="0"/>
> >>>    <xsd:element name="itemsList"  type="Items"/>
> >>>   </xsd:sequence>
> >>>   <xsd:attribute name="orderDate" type="xsd:date"/>
> >>>  </xsd:complexType>
> >>>
> >>>  <xsd:complexType name="USAddress">
> >>>   <xsd:sequence>
> >>>    <xsd:element name="name"   type="xsd:string"/>
> >>>    <xsd:element name="street" type="xsd:string"/>
> >>>    <xsd:element name="city"   type="xsd:string"/>
> >>>    <xsd:element name="state"  type="xsd:string"/>
> >>>    <xsd:element name="zip"    type="xsd:decimal"/>
> >>>   </xsd:sequence>
> >>>   <xsd:attribute name="country" type="xsd:NMTOKEN"
> >>>      fixed="US"/>
> >>>  </xsd:complexType>
> >>>
> >>>  <xsd:complexType name="Items">
> >>>   <xsd:sequence>
> >>>    <xsd:element name="item" minOccurs="0" maxOccurs="unbounded">
> >>>     <xsd:complexType>
> >>>      <xsd:sequence>
> >>>       <xsd:element name="productName" type="xsd:string"/>
> >>>       <xsd:element name="quantity">
> >>>        <xsd:simpleType>
> >>>         <xsd:restriction base="xsd:positiveInteger">
> >>>          <xsd:maxExclusive value="100"/>
> >>>         </xsd:restriction>
> >>>        </xsd:simpleType>
> >>>       </xsd:element>
> >>>       <xsd:element name="USPrice"  type="xsd:decimal"/>
> >>>       <xsd:element ref="comment"   minOccurs="0"/>
> >>>       <xsd:element name="shipDate" type="xsd:date" minOccurs="0"/>
> >>>      </xsd:sequence>
> >>>      <xsd:attribute name="partNum" type="SKU" use="required"/>
> >>>     </xsd:complexType>
> >>>    </xsd:element>
> >>>   </xsd:sequence>
> >>>  </xsd:complexType>
> >>>
> >>>  <!-- Stock Keeping Unit, a code for identifying products -->
> >>>  <xsd:simpleType name="SKU">
> >>>   <xsd:restriction base="xsd:string">
> >>>    <xsd:pattern value="\d{3}-[A-Z]{2}"/>
> >>>   </xsd:restriction>
> >>>  </xsd:simpleType>
> >>>
> >>> </xsd:schema>
> >>>
> >>> ---
> >>> here is the code. I modified XBeansXPath.java and
> >>> XBeansXPathAdv.java,
> >>> and
> >>> added XBeansNamespace.java
> >>>
> >>> package org.apache.xmlbeans.impl.xpath.jaxen;
> >>> ...
> >>> public class XBeansXPath extends BaseXPath
> >>> {
> >>>     ....
> >>>     public List selectNodes(Object node) throws JaxenException
> >>>     {
> >>>         XmlCursor xc;
> >>>  XmlObject xmlObj;
> >>>         if (node instanceof XmlObject)
> >>>         {
> >>>      xmlObj = (XmlObject)node;
> >>>      xc = xmlObj.newCursor();
> >>>         }
> >>>         else if (node instanceof XmlCursor)
> >>>  {
> >>>      xc = (XmlCursor)node;
> >>>      xmlObj = xc.getObject();
> >>>             xc = xc.newCursor();
> >>>         }
> >>>         else
> >>>             throw new IllegalArgumentException("node must be an
> >>> XmlObject or
> >>> an XmlCursor, found: " + node.getClass());
> >>>
> >>>  XBeansNamespace.addNamespaces(this, xmlObj); // nn (support xpath
> >>> including
> >>> namespace)
> >>>         ((XBeansNavigator)getNavigator()).setCursor(xc);
> >>>         return new ListImpl(super.selectNodes(
> >>> XBeansNavigator.getBookmarkInThisPlace(xc) ));
> >>>     }
> >>> ....
> >>> }
> >>> ----
> >>> package org.apache.xmlbeans.impl.xpath.jaxen;
> >>> ...
> >>> public class XBeansXPathAdv
> >>>     extends BaseXPath
> >>>     implements JaxenXBeansDelegate.SelectPathInterface
> >>> {
> >>> ...
> >>>     public List selectNodes(Object node) throws JaxenException
> >>>     {
> >>>         XmlCursor xc;
> >>>         xc = ((XmlCursor)node);
> >>>  XmlObject xmlObj = xc.getObject();
> >>>  XBeansNamespace.addNamespaces(this, xmlObj);
> >>>         ((XBeansNavigator)getNavigator()).setCursor(xc);
> >>>         return super.selectNodes(
> >>> XBeansNavigator.getBookmarkInThisPlace(xc) );
> >>>     }
> >>> ...
> >>> }
> >>> -----
> >>> package org.apache.xmlbeans.impl.xpath.jaxen;
> >>> import org.jaxen.BaseXPath;
> >>> import org.jaxen.JaxenException;
> >>> import org.apache.xmlbeans.XmlObject;
> >>> import org.apache.xmlbeans.XmlCursor;
> >>>
> >>> import java.util.List;
> >>> import java.util.Map;
> >>> import java.util.HashMap;
> >>> import java.util.Iterator;
> >>>
> >>> public class XBeansNamespace
> >>> {
> >>>     public static void addNamespaces(BaseXPath baseXPath, XmlObject
> >>> doc)
> >>> throws JaxenException {
> >>>  HashMap nslist = getAdditionalNamespaces(getFirstChild(doc));
> >>>  for (Iterator iter = nslist.entrySet().iterator(); iter.hasNext();
> >>> ) {
> >>>      Map.Entry entry = (Map.Entry)iter.next();
> >>>      String prefix = (String)entry.getKey();
> >>>      String uri = (String)entry.getValue();
> >>>      System.out.println(">> addNamespaces: prefix: "+prefix+", uri:
> >>> "+uri);
> >>>      baseXPath.addNamespace(prefix, uri);
> >>>  }
> >>>     }
> >>>     private static XmlObject getFirstChild(XmlObject xmlObj) {
> >>>  XmlCursor xc = xmlObj.newCursor();
> >>>  try {
> >>>      xc.toFirstChild();
> >>>      return xc.getObject();
> >>>  } finally {
> >>>      xc.dispose();
> >>>  }
> >>>     }
> >>>     private static HashMap getAdditionalNamespaces(XmlObject xmlObj)
> >>> {
> >>>  HashMap nslist = new HashMap();
> >>>  XmlCursor xc = xmlObj.newCursor();
> >>>  try {
> >>>      xc.getAllNamespaces(nslist);
> >>>  } finally {
> >>>      xc.dispose();
> >>>  }
> >>>  return nslist;
> >>>     }
> >>> }
> >>>
> >>>
> >>>
> >>> -
> >>> ---------------------------------------------------------------------
> >>> To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
> >>> For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
> >>> Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/
> >>>
> >>
> >>
> >> -
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
> >> For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
> >> Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/
> >>
> >
> >
> > - ---------------------------------------------------------------------
> > To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
> > For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
> > Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/
> >
>
>
> - ---------------------------------------------------------------------
> To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
> Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/
>


- ---------------------------------------------------------------------
To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/


Re: bug fix for XPath using Namespace

Posted by nn <nn...@comcast.net>.
David,
> The specification says 'the context of the xpath expression', but
> doesn't define that. In XSLT, the context of an xpath expression is the
> location within the XSLT document that it is used - all namespaces
> declared in the element scope of the path attribute or text will
> provide prefix mappings for the expression. In code, that context needs
> to be given in some other manner.

Do you mean that if selectPath is used to implement XSLT, it must get
context information from the environment of the XPath? It may be a possible
case, but normally in XPath 1.0, it would be used only to access sub nodes
of target document, how other namespace outside of target document would be
relevent for it?
Can you give me a simple example to justify such a usage?

Anyway probably, it would be good idea to allows to use XPath in various
context.
but the most simple case, such namespace declaration is quite redendent.
So maybe it would be useful to provide an option to use the target namespace
as implicitly declared.

>
> The odd cases are the default namespace and nonprefixed elements, I
> can't tell you how those are supposed to work off the top of my head
> :-)  My assumption is that nonprefixed parts of an xpath expression
> always correspond to the default namespace of the target document; if
> you have a non-prefixed namespace declaration on a child element, your
> xpath needs to map that to a namespace (probably using a local prefix)
> to match.
this behavior would be achieved using my approach, and it seems useful.

nn


>
> -David Waite
>
> On Jun 18, 2004, at 10:18 AM, nn wrote:
>
> >
> >> Namespace prefixes are determined by the context of the XPath
> >> expression, not that of the 'target' document.
> >
> > How do you define the context of XPath?
> > Is the syntax of current XMLBeans a part of XPath specification?
> >
> > Are there any meaningfull case where the namespace which is not used
> > in the
> > target document is useful?
> > I think the most simple use case, using namespace associated with
> > target
> > document as default namespace would be the most convenient. This kind
> > of
> > behavior seems not part of standard, but a part of embeding system.
> >
> >>
> >> I have a patch that uses XmlOptions (I think setSaveSuggestedPrefixes)
> >> to supply this information into XmlBeans for XPath usage. I'm fine
> >> with
> >> submitting this patch sometime this weekend - would a 'fresh'
> >> XmlOptions option be preferred over setSaveSuggestedPrefixes and the
> >> others present already?
> >
> > Anyway, as long as this problem are removed soon, it's OK.
> > This aspect was crutial for me.
> >
> > nn
> >
> >>
> >> -David Waite
> >>
> >> On Jun 18, 2004, at 5:48 AM, nn wrote:
> >>
> >>> Hi,
> >>> I think it is important to remove the restriction for not allowing to
> >>> use
> >>> namespace in complex XPath expression.
> >>> (s.t ./po:purchaseOrder/po:shipTo[po:name = 'Helen Zoe']" ).
> >>>
> >>> Since otherwise, the useful XPath (which is provided by JAXEN) cannot
> >>> be
> >>> used for most XML data associated with XMLSchema. I thought this may
> >>> be a
> >>> restriction of JAXEN, but it wasn't.
> >>> This fix is relatively simple. I  hope this should be incorporated in
> >>> the
> >>> release (after reviewed by commiters and tested throughly).
> >>>
> >>> I'll attach the jar file.
> >>>
> >>> nn
> >>>
> >>> ------
> >>>
> >>> public class POTest {
> >>>     public static void filter(XmlObject[] result) throws Throwable {
> >>>         for (int i = 0; i < result.length; i++) {
> >>>      XmlObject obj = result[i];
> >>>      System.out.println(">> filter: "+obj.getClass()+", "+obj);
> >>>  }
> >>>     }
> >>>     private static String[] test_xpathes = new String[]{
> >>>   "//po:purchaseOrder",
> >>>   "./po:purchaseOrder/po:shipTo/po:name",
> >>>   "./po:purchaseOrder/po:shipTo[po:name = 'Helen Zoe']",
> >>>   };
> >>>
> >>>     public static void test(PurchaseOrderDocument xmlObj, String
> >>> xpath)
> >>> throws Throwable {
> >>>  System.out.println("\n===== test: xpath: "+xpath+" =====");
> >>>  XmlObject[] result = xmlObj.selectPath(xpath);
> >>>  filter(result);
> >>>     }
> >>>
> >>>     public static void main(String argv[]) {
> >>>  try {
> >>>      File sourceID = new File(argv[0]);
> >>>      PurchaseOrderDocument xmlObj =
> >>> PurchaseOrderDocument.Factory.parse(sourceID);
> >>>      for (int i = 0; i < test_xpathes.length; i++) {
> >>>   test(xmlObj, test_xpathes[i]);
> >>>      }
> >>>  } catch (Throwable err) {
> >>>      err.printStackTrace();
> >>>  }
> >>>     }
> >>> }
> >>> -----
> >>> the result:
> >>>
> >>> ===== test: xpath: //po:purchaseOrder =====
> >>>>> addNamespaces: prefix: po, uri: http://example.com/po
> >>>>> filter: class com.example.po.impl.PurchaseOrderTypeImpl,
> >>>>> <xml-fragment
> >>> xmlns:po="http://example.com/po">
> >>>   <po:shipTo type="USAddress">
> >>>     <po:name>Helen Zoe</po:name>
> >>>     <po:street>47 Eden Street</po:street>
> >>>     <po:city>Santa Clara</po:city>
> >>>     <po:state>CA</po:state>
> >>>     <po:zip>95054</po:zip>
> >>>   </po:shipTo>
> >>>   <po:billTo type="USAddress">
> >>>     <po:name>Robert Zoe</po:name>
> >>>     <po:street>47 Eden Street</po:street>
> >>>     <po:city>Santa Clara</po:city>
> >>>     <po:state>CA</po:state>
> >>>     <po:zip>95054</po:zip>
> >>>   </po:billTo>
> >>>   <po:itemsList>
> >>>     <po:item partNum="833-AA">
> >>>       <po:productName>Lapis necklace</po:productName>
> >>>       <po:quantity>1</po:quantity>
> >>>       <po:USPrice>120</po:USPrice>
> >>>       <po:comment>Want this for the holidays!</po:comment>
> >>>       <po:shipDate>2001-12-15</po:shipDate>
> >>>     </po:item>
> >>>     <po:item partNum="423-AA">
> >>>       <po:productName>Baby Sound Monitor</po:productName>
> >>>       <po:quantity>2</po:quantity>
> >>>       <po:USPrice>300</po:USPrice>
> >>>       <po:comment>Got twins!</po:comment>
> >>>       <po:shipDate>2001-12-17</po:shipDate>
> >>>     </po:item>
> >>>   </po:itemsList>
> >>> </xml-fragment>
> >>>
> >>> ===== test: xpath: ./po:purchaseOrder/po:shipTo/po:name =====
> >>>>> addNamespaces: prefix: po, uri: http://example.com/po
> >>>>> filter: class org.apache.xmlbeans.impl.values.XmlStringImpl,
> >>> <xml-fragment xmlns:po="http://example.com/po">Helen
> >>> Zoe</xml-fragment>
> >>>
> >>> ===== test: xpath: ./po:purchaseOrder/po:shipTo[po:name = 'Helen
> >>> Zoe']
> >>> =====
> >>>>> addNamespaces: prefix: po, uri: http://example.com/po
> >>>>> filter: class com.example.po.impl.USAddressImpl, <xml-fragment
> >>> type="USAddress" xmlns:po="http://example.com/po">
> >>>   <po:name>Helen Zoe</po:name>
> >>>   <po:street>47 Eden Street</po:street>
> >>>   <po:city>Santa Clara</po:city>
> >>>   <po:state>CA</po:state>
> >>>   <po:zip>95054</po:zip>
> >>> </xml-fragment>
> >>>
> >>> -----
> >>> po.xml
> >>>
> >>> ?xml version="1.0"?>
> >>> <po:purchaseOrder xmlns:po="http://example.com/po">
> >>>    <po:shipTo type="USAddress">
> >>>       <po:name>Helen Zoe</po:name>
> >>>       <po:street>47 Eden Street</po:street>
> >>>       <po:city>Santa Clara</po:city>
> >>>       <po:state>CA</po:state>
> >>>       <po:zip>95054</po:zip>
> >>>    </po:shipTo>
> >>>    <po:billTo type="USAddress">
> >>>       <po:name>Robert Zoe</po:name>
> >>>       <po:street>47 Eden Street</po:street>
> >>>       <po:city>Santa Clara</po:city>
> >>>       <po:state>CA</po:state>
> >>>       <po:zip>95054</po:zip>
> >>>    </po:billTo>
> >>>    <po:itemsList>
> >>>         <po:item partNum="833-AA">
> >>>             <po:productName>Lapis necklace</po:productName>
> >>>             <po:quantity>1</po:quantity>
> >>>             <po:USPrice>120</po:USPrice>
> >>>             <po:comment>Want this for the holidays!</po:comment>
> >>>             <po:shipDate>2001-12-15</po:shipDate>
> >>>         </po:item>
> >>>         <po:item partNum="423-AA">
> >>>             <po:productName>Baby Sound Monitor</po:productName>
> >>>             <po:quantity>2</po:quantity>
> >>>             <po:USPrice>300</po:USPrice>
> >>>             <po:comment>Got twins!</po:comment>
> >>>             <po:shipDate>2001-12-17</po:shipDate>
> >>>         </po:item>
> >>>     </po:itemsList>
> >>> </po:purchaseOrder>
> >>>
> >>> ----
> >>> po.xsd
> >>> <xsd:schema targetNamespace="http://example.com/po"
> >>>       xmlns="http://example.com/po"
> >>>       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
> >>>       elementFormDefault="qualified"
> >>>       attributeFormDefault="qualified">
> >>>
> >>>  <xsd:annotation>
> >>>   <xsd:documentation xml:lang="en">
> >>>    Purchase order schema for Example.com.
> >>>    Copyright 2000 Example.com. All rights reserved.
> >>>   </xsd:documentation>
> >>>  </xsd:annotation>
> >>>
> >>>  <xsd:element name="purchaseOrder" type="PurchaseOrderType"/>
> >>>
> >>>  <xsd:element name="comment" type="xsd:string"/>
> >>>
> >>>  <xsd:complexType name="PurchaseOrderType">
> >>>   <xsd:sequence>
> >>>    <xsd:element name="shipTo" type="USAddress"/>
> >>>    <xsd:element name="billTo" type="USAddress"/>
> >>>    <xsd:element ref="comment" minOccurs="0"/>
> >>>    <xsd:element name="itemsList"  type="Items"/>
> >>>   </xsd:sequence>
> >>>   <xsd:attribute name="orderDate" type="xsd:date"/>
> >>>  </xsd:complexType>
> >>>
> >>>  <xsd:complexType name="USAddress">
> >>>   <xsd:sequence>
> >>>    <xsd:element name="name"   type="xsd:string"/>
> >>>    <xsd:element name="street" type="xsd:string"/>
> >>>    <xsd:element name="city"   type="xsd:string"/>
> >>>    <xsd:element name="state"  type="xsd:string"/>
> >>>    <xsd:element name="zip"    type="xsd:decimal"/>
> >>>   </xsd:sequence>
> >>>   <xsd:attribute name="country" type="xsd:NMTOKEN"
> >>>      fixed="US"/>
> >>>  </xsd:complexType>
> >>>
> >>>  <xsd:complexType name="Items">
> >>>   <xsd:sequence>
> >>>    <xsd:element name="item" minOccurs="0" maxOccurs="unbounded">
> >>>     <xsd:complexType>
> >>>      <xsd:sequence>
> >>>       <xsd:element name="productName" type="xsd:string"/>
> >>>       <xsd:element name="quantity">
> >>>        <xsd:simpleType>
> >>>         <xsd:restriction base="xsd:positiveInteger">
> >>>          <xsd:maxExclusive value="100"/>
> >>>         </xsd:restriction>
> >>>        </xsd:simpleType>
> >>>       </xsd:element>
> >>>       <xsd:element name="USPrice"  type="xsd:decimal"/>
> >>>       <xsd:element ref="comment"   minOccurs="0"/>
> >>>       <xsd:element name="shipDate" type="xsd:date" minOccurs="0"/>
> >>>      </xsd:sequence>
> >>>      <xsd:attribute name="partNum" type="SKU" use="required"/>
> >>>     </xsd:complexType>
> >>>    </xsd:element>
> >>>   </xsd:sequence>
> >>>  </xsd:complexType>
> >>>
> >>>  <!-- Stock Keeping Unit, a code for identifying products -->
> >>>  <xsd:simpleType name="SKU">
> >>>   <xsd:restriction base="xsd:string">
> >>>    <xsd:pattern value="\d{3}-[A-Z]{2}"/>
> >>>   </xsd:restriction>
> >>>  </xsd:simpleType>
> >>>
> >>> </xsd:schema>
> >>>
> >>> ---
> >>> here is the code. I modified XBeansXPath.java and
> >>> XBeansXPathAdv.java,
> >>> and
> >>> added XBeansNamespace.java
> >>>
> >>> package org.apache.xmlbeans.impl.xpath.jaxen;
> >>> ...
> >>> public class XBeansXPath extends BaseXPath
> >>> {
> >>>     ....
> >>>     public List selectNodes(Object node) throws JaxenException
> >>>     {
> >>>         XmlCursor xc;
> >>>  XmlObject xmlObj;
> >>>         if (node instanceof XmlObject)
> >>>         {
> >>>      xmlObj = (XmlObject)node;
> >>>      xc = xmlObj.newCursor();
> >>>         }
> >>>         else if (node instanceof XmlCursor)
> >>>  {
> >>>      xc = (XmlCursor)node;
> >>>      xmlObj = xc.getObject();
> >>>             xc = xc.newCursor();
> >>>         }
> >>>         else
> >>>             throw new IllegalArgumentException("node must be an
> >>> XmlObject or
> >>> an XmlCursor, found: " + node.getClass());
> >>>
> >>>  XBeansNamespace.addNamespaces(this, xmlObj); // nn (support xpath
> >>> including
> >>> namespace)
> >>>         ((XBeansNavigator)getNavigator()).setCursor(xc);
> >>>         return new ListImpl(super.selectNodes(
> >>> XBeansNavigator.getBookmarkInThisPlace(xc) ));
> >>>     }
> >>> ....
> >>> }
> >>> ----
> >>> package org.apache.xmlbeans.impl.xpath.jaxen;
> >>> ...
> >>> public class XBeansXPathAdv
> >>>     extends BaseXPath
> >>>     implements JaxenXBeansDelegate.SelectPathInterface
> >>> {
> >>> ...
> >>>     public List selectNodes(Object node) throws JaxenException
> >>>     {
> >>>         XmlCursor xc;
> >>>         xc = ((XmlCursor)node);
> >>>  XmlObject xmlObj = xc.getObject();
> >>>  XBeansNamespace.addNamespaces(this, xmlObj);
> >>>         ((XBeansNavigator)getNavigator()).setCursor(xc);
> >>>         return super.selectNodes(
> >>> XBeansNavigator.getBookmarkInThisPlace(xc) );
> >>>     }
> >>> ...
> >>> }
> >>> -----
> >>> package org.apache.xmlbeans.impl.xpath.jaxen;
> >>> import org.jaxen.BaseXPath;
> >>> import org.jaxen.JaxenException;
> >>> import org.apache.xmlbeans.XmlObject;
> >>> import org.apache.xmlbeans.XmlCursor;
> >>>
> >>> import java.util.List;
> >>> import java.util.Map;
> >>> import java.util.HashMap;
> >>> import java.util.Iterator;
> >>>
> >>> public class XBeansNamespace
> >>> {
> >>>     public static void addNamespaces(BaseXPath baseXPath, XmlObject
> >>> doc)
> >>> throws JaxenException {
> >>>  HashMap nslist = getAdditionalNamespaces(getFirstChild(doc));
> >>>  for (Iterator iter = nslist.entrySet().iterator(); iter.hasNext();
> >>> ) {
> >>>      Map.Entry entry = (Map.Entry)iter.next();
> >>>      String prefix = (String)entry.getKey();
> >>>      String uri = (String)entry.getValue();
> >>>      System.out.println(">> addNamespaces: prefix: "+prefix+", uri:
> >>> "+uri);
> >>>      baseXPath.addNamespace(prefix, uri);
> >>>  }
> >>>     }
> >>>     private static XmlObject getFirstChild(XmlObject xmlObj) {
> >>>  XmlCursor xc = xmlObj.newCursor();
> >>>  try {
> >>>      xc.toFirstChild();
> >>>      return xc.getObject();
> >>>  } finally {
> >>>      xc.dispose();
> >>>  }
> >>>     }
> >>>     private static HashMap getAdditionalNamespaces(XmlObject xmlObj)
> >>> {
> >>>  HashMap nslist = new HashMap();
> >>>  XmlCursor xc = xmlObj.newCursor();
> >>>  try {
> >>>      xc.getAllNamespaces(nslist);
> >>>  } finally {
> >>>      xc.dispose();
> >>>  }
> >>>  return nslist;
> >>>     }
> >>> }
> >>>
> >>>
> >>>
> >>> -
> >>> ---------------------------------------------------------------------
> >>> To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
> >>> For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
> >>> Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/
> >>>
> >>
> >>
> >> -
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
> >> For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
> >> Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/
> >>
> >
> >
> > - ---------------------------------------------------------------------
> > To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
> > For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
> > Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/
> >
>
>
> - ---------------------------------------------------------------------
> To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
> Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/
>


- ---------------------------------------------------------------------
To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/


Re: bug fix for XPath using Namespace

Posted by David Waite <ma...@akuma.org>.
The specification says 'the context of the xpath expression', but  
doesn't define that. In XSLT, the context of an xpath expression is the  
location within the XSLT document that it is used - all namespaces  
declared in the element scope of the path attribute or text will  
provide prefix mappings for the expression. In code, that context needs  
to be given in some other manner.

The odd cases are the default namespace and nonprefixed elements, I  
can't tell you how those are supposed to work off the top of my head  
:-)  My assumption is that nonprefixed parts of an xpath expression  
always correspond to the default namespace of the target document; if  
you have a non-prefixed namespace declaration on a child element, your  
xpath needs to map that to a namespace (probably using a local prefix)  
to match.

-David Waite

On Jun 18, 2004, at 10:18 AM, nn wrote:

>
>> Namespace prefixes are determined by the context of the XPath
>> expression, not that of the 'target' document.
>
> How do you define the context of XPath?
> Is the syntax of current XMLBeans a part of XPath specification?
>
> Are there any meaningfull case where the namespace which is not used  
> in the
> target document is useful?
> I think the most simple use case, using namespace associated with  
> target
> document as default namespace would be the most convenient. This kind  
> of
> behavior seems not part of standard, but a part of embeding system.
>
>>
>> I have a patch that uses XmlOptions (I think setSaveSuggestedPrefixes)
>> to supply this information into XmlBeans for XPath usage. I'm fine  
>> with
>> submitting this patch sometime this weekend - would a 'fresh'
>> XmlOptions option be preferred over setSaveSuggestedPrefixes and the
>> others present already?
>
> Anyway, as long as this problem are removed soon, it's OK.
> This aspect was crutial for me.
>
> nn
>
>>
>> -David Waite
>>
>> On Jun 18, 2004, at 5:48 AM, nn wrote:
>>
>>> Hi,
>>> I think it is important to remove the restriction for not allowing to
>>> use
>>> namespace in complex XPath expression.
>>> (s.t ./po:purchaseOrder/po:shipTo[po:name = 'Helen Zoe']" ).
>>>
>>> Since otherwise, the useful XPath (which is provided by JAXEN) cannot
>>> be
>>> used for most XML data associated with XMLSchema. I thought this may
>>> be a
>>> restriction of JAXEN, but it wasn't.
>>> This fix is relatively simple. I  hope this should be incorporated in
>>> the
>>> release (after reviewed by commiters and tested throughly).
>>>
>>> I'll attach the jar file.
>>>
>>> nn
>>>
>>> ------
>>>
>>> public class POTest {
>>>     public static void filter(XmlObject[] result) throws Throwable {
>>>         for (int i = 0; i < result.length; i++) {
>>>      XmlObject obj = result[i];
>>>      System.out.println(">> filter: "+obj.getClass()+", "+obj);
>>>  }
>>>     }
>>>     private static String[] test_xpathes = new String[]{
>>>   "//po:purchaseOrder",
>>>   "./po:purchaseOrder/po:shipTo/po:name",
>>>   "./po:purchaseOrder/po:shipTo[po:name = 'Helen Zoe']",
>>>   };
>>>
>>>     public static void test(PurchaseOrderDocument xmlObj, String  
>>> xpath)
>>> throws Throwable {
>>>  System.out.println("\n===== test: xpath: "+xpath+" =====");
>>>  XmlObject[] result = xmlObj.selectPath(xpath);
>>>  filter(result);
>>>     }
>>>
>>>     public static void main(String argv[]) {
>>>  try {
>>>      File sourceID = new File(argv[0]);
>>>      PurchaseOrderDocument xmlObj =
>>> PurchaseOrderDocument.Factory.parse(sourceID);
>>>      for (int i = 0; i < test_xpathes.length; i++) {
>>>   test(xmlObj, test_xpathes[i]);
>>>      }
>>>  } catch (Throwable err) {
>>>      err.printStackTrace();
>>>  }
>>>     }
>>> }
>>> -----
>>> the result:
>>>
>>> ===== test: xpath: //po:purchaseOrder =====
>>>>> addNamespaces: prefix: po, uri: http://example.com/po
>>>>> filter: class com.example.po.impl.PurchaseOrderTypeImpl,
>>>>> <xml-fragment
>>> xmlns:po="http://example.com/po">
>>>   <po:shipTo type="USAddress">
>>>     <po:name>Helen Zoe</po:name>
>>>     <po:street>47 Eden Street</po:street>
>>>     <po:city>Santa Clara</po:city>
>>>     <po:state>CA</po:state>
>>>     <po:zip>95054</po:zip>
>>>   </po:shipTo>
>>>   <po:billTo type="USAddress">
>>>     <po:name>Robert Zoe</po:name>
>>>     <po:street>47 Eden Street</po:street>
>>>     <po:city>Santa Clara</po:city>
>>>     <po:state>CA</po:state>
>>>     <po:zip>95054</po:zip>
>>>   </po:billTo>
>>>   <po:itemsList>
>>>     <po:item partNum="833-AA">
>>>       <po:productName>Lapis necklace</po:productName>
>>>       <po:quantity>1</po:quantity>
>>>       <po:USPrice>120</po:USPrice>
>>>       <po:comment>Want this for the holidays!</po:comment>
>>>       <po:shipDate>2001-12-15</po:shipDate>
>>>     </po:item>
>>>     <po:item partNum="423-AA">
>>>       <po:productName>Baby Sound Monitor</po:productName>
>>>       <po:quantity>2</po:quantity>
>>>       <po:USPrice>300</po:USPrice>
>>>       <po:comment>Got twins!</po:comment>
>>>       <po:shipDate>2001-12-17</po:shipDate>
>>>     </po:item>
>>>   </po:itemsList>
>>> </xml-fragment>
>>>
>>> ===== test: xpath: ./po:purchaseOrder/po:shipTo/po:name =====
>>>>> addNamespaces: prefix: po, uri: http://example.com/po
>>>>> filter: class org.apache.xmlbeans.impl.values.XmlStringImpl,
>>> <xml-fragment xmlns:po="http://example.com/po">Helen  
>>> Zoe</xml-fragment>
>>>
>>> ===== test: xpath: ./po:purchaseOrder/po:shipTo[po:name = 'Helen  
>>> Zoe']
>>> =====
>>>>> addNamespaces: prefix: po, uri: http://example.com/po
>>>>> filter: class com.example.po.impl.USAddressImpl, <xml-fragment
>>> type="USAddress" xmlns:po="http://example.com/po">
>>>   <po:name>Helen Zoe</po:name>
>>>   <po:street>47 Eden Street</po:street>
>>>   <po:city>Santa Clara</po:city>
>>>   <po:state>CA</po:state>
>>>   <po:zip>95054</po:zip>
>>> </xml-fragment>
>>>
>>> -----
>>> po.xml
>>>
>>> ?xml version="1.0"?>
>>> <po:purchaseOrder xmlns:po="http://example.com/po">
>>>    <po:shipTo type="USAddress">
>>>       <po:name>Helen Zoe</po:name>
>>>       <po:street>47 Eden Street</po:street>
>>>       <po:city>Santa Clara</po:city>
>>>       <po:state>CA</po:state>
>>>       <po:zip>95054</po:zip>
>>>    </po:shipTo>
>>>    <po:billTo type="USAddress">
>>>       <po:name>Robert Zoe</po:name>
>>>       <po:street>47 Eden Street</po:street>
>>>       <po:city>Santa Clara</po:city>
>>>       <po:state>CA</po:state>
>>>       <po:zip>95054</po:zip>
>>>    </po:billTo>
>>>    <po:itemsList>
>>>         <po:item partNum="833-AA">
>>>             <po:productName>Lapis necklace</po:productName>
>>>             <po:quantity>1</po:quantity>
>>>             <po:USPrice>120</po:USPrice>
>>>             <po:comment>Want this for the holidays!</po:comment>
>>>             <po:shipDate>2001-12-15</po:shipDate>
>>>         </po:item>
>>>         <po:item partNum="423-AA">
>>>             <po:productName>Baby Sound Monitor</po:productName>
>>>             <po:quantity>2</po:quantity>
>>>             <po:USPrice>300</po:USPrice>
>>>             <po:comment>Got twins!</po:comment>
>>>             <po:shipDate>2001-12-17</po:shipDate>
>>>         </po:item>
>>>     </po:itemsList>
>>> </po:purchaseOrder>
>>>
>>> ----
>>> po.xsd
>>> <xsd:schema targetNamespace="http://example.com/po"
>>>       xmlns="http://example.com/po"
>>>       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
>>>       elementFormDefault="qualified"
>>>       attributeFormDefault="qualified">
>>>
>>>  <xsd:annotation>
>>>   <xsd:documentation xml:lang="en">
>>>    Purchase order schema for Example.com.
>>>    Copyright 2000 Example.com. All rights reserved.
>>>   </xsd:documentation>
>>>  </xsd:annotation>
>>>
>>>  <xsd:element name="purchaseOrder" type="PurchaseOrderType"/>
>>>
>>>  <xsd:element name="comment" type="xsd:string"/>
>>>
>>>  <xsd:complexType name="PurchaseOrderType">
>>>   <xsd:sequence>
>>>    <xsd:element name="shipTo" type="USAddress"/>
>>>    <xsd:element name="billTo" type="USAddress"/>
>>>    <xsd:element ref="comment" minOccurs="0"/>
>>>    <xsd:element name="itemsList"  type="Items"/>
>>>   </xsd:sequence>
>>>   <xsd:attribute name="orderDate" type="xsd:date"/>
>>>  </xsd:complexType>
>>>
>>>  <xsd:complexType name="USAddress">
>>>   <xsd:sequence>
>>>    <xsd:element name="name"   type="xsd:string"/>
>>>    <xsd:element name="street" type="xsd:string"/>
>>>    <xsd:element name="city"   type="xsd:string"/>
>>>    <xsd:element name="state"  type="xsd:string"/>
>>>    <xsd:element name="zip"    type="xsd:decimal"/>
>>>   </xsd:sequence>
>>>   <xsd:attribute name="country" type="xsd:NMTOKEN"
>>>      fixed="US"/>
>>>  </xsd:complexType>
>>>
>>>  <xsd:complexType name="Items">
>>>   <xsd:sequence>
>>>    <xsd:element name="item" minOccurs="0" maxOccurs="unbounded">
>>>     <xsd:complexType>
>>>      <xsd:sequence>
>>>       <xsd:element name="productName" type="xsd:string"/>
>>>       <xsd:element name="quantity">
>>>        <xsd:simpleType>
>>>         <xsd:restriction base="xsd:positiveInteger">
>>>          <xsd:maxExclusive value="100"/>
>>>         </xsd:restriction>
>>>        </xsd:simpleType>
>>>       </xsd:element>
>>>       <xsd:element name="USPrice"  type="xsd:decimal"/>
>>>       <xsd:element ref="comment"   minOccurs="0"/>
>>>       <xsd:element name="shipDate" type="xsd:date" minOccurs="0"/>
>>>      </xsd:sequence>
>>>      <xsd:attribute name="partNum" type="SKU" use="required"/>
>>>     </xsd:complexType>
>>>    </xsd:element>
>>>   </xsd:sequence>
>>>  </xsd:complexType>
>>>
>>>  <!-- Stock Keeping Unit, a code for identifying products -->
>>>  <xsd:simpleType name="SKU">
>>>   <xsd:restriction base="xsd:string">
>>>    <xsd:pattern value="\d{3}-[A-Z]{2}"/>
>>>   </xsd:restriction>
>>>  </xsd:simpleType>
>>>
>>> </xsd:schema>
>>>
>>> ---
>>> here is the code. I modified XBeansXPath.java and  
>>> XBeansXPathAdv.java,
>>> and
>>> added XBeansNamespace.java
>>>
>>> package org.apache.xmlbeans.impl.xpath.jaxen;
>>> ...
>>> public class XBeansXPath extends BaseXPath
>>> {
>>>     ....
>>>     public List selectNodes(Object node) throws JaxenException
>>>     {
>>>         XmlCursor xc;
>>>  XmlObject xmlObj;
>>>         if (node instanceof XmlObject)
>>>         {
>>>      xmlObj = (XmlObject)node;
>>>      xc = xmlObj.newCursor();
>>>         }
>>>         else if (node instanceof XmlCursor)
>>>  {
>>>      xc = (XmlCursor)node;
>>>      xmlObj = xc.getObject();
>>>             xc = xc.newCursor();
>>>         }
>>>         else
>>>             throw new IllegalArgumentException("node must be an
>>> XmlObject or
>>> an XmlCursor, found: " + node.getClass());
>>>
>>>  XBeansNamespace.addNamespaces(this, xmlObj); // nn (support xpath
>>> including
>>> namespace)
>>>         ((XBeansNavigator)getNavigator()).setCursor(xc);
>>>         return new ListImpl(super.selectNodes(
>>> XBeansNavigator.getBookmarkInThisPlace(xc) ));
>>>     }
>>> ....
>>> }
>>> ----
>>> package org.apache.xmlbeans.impl.xpath.jaxen;
>>> ...
>>> public class XBeansXPathAdv
>>>     extends BaseXPath
>>>     implements JaxenXBeansDelegate.SelectPathInterface
>>> {
>>> ...
>>>     public List selectNodes(Object node) throws JaxenException
>>>     {
>>>         XmlCursor xc;
>>>         xc = ((XmlCursor)node);
>>>  XmlObject xmlObj = xc.getObject();
>>>  XBeansNamespace.addNamespaces(this, xmlObj);
>>>         ((XBeansNavigator)getNavigator()).setCursor(xc);
>>>         return super.selectNodes(
>>> XBeansNavigator.getBookmarkInThisPlace(xc) );
>>>     }
>>> ...
>>> }
>>> -----
>>> package org.apache.xmlbeans.impl.xpath.jaxen;
>>> import org.jaxen.BaseXPath;
>>> import org.jaxen.JaxenException;
>>> import org.apache.xmlbeans.XmlObject;
>>> import org.apache.xmlbeans.XmlCursor;
>>>
>>> import java.util.List;
>>> import java.util.Map;
>>> import java.util.HashMap;
>>> import java.util.Iterator;
>>>
>>> public class XBeansNamespace
>>> {
>>>     public static void addNamespaces(BaseXPath baseXPath, XmlObject
>>> doc)
>>> throws JaxenException {
>>>  HashMap nslist = getAdditionalNamespaces(getFirstChild(doc));
>>>  for (Iterator iter = nslist.entrySet().iterator(); iter.hasNext();  
>>> ) {
>>>      Map.Entry entry = (Map.Entry)iter.next();
>>>      String prefix = (String)entry.getKey();
>>>      String uri = (String)entry.getValue();
>>>      System.out.println(">> addNamespaces: prefix: "+prefix+", uri:
>>> "+uri);
>>>      baseXPath.addNamespace(prefix, uri);
>>>  }
>>>     }
>>>     private static XmlObject getFirstChild(XmlObject xmlObj) {
>>>  XmlCursor xc = xmlObj.newCursor();
>>>  try {
>>>      xc.toFirstChild();
>>>      return xc.getObject();
>>>  } finally {
>>>      xc.dispose();
>>>  }
>>>     }
>>>     private static HashMap getAdditionalNamespaces(XmlObject xmlObj)  
>>> {
>>>  HashMap nslist = new HashMap();
>>>  XmlCursor xc = xmlObj.newCursor();
>>>  try {
>>>      xc.getAllNamespaces(nslist);
>>>  } finally {
>>>      xc.dispose();
>>>  }
>>>  return nslist;
>>>     }
>>> }
>>>
>>>
>>>
>>> -  
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
>>> For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
>>> Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/
>>>
>>
>>
>> -  
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
>> For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
>> Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/
>>
>
>
> - ---------------------------------------------------------------------
> To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
> Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/
>


- ---------------------------------------------------------------------
To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/


Re: bug fix for XPath using Namespace

Posted by David Waite <ma...@akuma.org>.
The specification says 'the context of the xpath expression', but  
doesn't define that. In XSLT, the context of an xpath expression is the  
location within the XSLT document that it is used - all namespaces  
declared in the element scope of the path attribute or text will  
provide prefix mappings for the expression. In code, that context needs  
to be given in some other manner.

The odd cases are the default namespace and nonprefixed elements, I  
can't tell you how those are supposed to work off the top of my head  
:-)  My assumption is that nonprefixed parts of an xpath expression  
always correspond to the default namespace of the target document; if  
you have a non-prefixed namespace declaration on a child element, your  
xpath needs to map that to a namespace (probably using a local prefix)  
to match.

-David Waite

On Jun 18, 2004, at 10:18 AM, nn wrote:

>
>> Namespace prefixes are determined by the context of the XPath
>> expression, not that of the 'target' document.
>
> How do you define the context of XPath?
> Is the syntax of current XMLBeans a part of XPath specification?
>
> Are there any meaningfull case where the namespace which is not used  
> in the
> target document is useful?
> I think the most simple use case, using namespace associated with  
> target
> document as default namespace would be the most convenient. This kind  
> of
> behavior seems not part of standard, but a part of embeding system.
>
>>
>> I have a patch that uses XmlOptions (I think setSaveSuggestedPrefixes)
>> to supply this information into XmlBeans for XPath usage. I'm fine  
>> with
>> submitting this patch sometime this weekend - would a 'fresh'
>> XmlOptions option be preferred over setSaveSuggestedPrefixes and the
>> others present already?
>
> Anyway, as long as this problem are removed soon, it's OK.
> This aspect was crutial for me.
>
> nn
>
>>
>> -David Waite
>>
>> On Jun 18, 2004, at 5:48 AM, nn wrote:
>>
>>> Hi,
>>> I think it is important to remove the restriction for not allowing to
>>> use
>>> namespace in complex XPath expression.
>>> (s.t ./po:purchaseOrder/po:shipTo[po:name = 'Helen Zoe']" ).
>>>
>>> Since otherwise, the useful XPath (which is provided by JAXEN) cannot
>>> be
>>> used for most XML data associated with XMLSchema. I thought this may
>>> be a
>>> restriction of JAXEN, but it wasn't.
>>> This fix is relatively simple. I  hope this should be incorporated in
>>> the
>>> release (after reviewed by commiters and tested throughly).
>>>
>>> I'll attach the jar file.
>>>
>>> nn
>>>
>>> ------
>>>
>>> public class POTest {
>>>     public static void filter(XmlObject[] result) throws Throwable {
>>>         for (int i = 0; i < result.length; i++) {
>>>      XmlObject obj = result[i];
>>>      System.out.println(">> filter: "+obj.getClass()+", "+obj);
>>>  }
>>>     }
>>>     private static String[] test_xpathes = new String[]{
>>>   "//po:purchaseOrder",
>>>   "./po:purchaseOrder/po:shipTo/po:name",
>>>   "./po:purchaseOrder/po:shipTo[po:name = 'Helen Zoe']",
>>>   };
>>>
>>>     public static void test(PurchaseOrderDocument xmlObj, String  
>>> xpath)
>>> throws Throwable {
>>>  System.out.println("\n===== test: xpath: "+xpath+" =====");
>>>  XmlObject[] result = xmlObj.selectPath(xpath);
>>>  filter(result);
>>>     }
>>>
>>>     public static void main(String argv[]) {
>>>  try {
>>>      File sourceID = new File(argv[0]);
>>>      PurchaseOrderDocument xmlObj =
>>> PurchaseOrderDocument.Factory.parse(sourceID);
>>>      for (int i = 0; i < test_xpathes.length; i++) {
>>>   test(xmlObj, test_xpathes[i]);
>>>      }
>>>  } catch (Throwable err) {
>>>      err.printStackTrace();
>>>  }
>>>     }
>>> }
>>> -----
>>> the result:
>>>
>>> ===== test: xpath: //po:purchaseOrder =====
>>>>> addNamespaces: prefix: po, uri: http://example.com/po
>>>>> filter: class com.example.po.impl.PurchaseOrderTypeImpl,
>>>>> <xml-fragment
>>> xmlns:po="http://example.com/po">
>>>   <po:shipTo type="USAddress">
>>>     <po:name>Helen Zoe</po:name>
>>>     <po:street>47 Eden Street</po:street>
>>>     <po:city>Santa Clara</po:city>
>>>     <po:state>CA</po:state>
>>>     <po:zip>95054</po:zip>
>>>   </po:shipTo>
>>>   <po:billTo type="USAddress">
>>>     <po:name>Robert Zoe</po:name>
>>>     <po:street>47 Eden Street</po:street>
>>>     <po:city>Santa Clara</po:city>
>>>     <po:state>CA</po:state>
>>>     <po:zip>95054</po:zip>
>>>   </po:billTo>
>>>   <po:itemsList>
>>>     <po:item partNum="833-AA">
>>>       <po:productName>Lapis necklace</po:productName>
>>>       <po:quantity>1</po:quantity>
>>>       <po:USPrice>120</po:USPrice>
>>>       <po:comment>Want this for the holidays!</po:comment>
>>>       <po:shipDate>2001-12-15</po:shipDate>
>>>     </po:item>
>>>     <po:item partNum="423-AA">
>>>       <po:productName>Baby Sound Monitor</po:productName>
>>>       <po:quantity>2</po:quantity>
>>>       <po:USPrice>300</po:USPrice>
>>>       <po:comment>Got twins!</po:comment>
>>>       <po:shipDate>2001-12-17</po:shipDate>
>>>     </po:item>
>>>   </po:itemsList>
>>> </xml-fragment>
>>>
>>> ===== test: xpath: ./po:purchaseOrder/po:shipTo/po:name =====
>>>>> addNamespaces: prefix: po, uri: http://example.com/po
>>>>> filter: class org.apache.xmlbeans.impl.values.XmlStringImpl,
>>> <xml-fragment xmlns:po="http://example.com/po">Helen  
>>> Zoe</xml-fragment>
>>>
>>> ===== test: xpath: ./po:purchaseOrder/po:shipTo[po:name = 'Helen  
>>> Zoe']
>>> =====
>>>>> addNamespaces: prefix: po, uri: http://example.com/po
>>>>> filter: class com.example.po.impl.USAddressImpl, <xml-fragment
>>> type="USAddress" xmlns:po="http://example.com/po">
>>>   <po:name>Helen Zoe</po:name>
>>>   <po:street>47 Eden Street</po:street>
>>>   <po:city>Santa Clara</po:city>
>>>   <po:state>CA</po:state>
>>>   <po:zip>95054</po:zip>
>>> </xml-fragment>
>>>
>>> -----
>>> po.xml
>>>
>>> ?xml version="1.0"?>
>>> <po:purchaseOrder xmlns:po="http://example.com/po">
>>>    <po:shipTo type="USAddress">
>>>       <po:name>Helen Zoe</po:name>
>>>       <po:street>47 Eden Street</po:street>
>>>       <po:city>Santa Clara</po:city>
>>>       <po:state>CA</po:state>
>>>       <po:zip>95054</po:zip>
>>>    </po:shipTo>
>>>    <po:billTo type="USAddress">
>>>       <po:name>Robert Zoe</po:name>
>>>       <po:street>47 Eden Street</po:street>
>>>       <po:city>Santa Clara</po:city>
>>>       <po:state>CA</po:state>
>>>       <po:zip>95054</po:zip>
>>>    </po:billTo>
>>>    <po:itemsList>
>>>         <po:item partNum="833-AA">
>>>             <po:productName>Lapis necklace</po:productName>
>>>             <po:quantity>1</po:quantity>
>>>             <po:USPrice>120</po:USPrice>
>>>             <po:comment>Want this for the holidays!</po:comment>
>>>             <po:shipDate>2001-12-15</po:shipDate>
>>>         </po:item>
>>>         <po:item partNum="423-AA">
>>>             <po:productName>Baby Sound Monitor</po:productName>
>>>             <po:quantity>2</po:quantity>
>>>             <po:USPrice>300</po:USPrice>
>>>             <po:comment>Got twins!</po:comment>
>>>             <po:shipDate>2001-12-17</po:shipDate>
>>>         </po:item>
>>>     </po:itemsList>
>>> </po:purchaseOrder>
>>>
>>> ----
>>> po.xsd
>>> <xsd:schema targetNamespace="http://example.com/po"
>>>       xmlns="http://example.com/po"
>>>       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
>>>       elementFormDefault="qualified"
>>>       attributeFormDefault="qualified">
>>>
>>>  <xsd:annotation>
>>>   <xsd:documentation xml:lang="en">
>>>    Purchase order schema for Example.com.
>>>    Copyright 2000 Example.com. All rights reserved.
>>>   </xsd:documentation>
>>>  </xsd:annotation>
>>>
>>>  <xsd:element name="purchaseOrder" type="PurchaseOrderType"/>
>>>
>>>  <xsd:element name="comment" type="xsd:string"/>
>>>
>>>  <xsd:complexType name="PurchaseOrderType">
>>>   <xsd:sequence>
>>>    <xsd:element name="shipTo" type="USAddress"/>
>>>    <xsd:element name="billTo" type="USAddress"/>
>>>    <xsd:element ref="comment" minOccurs="0"/>
>>>    <xsd:element name="itemsList"  type="Items"/>
>>>   </xsd:sequence>
>>>   <xsd:attribute name="orderDate" type="xsd:date"/>
>>>  </xsd:complexType>
>>>
>>>  <xsd:complexType name="USAddress">
>>>   <xsd:sequence>
>>>    <xsd:element name="name"   type="xsd:string"/>
>>>    <xsd:element name="street" type="xsd:string"/>
>>>    <xsd:element name="city"   type="xsd:string"/>
>>>    <xsd:element name="state"  type="xsd:string"/>
>>>    <xsd:element name="zip"    type="xsd:decimal"/>
>>>   </xsd:sequence>
>>>   <xsd:attribute name="country" type="xsd:NMTOKEN"
>>>      fixed="US"/>
>>>  </xsd:complexType>
>>>
>>>  <xsd:complexType name="Items">
>>>   <xsd:sequence>
>>>    <xsd:element name="item" minOccurs="0" maxOccurs="unbounded">
>>>     <xsd:complexType>
>>>      <xsd:sequence>
>>>       <xsd:element name="productName" type="xsd:string"/>
>>>       <xsd:element name="quantity">
>>>        <xsd:simpleType>
>>>         <xsd:restriction base="xsd:positiveInteger">
>>>          <xsd:maxExclusive value="100"/>
>>>         </xsd:restriction>
>>>        </xsd:simpleType>
>>>       </xsd:element>
>>>       <xsd:element name="USPrice"  type="xsd:decimal"/>
>>>       <xsd:element ref="comment"   minOccurs="0"/>
>>>       <xsd:element name="shipDate" type="xsd:date" minOccurs="0"/>
>>>      </xsd:sequence>
>>>      <xsd:attribute name="partNum" type="SKU" use="required"/>
>>>     </xsd:complexType>
>>>    </xsd:element>
>>>   </xsd:sequence>
>>>  </xsd:complexType>
>>>
>>>  <!-- Stock Keeping Unit, a code for identifying products -->
>>>  <xsd:simpleType name="SKU">
>>>   <xsd:restriction base="xsd:string">
>>>    <xsd:pattern value="\d{3}-[A-Z]{2}"/>
>>>   </xsd:restriction>
>>>  </xsd:simpleType>
>>>
>>> </xsd:schema>
>>>
>>> ---
>>> here is the code. I modified XBeansXPath.java and  
>>> XBeansXPathAdv.java,
>>> and
>>> added XBeansNamespace.java
>>>
>>> package org.apache.xmlbeans.impl.xpath.jaxen;
>>> ...
>>> public class XBeansXPath extends BaseXPath
>>> {
>>>     ....
>>>     public List selectNodes(Object node) throws JaxenException
>>>     {
>>>         XmlCursor xc;
>>>  XmlObject xmlObj;
>>>         if (node instanceof XmlObject)
>>>         {
>>>      xmlObj = (XmlObject)node;
>>>      xc = xmlObj.newCursor();
>>>         }
>>>         else if (node instanceof XmlCursor)
>>>  {
>>>      xc = (XmlCursor)node;
>>>      xmlObj = xc.getObject();
>>>             xc = xc.newCursor();
>>>         }
>>>         else
>>>             throw new IllegalArgumentException("node must be an
>>> XmlObject or
>>> an XmlCursor, found: " + node.getClass());
>>>
>>>  XBeansNamespace.addNamespaces(this, xmlObj); // nn (support xpath
>>> including
>>> namespace)
>>>         ((XBeansNavigator)getNavigator()).setCursor(xc);
>>>         return new ListImpl(super.selectNodes(
>>> XBeansNavigator.getBookmarkInThisPlace(xc) ));
>>>     }
>>> ....
>>> }
>>> ----
>>> package org.apache.xmlbeans.impl.xpath.jaxen;
>>> ...
>>> public class XBeansXPathAdv
>>>     extends BaseXPath
>>>     implements JaxenXBeansDelegate.SelectPathInterface
>>> {
>>> ...
>>>     public List selectNodes(Object node) throws JaxenException
>>>     {
>>>         XmlCursor xc;
>>>         xc = ((XmlCursor)node);
>>>  XmlObject xmlObj = xc.getObject();
>>>  XBeansNamespace.addNamespaces(this, xmlObj);
>>>         ((XBeansNavigator)getNavigator()).setCursor(xc);
>>>         return super.selectNodes(
>>> XBeansNavigator.getBookmarkInThisPlace(xc) );
>>>     }
>>> ...
>>> }
>>> -----
>>> package org.apache.xmlbeans.impl.xpath.jaxen;
>>> import org.jaxen.BaseXPath;
>>> import org.jaxen.JaxenException;
>>> import org.apache.xmlbeans.XmlObject;
>>> import org.apache.xmlbeans.XmlCursor;
>>>
>>> import java.util.List;
>>> import java.util.Map;
>>> import java.util.HashMap;
>>> import java.util.Iterator;
>>>
>>> public class XBeansNamespace
>>> {
>>>     public static void addNamespaces(BaseXPath baseXPath, XmlObject
>>> doc)
>>> throws JaxenException {
>>>  HashMap nslist = getAdditionalNamespaces(getFirstChild(doc));
>>>  for (Iterator iter = nslist.entrySet().iterator(); iter.hasNext();  
>>> ) {
>>>      Map.Entry entry = (Map.Entry)iter.next();
>>>      String prefix = (String)entry.getKey();
>>>      String uri = (String)entry.getValue();
>>>      System.out.println(">> addNamespaces: prefix: "+prefix+", uri:
>>> "+uri);
>>>      baseXPath.addNamespace(prefix, uri);
>>>  }
>>>     }
>>>     private static XmlObject getFirstChild(XmlObject xmlObj) {
>>>  XmlCursor xc = xmlObj.newCursor();
>>>  try {
>>>      xc.toFirstChild();
>>>      return xc.getObject();
>>>  } finally {
>>>      xc.dispose();
>>>  }
>>>     }
>>>     private static HashMap getAdditionalNamespaces(XmlObject xmlObj)  
>>> {
>>>  HashMap nslist = new HashMap();
>>>  XmlCursor xc = xmlObj.newCursor();
>>>  try {
>>>      xc.getAllNamespaces(nslist);
>>>  } finally {
>>>      xc.dispose();
>>>  }
>>>  return nslist;
>>>     }
>>> }
>>>
>>>
>>>
>>> -  
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
>>> For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
>>> Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/
>>>
>>
>>
>> -  
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
>> For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
>> Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/
>>
>
>
> - ---------------------------------------------------------------------
> To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
> Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/
>


- ---------------------------------------------------------------------
To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/


Re: bug fix for XPath using Namespace

Posted by nn <nn...@comcast.net>.
> Namespace prefixes are determined by the context of the XPath
> expression, not that of the 'target' document.

How do you define the context of XPath?
Is the syntax of current XMLBeans a part of XPath specification?

Are there any meaningfull case where the namespace which is not used in the
target document is useful?
I think the most simple use case, using namespace associated with target
document as default namespace would be the most convenient. This kind of
behavior seems not part of standard, but a part of embeding system.

>
> I have a patch that uses XmlOptions (I think setSaveSuggestedPrefixes)
> to supply this information into XmlBeans for XPath usage. I'm fine with
> submitting this patch sometime this weekend - would a 'fresh'
> XmlOptions option be preferred over setSaveSuggestedPrefixes and the
> others present already?

Anyway, as long as this problem are removed soon, it's OK.
This aspect was crutial for me.

nn

>
> -David Waite
>
> On Jun 18, 2004, at 5:48 AM, nn wrote:
>
> > Hi,
> > I think it is important to remove the restriction for not allowing to
> > use
> > namespace in complex XPath expression.
> > (s.t ./po:purchaseOrder/po:shipTo[po:name = 'Helen Zoe']" ).
> >
> > Since otherwise, the useful XPath (which is provided by JAXEN) cannot
> > be
> > used for most XML data associated with XMLSchema. I thought this may
> > be a
> > restriction of JAXEN, but it wasn't.
> > This fix is relatively simple. I  hope this should be incorporated in
> > the
> > release (after reviewed by commiters and tested throughly).
> >
> > I'll attach the jar file.
> >
> > nn
> >
> > ------
> >
> > public class POTest {
> >     public static void filter(XmlObject[] result) throws Throwable {
> >         for (int i = 0; i < result.length; i++) {
> >      XmlObject obj = result[i];
> >      System.out.println(">> filter: "+obj.getClass()+", "+obj);
> >  }
> >     }
> >     private static String[] test_xpathes = new String[]{
> >   "//po:purchaseOrder",
> >   "./po:purchaseOrder/po:shipTo/po:name",
> >   "./po:purchaseOrder/po:shipTo[po:name = 'Helen Zoe']",
> >   };
> >
> >     public static void test(PurchaseOrderDocument xmlObj, String xpath)
> > throws Throwable {
> >  System.out.println("\n===== test: xpath: "+xpath+" =====");
> >  XmlObject[] result = xmlObj.selectPath(xpath);
> >  filter(result);
> >     }
> >
> >     public static void main(String argv[]) {
> >  try {
> >      File sourceID = new File(argv[0]);
> >      PurchaseOrderDocument xmlObj =
> > PurchaseOrderDocument.Factory.parse(sourceID);
> >      for (int i = 0; i < test_xpathes.length; i++) {
> >   test(xmlObj, test_xpathes[i]);
> >      }
> >  } catch (Throwable err) {
> >      err.printStackTrace();
> >  }
> >     }
> > }
> > -----
> > the result:
> >
> > ===== test: xpath: //po:purchaseOrder =====
> >>> addNamespaces: prefix: po, uri: http://example.com/po
> >>> filter: class com.example.po.impl.PurchaseOrderTypeImpl,
> >>> <xml-fragment
> > xmlns:po="http://example.com/po">
> >   <po:shipTo type="USAddress">
> >     <po:name>Helen Zoe</po:name>
> >     <po:street>47 Eden Street</po:street>
> >     <po:city>Santa Clara</po:city>
> >     <po:state>CA</po:state>
> >     <po:zip>95054</po:zip>
> >   </po:shipTo>
> >   <po:billTo type="USAddress">
> >     <po:name>Robert Zoe</po:name>
> >     <po:street>47 Eden Street</po:street>
> >     <po:city>Santa Clara</po:city>
> >     <po:state>CA</po:state>
> >     <po:zip>95054</po:zip>
> >   </po:billTo>
> >   <po:itemsList>
> >     <po:item partNum="833-AA">
> >       <po:productName>Lapis necklace</po:productName>
> >       <po:quantity>1</po:quantity>
> >       <po:USPrice>120</po:USPrice>
> >       <po:comment>Want this for the holidays!</po:comment>
> >       <po:shipDate>2001-12-15</po:shipDate>
> >     </po:item>
> >     <po:item partNum="423-AA">
> >       <po:productName>Baby Sound Monitor</po:productName>
> >       <po:quantity>2</po:quantity>
> >       <po:USPrice>300</po:USPrice>
> >       <po:comment>Got twins!</po:comment>
> >       <po:shipDate>2001-12-17</po:shipDate>
> >     </po:item>
> >   </po:itemsList>
> > </xml-fragment>
> >
> > ===== test: xpath: ./po:purchaseOrder/po:shipTo/po:name =====
> >>> addNamespaces: prefix: po, uri: http://example.com/po
> >>> filter: class org.apache.xmlbeans.impl.values.XmlStringImpl,
> > <xml-fragment xmlns:po="http://example.com/po">Helen Zoe</xml-fragment>
> >
> > ===== test: xpath: ./po:purchaseOrder/po:shipTo[po:name = 'Helen Zoe']
> > =====
> >>> addNamespaces: prefix: po, uri: http://example.com/po
> >>> filter: class com.example.po.impl.USAddressImpl, <xml-fragment
> > type="USAddress" xmlns:po="http://example.com/po">
> >   <po:name>Helen Zoe</po:name>
> >   <po:street>47 Eden Street</po:street>
> >   <po:city>Santa Clara</po:city>
> >   <po:state>CA</po:state>
> >   <po:zip>95054</po:zip>
> > </xml-fragment>
> >
> > -----
> > po.xml
> >
> > ?xml version="1.0"?>
> > <po:purchaseOrder xmlns:po="http://example.com/po">
> >    <po:shipTo type="USAddress">
> >       <po:name>Helen Zoe</po:name>
> >       <po:street>47 Eden Street</po:street>
> >       <po:city>Santa Clara</po:city>
> >       <po:state>CA</po:state>
> >       <po:zip>95054</po:zip>
> >    </po:shipTo>
> >    <po:billTo type="USAddress">
> >       <po:name>Robert Zoe</po:name>
> >       <po:street>47 Eden Street</po:street>
> >       <po:city>Santa Clara</po:city>
> >       <po:state>CA</po:state>
> >       <po:zip>95054</po:zip>
> >    </po:billTo>
> >    <po:itemsList>
> >         <po:item partNum="833-AA">
> >             <po:productName>Lapis necklace</po:productName>
> >             <po:quantity>1</po:quantity>
> >             <po:USPrice>120</po:USPrice>
> >             <po:comment>Want this for the holidays!</po:comment>
> >             <po:shipDate>2001-12-15</po:shipDate>
> >         </po:item>
> >         <po:item partNum="423-AA">
> >             <po:productName>Baby Sound Monitor</po:productName>
> >             <po:quantity>2</po:quantity>
> >             <po:USPrice>300</po:USPrice>
> >             <po:comment>Got twins!</po:comment>
> >             <po:shipDate>2001-12-17</po:shipDate>
> >         </po:item>
> >     </po:itemsList>
> > </po:purchaseOrder>
> >
> > ----
> > po.xsd
> > <xsd:schema targetNamespace="http://example.com/po"
> >       xmlns="http://example.com/po"
> >       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
> >       elementFormDefault="qualified"
> >       attributeFormDefault="qualified">
> >
> >  <xsd:annotation>
> >   <xsd:documentation xml:lang="en">
> >    Purchase order schema for Example.com.
> >    Copyright 2000 Example.com. All rights reserved.
> >   </xsd:documentation>
> >  </xsd:annotation>
> >
> >  <xsd:element name="purchaseOrder" type="PurchaseOrderType"/>
> >
> >  <xsd:element name="comment" type="xsd:string"/>
> >
> >  <xsd:complexType name="PurchaseOrderType">
> >   <xsd:sequence>
> >    <xsd:element name="shipTo" type="USAddress"/>
> >    <xsd:element name="billTo" type="USAddress"/>
> >    <xsd:element ref="comment" minOccurs="0"/>
> >    <xsd:element name="itemsList"  type="Items"/>
> >   </xsd:sequence>
> >   <xsd:attribute name="orderDate" type="xsd:date"/>
> >  </xsd:complexType>
> >
> >  <xsd:complexType name="USAddress">
> >   <xsd:sequence>
> >    <xsd:element name="name"   type="xsd:string"/>
> >    <xsd:element name="street" type="xsd:string"/>
> >    <xsd:element name="city"   type="xsd:string"/>
> >    <xsd:element name="state"  type="xsd:string"/>
> >    <xsd:element name="zip"    type="xsd:decimal"/>
> >   </xsd:sequence>
> >   <xsd:attribute name="country" type="xsd:NMTOKEN"
> >      fixed="US"/>
> >  </xsd:complexType>
> >
> >  <xsd:complexType name="Items">
> >   <xsd:sequence>
> >    <xsd:element name="item" minOccurs="0" maxOccurs="unbounded">
> >     <xsd:complexType>
> >      <xsd:sequence>
> >       <xsd:element name="productName" type="xsd:string"/>
> >       <xsd:element name="quantity">
> >        <xsd:simpleType>
> >         <xsd:restriction base="xsd:positiveInteger">
> >          <xsd:maxExclusive value="100"/>
> >         </xsd:restriction>
> >        </xsd:simpleType>
> >       </xsd:element>
> >       <xsd:element name="USPrice"  type="xsd:decimal"/>
> >       <xsd:element ref="comment"   minOccurs="0"/>
> >       <xsd:element name="shipDate" type="xsd:date" minOccurs="0"/>
> >      </xsd:sequence>
> >      <xsd:attribute name="partNum" type="SKU" use="required"/>
> >     </xsd:complexType>
> >    </xsd:element>
> >   </xsd:sequence>
> >  </xsd:complexType>
> >
> >  <!-- Stock Keeping Unit, a code for identifying products -->
> >  <xsd:simpleType name="SKU">
> >   <xsd:restriction base="xsd:string">
> >    <xsd:pattern value="\d{3}-[A-Z]{2}"/>
> >   </xsd:restriction>
> >  </xsd:simpleType>
> >
> > </xsd:schema>
> >
> > ---
> > here is the code. I modified XBeansXPath.java and XBeansXPathAdv.java,
> > and
> > added XBeansNamespace.java
> >
> > package org.apache.xmlbeans.impl.xpath.jaxen;
> > ...
> > public class XBeansXPath extends BaseXPath
> > {
> >     ....
> >     public List selectNodes(Object node) throws JaxenException
> >     {
> >         XmlCursor xc;
> >  XmlObject xmlObj;
> >         if (node instanceof XmlObject)
> >         {
> >      xmlObj = (XmlObject)node;
> >      xc = xmlObj.newCursor();
> >         }
> >         else if (node instanceof XmlCursor)
> >  {
> >      xc = (XmlCursor)node;
> >      xmlObj = xc.getObject();
> >             xc = xc.newCursor();
> >         }
> >         else
> >             throw new IllegalArgumentException("node must be an
> > XmlObject or
> > an XmlCursor, found: " + node.getClass());
> >
> >  XBeansNamespace.addNamespaces(this, xmlObj); // nn (support xpath
> > including
> > namespace)
> >         ((XBeansNavigator)getNavigator()).setCursor(xc);
> >         return new ListImpl(super.selectNodes(
> > XBeansNavigator.getBookmarkInThisPlace(xc) ));
> >     }
> > ....
> > }
> > ----
> > package org.apache.xmlbeans.impl.xpath.jaxen;
> > ...
> > public class XBeansXPathAdv
> >     extends BaseXPath
> >     implements JaxenXBeansDelegate.SelectPathInterface
> > {
> > ...
> >     public List selectNodes(Object node) throws JaxenException
> >     {
> >         XmlCursor xc;
> >         xc = ((XmlCursor)node);
> >  XmlObject xmlObj = xc.getObject();
> >  XBeansNamespace.addNamespaces(this, xmlObj);
> >         ((XBeansNavigator)getNavigator()).setCursor(xc);
> >         return super.selectNodes(
> > XBeansNavigator.getBookmarkInThisPlace(xc) );
> >     }
> > ...
> > }
> > -----
> > package org.apache.xmlbeans.impl.xpath.jaxen;
> > import org.jaxen.BaseXPath;
> > import org.jaxen.JaxenException;
> > import org.apache.xmlbeans.XmlObject;
> > import org.apache.xmlbeans.XmlCursor;
> >
> > import java.util.List;
> > import java.util.Map;
> > import java.util.HashMap;
> > import java.util.Iterator;
> >
> > public class XBeansNamespace
> > {
> >     public static void addNamespaces(BaseXPath baseXPath, XmlObject
> > doc)
> > throws JaxenException {
> >  HashMap nslist = getAdditionalNamespaces(getFirstChild(doc));
> >  for (Iterator iter = nslist.entrySet().iterator(); iter.hasNext(); ) {
> >      Map.Entry entry = (Map.Entry)iter.next();
> >      String prefix = (String)entry.getKey();
> >      String uri = (String)entry.getValue();
> >      System.out.println(">> addNamespaces: prefix: "+prefix+", uri:
> > "+uri);
> >      baseXPath.addNamespace(prefix, uri);
> >  }
> >     }
> >     private static XmlObject getFirstChild(XmlObject xmlObj) {
> >  XmlCursor xc = xmlObj.newCursor();
> >  try {
> >      xc.toFirstChild();
> >      return xc.getObject();
> >  } finally {
> >      xc.dispose();
> >  }
> >     }
> >     private static HashMap getAdditionalNamespaces(XmlObject xmlObj) {
> >  HashMap nslist = new HashMap();
> >  XmlCursor xc = xmlObj.newCursor();
> >  try {
> >      xc.getAllNamespaces(nslist);
> >  } finally {
> >      xc.dispose();
> >  }
> >  return nslist;
> >     }
> > }
> >
> >
> >
> > - ---------------------------------------------------------------------
> > To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
> > For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
> > Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/
> >
>
>
> - ---------------------------------------------------------------------
> To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
> Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/
>


- ---------------------------------------------------------------------
To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/


Re: bug fix for XPath using Namespace

Posted by nn <nn...@comcast.net>.
> Namespace prefixes are determined by the context of the XPath
> expression, not that of the 'target' document.

How do you define the context of XPath?
Is the syntax of current XMLBeans a part of XPath specification?

Are there any meaningfull case where the namespace which is not used in the
target document is useful?
I think the most simple use case, using namespace associated with target
document as default namespace would be the most convenient. This kind of
behavior seems not part of standard, but a part of embeding system.

>
> I have a patch that uses XmlOptions (I think setSaveSuggestedPrefixes)
> to supply this information into XmlBeans for XPath usage. I'm fine with
> submitting this patch sometime this weekend - would a 'fresh'
> XmlOptions option be preferred over setSaveSuggestedPrefixes and the
> others present already?

Anyway, as long as this problem are removed soon, it's OK.
This aspect was crutial for me.

nn

>
> -David Waite
>
> On Jun 18, 2004, at 5:48 AM, nn wrote:
>
> > Hi,
> > I think it is important to remove the restriction for not allowing to
> > use
> > namespace in complex XPath expression.
> > (s.t ./po:purchaseOrder/po:shipTo[po:name = 'Helen Zoe']" ).
> >
> > Since otherwise, the useful XPath (which is provided by JAXEN) cannot
> > be
> > used for most XML data associated with XMLSchema. I thought this may
> > be a
> > restriction of JAXEN, but it wasn't.
> > This fix is relatively simple. I  hope this should be incorporated in
> > the
> > release (after reviewed by commiters and tested throughly).
> >
> > I'll attach the jar file.
> >
> > nn
> >
> > ------
> >
> > public class POTest {
> >     public static void filter(XmlObject[] result) throws Throwable {
> >         for (int i = 0; i < result.length; i++) {
> >      XmlObject obj = result[i];
> >      System.out.println(">> filter: "+obj.getClass()+", "+obj);
> >  }
> >     }
> >     private static String[] test_xpathes = new String[]{
> >   "//po:purchaseOrder",
> >   "./po:purchaseOrder/po:shipTo/po:name",
> >   "./po:purchaseOrder/po:shipTo[po:name = 'Helen Zoe']",
> >   };
> >
> >     public static void test(PurchaseOrderDocument xmlObj, String xpath)
> > throws Throwable {
> >  System.out.println("\n===== test: xpath: "+xpath+" =====");
> >  XmlObject[] result = xmlObj.selectPath(xpath);
> >  filter(result);
> >     }
> >
> >     public static void main(String argv[]) {
> >  try {
> >      File sourceID = new File(argv[0]);
> >      PurchaseOrderDocument xmlObj =
> > PurchaseOrderDocument.Factory.parse(sourceID);
> >      for (int i = 0; i < test_xpathes.length; i++) {
> >   test(xmlObj, test_xpathes[i]);
> >      }
> >  } catch (Throwable err) {
> >      err.printStackTrace();
> >  }
> >     }
> > }
> > -----
> > the result:
> >
> > ===== test: xpath: //po:purchaseOrder =====
> >>> addNamespaces: prefix: po, uri: http://example.com/po
> >>> filter: class com.example.po.impl.PurchaseOrderTypeImpl,
> >>> <xml-fragment
> > xmlns:po="http://example.com/po">
> >   <po:shipTo type="USAddress">
> >     <po:name>Helen Zoe</po:name>
> >     <po:street>47 Eden Street</po:street>
> >     <po:city>Santa Clara</po:city>
> >     <po:state>CA</po:state>
> >     <po:zip>95054</po:zip>
> >   </po:shipTo>
> >   <po:billTo type="USAddress">
> >     <po:name>Robert Zoe</po:name>
> >     <po:street>47 Eden Street</po:street>
> >     <po:city>Santa Clara</po:city>
> >     <po:state>CA</po:state>
> >     <po:zip>95054</po:zip>
> >   </po:billTo>
> >   <po:itemsList>
> >     <po:item partNum="833-AA">
> >       <po:productName>Lapis necklace</po:productName>
> >       <po:quantity>1</po:quantity>
> >       <po:USPrice>120</po:USPrice>
> >       <po:comment>Want this for the holidays!</po:comment>
> >       <po:shipDate>2001-12-15</po:shipDate>
> >     </po:item>
> >     <po:item partNum="423-AA">
> >       <po:productName>Baby Sound Monitor</po:productName>
> >       <po:quantity>2</po:quantity>
> >       <po:USPrice>300</po:USPrice>
> >       <po:comment>Got twins!</po:comment>
> >       <po:shipDate>2001-12-17</po:shipDate>
> >     </po:item>
> >   </po:itemsList>
> > </xml-fragment>
> >
> > ===== test: xpath: ./po:purchaseOrder/po:shipTo/po:name =====
> >>> addNamespaces: prefix: po, uri: http://example.com/po
> >>> filter: class org.apache.xmlbeans.impl.values.XmlStringImpl,
> > <xml-fragment xmlns:po="http://example.com/po">Helen Zoe</xml-fragment>
> >
> > ===== test: xpath: ./po:purchaseOrder/po:shipTo[po:name = 'Helen Zoe']
> > =====
> >>> addNamespaces: prefix: po, uri: http://example.com/po
> >>> filter: class com.example.po.impl.USAddressImpl, <xml-fragment
> > type="USAddress" xmlns:po="http://example.com/po">
> >   <po:name>Helen Zoe</po:name>
> >   <po:street>47 Eden Street</po:street>
> >   <po:city>Santa Clara</po:city>
> >   <po:state>CA</po:state>
> >   <po:zip>95054</po:zip>
> > </xml-fragment>
> >
> > -----
> > po.xml
> >
> > ?xml version="1.0"?>
> > <po:purchaseOrder xmlns:po="http://example.com/po">
> >    <po:shipTo type="USAddress">
> >       <po:name>Helen Zoe</po:name>
> >       <po:street>47 Eden Street</po:street>
> >       <po:city>Santa Clara</po:city>
> >       <po:state>CA</po:state>
> >       <po:zip>95054</po:zip>
> >    </po:shipTo>
> >    <po:billTo type="USAddress">
> >       <po:name>Robert Zoe</po:name>
> >       <po:street>47 Eden Street</po:street>
> >       <po:city>Santa Clara</po:city>
> >       <po:state>CA</po:state>
> >       <po:zip>95054</po:zip>
> >    </po:billTo>
> >    <po:itemsList>
> >         <po:item partNum="833-AA">
> >             <po:productName>Lapis necklace</po:productName>
> >             <po:quantity>1</po:quantity>
> >             <po:USPrice>120</po:USPrice>
> >             <po:comment>Want this for the holidays!</po:comment>
> >             <po:shipDate>2001-12-15</po:shipDate>
> >         </po:item>
> >         <po:item partNum="423-AA">
> >             <po:productName>Baby Sound Monitor</po:productName>
> >             <po:quantity>2</po:quantity>
> >             <po:USPrice>300</po:USPrice>
> >             <po:comment>Got twins!</po:comment>
> >             <po:shipDate>2001-12-17</po:shipDate>
> >         </po:item>
> >     </po:itemsList>
> > </po:purchaseOrder>
> >
> > ----
> > po.xsd
> > <xsd:schema targetNamespace="http://example.com/po"
> >       xmlns="http://example.com/po"
> >       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
> >       elementFormDefault="qualified"
> >       attributeFormDefault="qualified">
> >
> >  <xsd:annotation>
> >   <xsd:documentation xml:lang="en">
> >    Purchase order schema for Example.com.
> >    Copyright 2000 Example.com. All rights reserved.
> >   </xsd:documentation>
> >  </xsd:annotation>
> >
> >  <xsd:element name="purchaseOrder" type="PurchaseOrderType"/>
> >
> >  <xsd:element name="comment" type="xsd:string"/>
> >
> >  <xsd:complexType name="PurchaseOrderType">
> >   <xsd:sequence>
> >    <xsd:element name="shipTo" type="USAddress"/>
> >    <xsd:element name="billTo" type="USAddress"/>
> >    <xsd:element ref="comment" minOccurs="0"/>
> >    <xsd:element name="itemsList"  type="Items"/>
> >   </xsd:sequence>
> >   <xsd:attribute name="orderDate" type="xsd:date"/>
> >  </xsd:complexType>
> >
> >  <xsd:complexType name="USAddress">
> >   <xsd:sequence>
> >    <xsd:element name="name"   type="xsd:string"/>
> >    <xsd:element name="street" type="xsd:string"/>
> >    <xsd:element name="city"   type="xsd:string"/>
> >    <xsd:element name="state"  type="xsd:string"/>
> >    <xsd:element name="zip"    type="xsd:decimal"/>
> >   </xsd:sequence>
> >   <xsd:attribute name="country" type="xsd:NMTOKEN"
> >      fixed="US"/>
> >  </xsd:complexType>
> >
> >  <xsd:complexType name="Items">
> >   <xsd:sequence>
> >    <xsd:element name="item" minOccurs="0" maxOccurs="unbounded">
> >     <xsd:complexType>
> >      <xsd:sequence>
> >       <xsd:element name="productName" type="xsd:string"/>
> >       <xsd:element name="quantity">
> >        <xsd:simpleType>
> >         <xsd:restriction base="xsd:positiveInteger">
> >          <xsd:maxExclusive value="100"/>
> >         </xsd:restriction>
> >        </xsd:simpleType>
> >       </xsd:element>
> >       <xsd:element name="USPrice"  type="xsd:decimal"/>
> >       <xsd:element ref="comment"   minOccurs="0"/>
> >       <xsd:element name="shipDate" type="xsd:date" minOccurs="0"/>
> >      </xsd:sequence>
> >      <xsd:attribute name="partNum" type="SKU" use="required"/>
> >     </xsd:complexType>
> >    </xsd:element>
> >   </xsd:sequence>
> >  </xsd:complexType>
> >
> >  <!-- Stock Keeping Unit, a code for identifying products -->
> >  <xsd:simpleType name="SKU">
> >   <xsd:restriction base="xsd:string">
> >    <xsd:pattern value="\d{3}-[A-Z]{2}"/>
> >   </xsd:restriction>
> >  </xsd:simpleType>
> >
> > </xsd:schema>
> >
> > ---
> > here is the code. I modified XBeansXPath.java and XBeansXPathAdv.java,
> > and
> > added XBeansNamespace.java
> >
> > package org.apache.xmlbeans.impl.xpath.jaxen;
> > ...
> > public class XBeansXPath extends BaseXPath
> > {
> >     ....
> >     public List selectNodes(Object node) throws JaxenException
> >     {
> >         XmlCursor xc;
> >  XmlObject xmlObj;
> >         if (node instanceof XmlObject)
> >         {
> >      xmlObj = (XmlObject)node;
> >      xc = xmlObj.newCursor();
> >         }
> >         else if (node instanceof XmlCursor)
> >  {
> >      xc = (XmlCursor)node;
> >      xmlObj = xc.getObject();
> >             xc = xc.newCursor();
> >         }
> >         else
> >             throw new IllegalArgumentException("node must be an
> > XmlObject or
> > an XmlCursor, found: " + node.getClass());
> >
> >  XBeansNamespace.addNamespaces(this, xmlObj); // nn (support xpath
> > including
> > namespace)
> >         ((XBeansNavigator)getNavigator()).setCursor(xc);
> >         return new ListImpl(super.selectNodes(
> > XBeansNavigator.getBookmarkInThisPlace(xc) ));
> >     }
> > ....
> > }
> > ----
> > package org.apache.xmlbeans.impl.xpath.jaxen;
> > ...
> > public class XBeansXPathAdv
> >     extends BaseXPath
> >     implements JaxenXBeansDelegate.SelectPathInterface
> > {
> > ...
> >     public List selectNodes(Object node) throws JaxenException
> >     {
> >         XmlCursor xc;
> >         xc = ((XmlCursor)node);
> >  XmlObject xmlObj = xc.getObject();
> >  XBeansNamespace.addNamespaces(this, xmlObj);
> >         ((XBeansNavigator)getNavigator()).setCursor(xc);
> >         return super.selectNodes(
> > XBeansNavigator.getBookmarkInThisPlace(xc) );
> >     }
> > ...
> > }
> > -----
> > package org.apache.xmlbeans.impl.xpath.jaxen;
> > import org.jaxen.BaseXPath;
> > import org.jaxen.JaxenException;
> > import org.apache.xmlbeans.XmlObject;
> > import org.apache.xmlbeans.XmlCursor;
> >
> > import java.util.List;
> > import java.util.Map;
> > import java.util.HashMap;
> > import java.util.Iterator;
> >
> > public class XBeansNamespace
> > {
> >     public static void addNamespaces(BaseXPath baseXPath, XmlObject
> > doc)
> > throws JaxenException {
> >  HashMap nslist = getAdditionalNamespaces(getFirstChild(doc));
> >  for (Iterator iter = nslist.entrySet().iterator(); iter.hasNext(); ) {
> >      Map.Entry entry = (Map.Entry)iter.next();
> >      String prefix = (String)entry.getKey();
> >      String uri = (String)entry.getValue();
> >      System.out.println(">> addNamespaces: prefix: "+prefix+", uri:
> > "+uri);
> >      baseXPath.addNamespace(prefix, uri);
> >  }
> >     }
> >     private static XmlObject getFirstChild(XmlObject xmlObj) {
> >  XmlCursor xc = xmlObj.newCursor();
> >  try {
> >      xc.toFirstChild();
> >      return xc.getObject();
> >  } finally {
> >      xc.dispose();
> >  }
> >     }
> >     private static HashMap getAdditionalNamespaces(XmlObject xmlObj) {
> >  HashMap nslist = new HashMap();
> >  XmlCursor xc = xmlObj.newCursor();
> >  try {
> >      xc.getAllNamespaces(nslist);
> >  } finally {
> >      xc.dispose();
> >  }
> >  return nslist;
> >     }
> > }
> >
> >
> >
> > - ---------------------------------------------------------------------
> > To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
> > For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
> > Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/
> >
>
>
> - ---------------------------------------------------------------------
> To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
> Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/
>


- ---------------------------------------------------------------------
To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/


Re: bug fix for XPath using Namespace

Posted by David Waite <ma...@akuma.org>.
Namespace prefixes are determined by the context of the XPath 
expression, not that of the 'target' document.

I have a patch that uses XmlOptions (I think setSaveSuggestedPrefixes) 
to supply this information into XmlBeans for XPath usage. I'm fine with 
submitting this patch sometime this weekend - would a 'fresh' 
XmlOptions option be preferred over setSaveSuggestedPrefixes and the 
others present already?

-David Waite

On Jun 18, 2004, at 5:48 AM, nn wrote:

> Hi,
> I think it is important to remove the restriction for not allowing to 
> use
> namespace in complex XPath expression.
> (s.t ./po:purchaseOrder/po:shipTo[po:name = 'Helen Zoe']" ).
>
> Since otherwise, the useful XPath (which is provided by JAXEN) cannot 
> be
> used for most XML data associated with XMLSchema. I thought this may 
> be a
> restriction of JAXEN, but it wasn't.
> This fix is relatively simple. I  hope this should be incorporated in 
> the
> release (after reviewed by commiters and tested throughly).
>
> I'll attach the jar file.
>
> nn
>
> ------
>
> public class POTest {
>     public static void filter(XmlObject[] result) throws Throwable {
>         for (int i = 0; i < result.length; i++) {
>      XmlObject obj = result[i];
>      System.out.println(">> filter: "+obj.getClass()+", "+obj);
>  }
>     }
>     private static String[] test_xpathes = new String[]{
>   "//po:purchaseOrder",
>   "./po:purchaseOrder/po:shipTo/po:name",
>   "./po:purchaseOrder/po:shipTo[po:name = 'Helen Zoe']",
>   };
>
>     public static void test(PurchaseOrderDocument xmlObj, String xpath)
> throws Throwable {
>  System.out.println("\n===== test: xpath: "+xpath+" =====");
>  XmlObject[] result = xmlObj.selectPath(xpath);
>  filter(result);
>     }
>
>     public static void main(String argv[]) {
>  try {
>      File sourceID = new File(argv[0]);
>      PurchaseOrderDocument xmlObj =
> PurchaseOrderDocument.Factory.parse(sourceID);
>      for (int i = 0; i < test_xpathes.length; i++) {
>   test(xmlObj, test_xpathes[i]);
>      }
>  } catch (Throwable err) {
>      err.printStackTrace();
>  }
>     }
> }
> -----
> the result:
>
> ===== test: xpath: //po:purchaseOrder =====
>>> addNamespaces: prefix: po, uri: http://example.com/po
>>> filter: class com.example.po.impl.PurchaseOrderTypeImpl, 
>>> <xml-fragment
> xmlns:po="http://example.com/po">
>   <po:shipTo type="USAddress">
>     <po:name>Helen Zoe</po:name>
>     <po:street>47 Eden Street</po:street>
>     <po:city>Santa Clara</po:city>
>     <po:state>CA</po:state>
>     <po:zip>95054</po:zip>
>   </po:shipTo>
>   <po:billTo type="USAddress">
>     <po:name>Robert Zoe</po:name>
>     <po:street>47 Eden Street</po:street>
>     <po:city>Santa Clara</po:city>
>     <po:state>CA</po:state>
>     <po:zip>95054</po:zip>
>   </po:billTo>
>   <po:itemsList>
>     <po:item partNum="833-AA">
>       <po:productName>Lapis necklace</po:productName>
>       <po:quantity>1</po:quantity>
>       <po:USPrice>120</po:USPrice>
>       <po:comment>Want this for the holidays!</po:comment>
>       <po:shipDate>2001-12-15</po:shipDate>
>     </po:item>
>     <po:item partNum="423-AA">
>       <po:productName>Baby Sound Monitor</po:productName>
>       <po:quantity>2</po:quantity>
>       <po:USPrice>300</po:USPrice>
>       <po:comment>Got twins!</po:comment>
>       <po:shipDate>2001-12-17</po:shipDate>
>     </po:item>
>   </po:itemsList>
> </xml-fragment>
>
> ===== test: xpath: ./po:purchaseOrder/po:shipTo/po:name =====
>>> addNamespaces: prefix: po, uri: http://example.com/po
>>> filter: class org.apache.xmlbeans.impl.values.XmlStringImpl,
> <xml-fragment xmlns:po="http://example.com/po">Helen Zoe</xml-fragment>
>
> ===== test: xpath: ./po:purchaseOrder/po:shipTo[po:name = 'Helen Zoe'] 
> =====
>>> addNamespaces: prefix: po, uri: http://example.com/po
>>> filter: class com.example.po.impl.USAddressImpl, <xml-fragment
> type="USAddress" xmlns:po="http://example.com/po">
>   <po:name>Helen Zoe</po:name>
>   <po:street>47 Eden Street</po:street>
>   <po:city>Santa Clara</po:city>
>   <po:state>CA</po:state>
>   <po:zip>95054</po:zip>
> </xml-fragment>
>
> -----
> po.xml
>
> ?xml version="1.0"?>
> <po:purchaseOrder xmlns:po="http://example.com/po">
>    <po:shipTo type="USAddress">
>       <po:name>Helen Zoe</po:name>
>       <po:street>47 Eden Street</po:street>
>       <po:city>Santa Clara</po:city>
>       <po:state>CA</po:state>
>       <po:zip>95054</po:zip>
>    </po:shipTo>
>    <po:billTo type="USAddress">
>       <po:name>Robert Zoe</po:name>
>       <po:street>47 Eden Street</po:street>
>       <po:city>Santa Clara</po:city>
>       <po:state>CA</po:state>
>       <po:zip>95054</po:zip>
>    </po:billTo>
>    <po:itemsList>
>         <po:item partNum="833-AA">
>             <po:productName>Lapis necklace</po:productName>
>             <po:quantity>1</po:quantity>
>             <po:USPrice>120</po:USPrice>
>             <po:comment>Want this for the holidays!</po:comment>
>             <po:shipDate>2001-12-15</po:shipDate>
>         </po:item>
>         <po:item partNum="423-AA">
>             <po:productName>Baby Sound Monitor</po:productName>
>             <po:quantity>2</po:quantity>
>             <po:USPrice>300</po:USPrice>
>             <po:comment>Got twins!</po:comment>
>             <po:shipDate>2001-12-17</po:shipDate>
>         </po:item>
>     </po:itemsList>
> </po:purchaseOrder>
>
> ----
> po.xsd
> <xsd:schema targetNamespace="http://example.com/po"
>       xmlns="http://example.com/po"
>       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
>       elementFormDefault="qualified"
>       attributeFormDefault="qualified">
>
>  <xsd:annotation>
>   <xsd:documentation xml:lang="en">
>    Purchase order schema for Example.com.
>    Copyright 2000 Example.com. All rights reserved.
>   </xsd:documentation>
>  </xsd:annotation>
>
>  <xsd:element name="purchaseOrder" type="PurchaseOrderType"/>
>
>  <xsd:element name="comment" type="xsd:string"/>
>
>  <xsd:complexType name="PurchaseOrderType">
>   <xsd:sequence>
>    <xsd:element name="shipTo" type="USAddress"/>
>    <xsd:element name="billTo" type="USAddress"/>
>    <xsd:element ref="comment" minOccurs="0"/>
>    <xsd:element name="itemsList"  type="Items"/>
>   </xsd:sequence>
>   <xsd:attribute name="orderDate" type="xsd:date"/>
>  </xsd:complexType>
>
>  <xsd:complexType name="USAddress">
>   <xsd:sequence>
>    <xsd:element name="name"   type="xsd:string"/>
>    <xsd:element name="street" type="xsd:string"/>
>    <xsd:element name="city"   type="xsd:string"/>
>    <xsd:element name="state"  type="xsd:string"/>
>    <xsd:element name="zip"    type="xsd:decimal"/>
>   </xsd:sequence>
>   <xsd:attribute name="country" type="xsd:NMTOKEN"
>      fixed="US"/>
>  </xsd:complexType>
>
>  <xsd:complexType name="Items">
>   <xsd:sequence>
>    <xsd:element name="item" minOccurs="0" maxOccurs="unbounded">
>     <xsd:complexType>
>      <xsd:sequence>
>       <xsd:element name="productName" type="xsd:string"/>
>       <xsd:element name="quantity">
>        <xsd:simpleType>
>         <xsd:restriction base="xsd:positiveInteger">
>          <xsd:maxExclusive value="100"/>
>         </xsd:restriction>
>        </xsd:simpleType>
>       </xsd:element>
>       <xsd:element name="USPrice"  type="xsd:decimal"/>
>       <xsd:element ref="comment"   minOccurs="0"/>
>       <xsd:element name="shipDate" type="xsd:date" minOccurs="0"/>
>      </xsd:sequence>
>      <xsd:attribute name="partNum" type="SKU" use="required"/>
>     </xsd:complexType>
>    </xsd:element>
>   </xsd:sequence>
>  </xsd:complexType>
>
>  <!-- Stock Keeping Unit, a code for identifying products -->
>  <xsd:simpleType name="SKU">
>   <xsd:restriction base="xsd:string">
>    <xsd:pattern value="\d{3}-[A-Z]{2}"/>
>   </xsd:restriction>
>  </xsd:simpleType>
>
> </xsd:schema>
>
> ---
> here is the code. I modified XBeansXPath.java and XBeansXPathAdv.java, 
> and
> added XBeansNamespace.java
>
> package org.apache.xmlbeans.impl.xpath.jaxen;
> ...
> public class XBeansXPath extends BaseXPath
> {
>     ....
>     public List selectNodes(Object node) throws JaxenException
>     {
>         XmlCursor xc;
>  XmlObject xmlObj;
>         if (node instanceof XmlObject)
>         {
>      xmlObj = (XmlObject)node;
>      xc = xmlObj.newCursor();
>         }
>         else if (node instanceof XmlCursor)
>  {
>      xc = (XmlCursor)node;
>      xmlObj = xc.getObject();
>             xc = xc.newCursor();
>         }
>         else
>             throw new IllegalArgumentException("node must be an 
> XmlObject or
> an XmlCursor, found: " + node.getClass());
>
>  XBeansNamespace.addNamespaces(this, xmlObj); // nn (support xpath 
> including
> namespace)
>         ((XBeansNavigator)getNavigator()).setCursor(xc);
>         return new ListImpl(super.selectNodes(
> XBeansNavigator.getBookmarkInThisPlace(xc) ));
>     }
> ....
> }
> ----
> package org.apache.xmlbeans.impl.xpath.jaxen;
> ...
> public class XBeansXPathAdv
>     extends BaseXPath
>     implements JaxenXBeansDelegate.SelectPathInterface
> {
> ...
>     public List selectNodes(Object node) throws JaxenException
>     {
>         XmlCursor xc;
>         xc = ((XmlCursor)node);
>  XmlObject xmlObj = xc.getObject();
>  XBeansNamespace.addNamespaces(this, xmlObj);
>         ((XBeansNavigator)getNavigator()).setCursor(xc);
>         return super.selectNodes(
> XBeansNavigator.getBookmarkInThisPlace(xc) );
>     }
> ...
> }
> -----
> package org.apache.xmlbeans.impl.xpath.jaxen;
> import org.jaxen.BaseXPath;
> import org.jaxen.JaxenException;
> import org.apache.xmlbeans.XmlObject;
> import org.apache.xmlbeans.XmlCursor;
>
> import java.util.List;
> import java.util.Map;
> import java.util.HashMap;
> import java.util.Iterator;
>
> public class XBeansNamespace
> {
>     public static void addNamespaces(BaseXPath baseXPath, XmlObject 
> doc)
> throws JaxenException {
>  HashMap nslist = getAdditionalNamespaces(getFirstChild(doc));
>  for (Iterator iter = nslist.entrySet().iterator(); iter.hasNext(); ) {
>      Map.Entry entry = (Map.Entry)iter.next();
>      String prefix = (String)entry.getKey();
>      String uri = (String)entry.getValue();
>      System.out.println(">> addNamespaces: prefix: "+prefix+", uri: 
> "+uri);
>      baseXPath.addNamespace(prefix, uri);
>  }
>     }
>     private static XmlObject getFirstChild(XmlObject xmlObj) {
>  XmlCursor xc = xmlObj.newCursor();
>  try {
>      xc.toFirstChild();
>      return xc.getObject();
>  } finally {
>      xc.dispose();
>  }
>     }
>     private static HashMap getAdditionalNamespaces(XmlObject xmlObj) {
>  HashMap nslist = new HashMap();
>  XmlCursor xc = xmlObj.newCursor();
>  try {
>      xc.getAllNamespaces(nslist);
>  } finally {
>      xc.dispose();
>  }
>  return nslist;
>     }
> }
>
>
>
> - ---------------------------------------------------------------------
> To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
> Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/
>


- ---------------------------------------------------------------------
To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/


Re: bug fix for XPath using Namespace

Posted by David Waite <ma...@akuma.org>.
Namespace prefixes are determined by the context of the XPath 
expression, not that of the 'target' document.

I have a patch that uses XmlOptions (I think setSaveSuggestedPrefixes) 
to supply this information into XmlBeans for XPath usage. I'm fine with 
submitting this patch sometime this weekend - would a 'fresh' 
XmlOptions option be preferred over setSaveSuggestedPrefixes and the 
others present already?

-David Waite

On Jun 18, 2004, at 5:48 AM, nn wrote:

> Hi,
> I think it is important to remove the restriction for not allowing to 
> use
> namespace in complex XPath expression.
> (s.t ./po:purchaseOrder/po:shipTo[po:name = 'Helen Zoe']" ).
>
> Since otherwise, the useful XPath (which is provided by JAXEN) cannot 
> be
> used for most XML data associated with XMLSchema. I thought this may 
> be a
> restriction of JAXEN, but it wasn't.
> This fix is relatively simple. I  hope this should be incorporated in 
> the
> release (after reviewed by commiters and tested throughly).
>
> I'll attach the jar file.
>
> nn
>
> ------
>
> public class POTest {
>     public static void filter(XmlObject[] result) throws Throwable {
>         for (int i = 0; i < result.length; i++) {
>      XmlObject obj = result[i];
>      System.out.println(">> filter: "+obj.getClass()+", "+obj);
>  }
>     }
>     private static String[] test_xpathes = new String[]{
>   "//po:purchaseOrder",
>   "./po:purchaseOrder/po:shipTo/po:name",
>   "./po:purchaseOrder/po:shipTo[po:name = 'Helen Zoe']",
>   };
>
>     public static void test(PurchaseOrderDocument xmlObj, String xpath)
> throws Throwable {
>  System.out.println("\n===== test: xpath: "+xpath+" =====");
>  XmlObject[] result = xmlObj.selectPath(xpath);
>  filter(result);
>     }
>
>     public static void main(String argv[]) {
>  try {
>      File sourceID = new File(argv[0]);
>      PurchaseOrderDocument xmlObj =
> PurchaseOrderDocument.Factory.parse(sourceID);
>      for (int i = 0; i < test_xpathes.length; i++) {
>   test(xmlObj, test_xpathes[i]);
>      }
>  } catch (Throwable err) {
>      err.printStackTrace();
>  }
>     }
> }
> -----
> the result:
>
> ===== test: xpath: //po:purchaseOrder =====
>>> addNamespaces: prefix: po, uri: http://example.com/po
>>> filter: class com.example.po.impl.PurchaseOrderTypeImpl, 
>>> <xml-fragment
> xmlns:po="http://example.com/po">
>   <po:shipTo type="USAddress">
>     <po:name>Helen Zoe</po:name>
>     <po:street>47 Eden Street</po:street>
>     <po:city>Santa Clara</po:city>
>     <po:state>CA</po:state>
>     <po:zip>95054</po:zip>
>   </po:shipTo>
>   <po:billTo type="USAddress">
>     <po:name>Robert Zoe</po:name>
>     <po:street>47 Eden Street</po:street>
>     <po:city>Santa Clara</po:city>
>     <po:state>CA</po:state>
>     <po:zip>95054</po:zip>
>   </po:billTo>
>   <po:itemsList>
>     <po:item partNum="833-AA">
>       <po:productName>Lapis necklace</po:productName>
>       <po:quantity>1</po:quantity>
>       <po:USPrice>120</po:USPrice>
>       <po:comment>Want this for the holidays!</po:comment>
>       <po:shipDate>2001-12-15</po:shipDate>
>     </po:item>
>     <po:item partNum="423-AA">
>       <po:productName>Baby Sound Monitor</po:productName>
>       <po:quantity>2</po:quantity>
>       <po:USPrice>300</po:USPrice>
>       <po:comment>Got twins!</po:comment>
>       <po:shipDate>2001-12-17</po:shipDate>
>     </po:item>
>   </po:itemsList>
> </xml-fragment>
>
> ===== test: xpath: ./po:purchaseOrder/po:shipTo/po:name =====
>>> addNamespaces: prefix: po, uri: http://example.com/po
>>> filter: class org.apache.xmlbeans.impl.values.XmlStringImpl,
> <xml-fragment xmlns:po="http://example.com/po">Helen Zoe</xml-fragment>
>
> ===== test: xpath: ./po:purchaseOrder/po:shipTo[po:name = 'Helen Zoe'] 
> =====
>>> addNamespaces: prefix: po, uri: http://example.com/po
>>> filter: class com.example.po.impl.USAddressImpl, <xml-fragment
> type="USAddress" xmlns:po="http://example.com/po">
>   <po:name>Helen Zoe</po:name>
>   <po:street>47 Eden Street</po:street>
>   <po:city>Santa Clara</po:city>
>   <po:state>CA</po:state>
>   <po:zip>95054</po:zip>
> </xml-fragment>
>
> -----
> po.xml
>
> ?xml version="1.0"?>
> <po:purchaseOrder xmlns:po="http://example.com/po">
>    <po:shipTo type="USAddress">
>       <po:name>Helen Zoe</po:name>
>       <po:street>47 Eden Street</po:street>
>       <po:city>Santa Clara</po:city>
>       <po:state>CA</po:state>
>       <po:zip>95054</po:zip>
>    </po:shipTo>
>    <po:billTo type="USAddress">
>       <po:name>Robert Zoe</po:name>
>       <po:street>47 Eden Street</po:street>
>       <po:city>Santa Clara</po:city>
>       <po:state>CA</po:state>
>       <po:zip>95054</po:zip>
>    </po:billTo>
>    <po:itemsList>
>         <po:item partNum="833-AA">
>             <po:productName>Lapis necklace</po:productName>
>             <po:quantity>1</po:quantity>
>             <po:USPrice>120</po:USPrice>
>             <po:comment>Want this for the holidays!</po:comment>
>             <po:shipDate>2001-12-15</po:shipDate>
>         </po:item>
>         <po:item partNum="423-AA">
>             <po:productName>Baby Sound Monitor</po:productName>
>             <po:quantity>2</po:quantity>
>             <po:USPrice>300</po:USPrice>
>             <po:comment>Got twins!</po:comment>
>             <po:shipDate>2001-12-17</po:shipDate>
>         </po:item>
>     </po:itemsList>
> </po:purchaseOrder>
>
> ----
> po.xsd
> <xsd:schema targetNamespace="http://example.com/po"
>       xmlns="http://example.com/po"
>       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
>       elementFormDefault="qualified"
>       attributeFormDefault="qualified">
>
>  <xsd:annotation>
>   <xsd:documentation xml:lang="en">
>    Purchase order schema for Example.com.
>    Copyright 2000 Example.com. All rights reserved.
>   </xsd:documentation>
>  </xsd:annotation>
>
>  <xsd:element name="purchaseOrder" type="PurchaseOrderType"/>
>
>  <xsd:element name="comment" type="xsd:string"/>
>
>  <xsd:complexType name="PurchaseOrderType">
>   <xsd:sequence>
>    <xsd:element name="shipTo" type="USAddress"/>
>    <xsd:element name="billTo" type="USAddress"/>
>    <xsd:element ref="comment" minOccurs="0"/>
>    <xsd:element name="itemsList"  type="Items"/>
>   </xsd:sequence>
>   <xsd:attribute name="orderDate" type="xsd:date"/>
>  </xsd:complexType>
>
>  <xsd:complexType name="USAddress">
>   <xsd:sequence>
>    <xsd:element name="name"   type="xsd:string"/>
>    <xsd:element name="street" type="xsd:string"/>
>    <xsd:element name="city"   type="xsd:string"/>
>    <xsd:element name="state"  type="xsd:string"/>
>    <xsd:element name="zip"    type="xsd:decimal"/>
>   </xsd:sequence>
>   <xsd:attribute name="country" type="xsd:NMTOKEN"
>      fixed="US"/>
>  </xsd:complexType>
>
>  <xsd:complexType name="Items">
>   <xsd:sequence>
>    <xsd:element name="item" minOccurs="0" maxOccurs="unbounded">
>     <xsd:complexType>
>      <xsd:sequence>
>       <xsd:element name="productName" type="xsd:string"/>
>       <xsd:element name="quantity">
>        <xsd:simpleType>
>         <xsd:restriction base="xsd:positiveInteger">
>          <xsd:maxExclusive value="100"/>
>         </xsd:restriction>
>        </xsd:simpleType>
>       </xsd:element>
>       <xsd:element name="USPrice"  type="xsd:decimal"/>
>       <xsd:element ref="comment"   minOccurs="0"/>
>       <xsd:element name="shipDate" type="xsd:date" minOccurs="0"/>
>      </xsd:sequence>
>      <xsd:attribute name="partNum" type="SKU" use="required"/>
>     </xsd:complexType>
>    </xsd:element>
>   </xsd:sequence>
>  </xsd:complexType>
>
>  <!-- Stock Keeping Unit, a code for identifying products -->
>  <xsd:simpleType name="SKU">
>   <xsd:restriction base="xsd:string">
>    <xsd:pattern value="\d{3}-[A-Z]{2}"/>
>   </xsd:restriction>
>  </xsd:simpleType>
>
> </xsd:schema>
>
> ---
> here is the code. I modified XBeansXPath.java and XBeansXPathAdv.java, 
> and
> added XBeansNamespace.java
>
> package org.apache.xmlbeans.impl.xpath.jaxen;
> ...
> public class XBeansXPath extends BaseXPath
> {
>     ....
>     public List selectNodes(Object node) throws JaxenException
>     {
>         XmlCursor xc;
>  XmlObject xmlObj;
>         if (node instanceof XmlObject)
>         {
>      xmlObj = (XmlObject)node;
>      xc = xmlObj.newCursor();
>         }
>         else if (node instanceof XmlCursor)
>  {
>      xc = (XmlCursor)node;
>      xmlObj = xc.getObject();
>             xc = xc.newCursor();
>         }
>         else
>             throw new IllegalArgumentException("node must be an 
> XmlObject or
> an XmlCursor, found: " + node.getClass());
>
>  XBeansNamespace.addNamespaces(this, xmlObj); // nn (support xpath 
> including
> namespace)
>         ((XBeansNavigator)getNavigator()).setCursor(xc);
>         return new ListImpl(super.selectNodes(
> XBeansNavigator.getBookmarkInThisPlace(xc) ));
>     }
> ....
> }
> ----
> package org.apache.xmlbeans.impl.xpath.jaxen;
> ...
> public class XBeansXPathAdv
>     extends BaseXPath
>     implements JaxenXBeansDelegate.SelectPathInterface
> {
> ...
>     public List selectNodes(Object node) throws JaxenException
>     {
>         XmlCursor xc;
>         xc = ((XmlCursor)node);
>  XmlObject xmlObj = xc.getObject();
>  XBeansNamespace.addNamespaces(this, xmlObj);
>         ((XBeansNavigator)getNavigator()).setCursor(xc);
>         return super.selectNodes(
> XBeansNavigator.getBookmarkInThisPlace(xc) );
>     }
> ...
> }
> -----
> package org.apache.xmlbeans.impl.xpath.jaxen;
> import org.jaxen.BaseXPath;
> import org.jaxen.JaxenException;
> import org.apache.xmlbeans.XmlObject;
> import org.apache.xmlbeans.XmlCursor;
>
> import java.util.List;
> import java.util.Map;
> import java.util.HashMap;
> import java.util.Iterator;
>
> public class XBeansNamespace
> {
>     public static void addNamespaces(BaseXPath baseXPath, XmlObject 
> doc)
> throws JaxenException {
>  HashMap nslist = getAdditionalNamespaces(getFirstChild(doc));
>  for (Iterator iter = nslist.entrySet().iterator(); iter.hasNext(); ) {
>      Map.Entry entry = (Map.Entry)iter.next();
>      String prefix = (String)entry.getKey();
>      String uri = (String)entry.getValue();
>      System.out.println(">> addNamespaces: prefix: "+prefix+", uri: 
> "+uri);
>      baseXPath.addNamespace(prefix, uri);
>  }
>     }
>     private static XmlObject getFirstChild(XmlObject xmlObj) {
>  XmlCursor xc = xmlObj.newCursor();
>  try {
>      xc.toFirstChild();
>      return xc.getObject();
>  } finally {
>      xc.dispose();
>  }
>     }
>     private static HashMap getAdditionalNamespaces(XmlObject xmlObj) {
>  HashMap nslist = new HashMap();
>  XmlCursor xc = xmlObj.newCursor();
>  try {
>      xc.getAllNamespaces(nslist);
>  } finally {
>      xc.dispose();
>  }
>  return nslist;
>     }
> }
>
>
>
> - ---------------------------------------------------------------------
> To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
> Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/
>


- ---------------------------------------------------------------------
To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/