xml-xmlbeans-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "nn" <nna...@comcast.net>
Subject Re: A New approach for XPath using Namespace
Date Wed, 23 Jun 2004 15:32:01 GMT
I'm considering a case where xpath is applied in a loop.

like:
while (iter.hasNext()) {
  Option option = new Option();
  option.setNamespace("prefix", http://a/a/);
  ... // more environment setting
  XmlObject[] xs = ((XmlObject)iter.next()).selectPath("./a/b[@c < $a]",
i++, option);
 ..
}
(though variable binding is not currently supported)
In general, content may want to use full environment which may be defined
XQuery..

The above example is not good style.
it should be something like:

XPath xpath = new XPath("./a/b[@c < $a]");
..
xpath.setNamespace("a", "http://a/bb");
... // more environment setting
while (iter.hasNext()) {
  XmlObject[] xs = xpath.evaluate(((XmlObject)iter.next(), i++);
 ..
}

I think this is a quite standard approach for this kind of thing. xpath is
also 'compiled' from the string.
This is what I mentionaled as ineffieicnt.

> side effect would be the trouble of try and anticipate the namespace
> prefixes that are generated, something that can only be determined at
> runtime.  This means that if the algo. changes for determining default

The XPath query only depends on URI context (e.g, http://a/abb) not on
prefix.
So even if target document uses different prefix for the same URI, the XPath
can handle the different prefix in the target document. So as long as A is
supporsed to be an element of targetNamespace of the XMLSchema, this meaning
does not depends on runtime target prefix(namespace) definition (see JAXEN
document I refered before). Since xsd definition is static, XPath can rely
on the information in xsd file.
If we do not have XML Schema, this approach cannot be taken. That case, we
still need to provide explicit namespace defnition using some api outside of
xpath.
When we do handle simple Xml document associated with XMLSchema, this
approach seems quite natural.

nn

----- Original Message ----- 
From: "Noah Campbell" <noahcampbell@gmail.com>
To: <xmlbeans-dev@xml.apache.org>
Sent: Wednesday, June 23, 2004 5:13 AM
Subject: Re: A New approach for XPath using Namespace


> If I understand you correctly, the declaration of a namespace for a
> XPath Query should be avoided and the repeated namespace declarations
> are inefficient.
>
> I'd be curious to see how your proposed context base approach would
> improve efficiency.  Other than saving some keystrokes when you
> declare the select path query, it'd be just the same.  In fact the
> side effect would be the trouble of try and anticipate the namespace
> prefixes that are generated, something that can only be determined at
> runtime.  This means that if the algo. changes for determining default
> prefixes, all previous queries need to be rewritten (or at least
> tested) to make sure they still work.  If you make the declarations up
> front and explicitly declare your namespaces and their corresponding
> prefixes, you're assured that your query will always work.
>
> Noah
>
> On Tue, 22 Jun 2004 22:57:16 -0700, nn <nnakae@comcast.net> wrote:
> >
> > This may not resolve general case, but can be used as convenient option
> > which can be used for majority of cases.
> > It would be still necesary to provide namespace declaration in general.
> > but I think it is better to use context rathar than using special
syntax.
> > Context based approach is more efficient(we can avoid repeated namespace
> > declaration in 'Xpath'), and provide a way to associate values to the
> > variables in a XML expression.
> > I'm actually thinking to use Jaxen directly rathar than using XMLBeans's
> > XPath API.
> >
> > Also this does not apply for none xml schema assocaited XML.
> > But XMLBeans is for XMLSchema bound XML. If we do handle XML without
> > binding, there are a lot of other choise, we do not have to use
XMLBeans.
> >
> > nn
> >
> >
> >
> >
> > ----- Original Message -----
> > From: "Eric Vasilik" <ericvas@bea.com>
> > To: <xmlbeans-dev@xml.apache.org>
> > Sent: Tuesday, June 22, 2004 10:35 PM
> > Subject: RE: A New approach for XPath using Namespace
> >
> > Pathing has nothing to do with Schema or generated classes.  XmlBeans
> > can be used with out compiling a Schema, and operate on "raw" XML.  The
> > feature of selectPath should not be dependent on the presence of a
> > Schema.
> >
> > I can see where a user can supply a prefix resolver, obtained from a
> > source of his choice, for the execution of a path.  And, we can add a
> > syntax to xpath for declaratively specifying the prefix mappings.
> >
> > But, trying to pull information from some Schema will not work.
> > Consider the case where some typed XML is bound from several schemas.
> > Which one will you use?  The interpretation of a path must be
> > independent from any Schema.
> >
> > - Eric
> >
> > -----Original Message-----
> > From: nn [mailto:nnakae@comcast.net]
> > Sent: Tuesday, June 22, 2004 8:23 PM
> > To: xmlbeans-dev@xml.apache.org
> > Subject: Re: A New approach for XPath using Namespace
> >
> > > What if your namespace is http://a.b.c/c/d.xsd?  Do you include d.xsd
> > > as the namespace?  Is this even proper?
> >
> > This portion is kind of optional, there is no good guidance for choosing
> > prefix from uri.
> > So for instance we may remove .xsd.
> > I think, using default namespace would be more appropriate and
> > convenient.
> > nn
> >
> > >
> > > Noah
> > >
> > > On Tue, 22 Jun 2004 17:50:21 -0700, nn <nnakae@comcast.net> wrote:
> > > >
> > > > Hi,
> > > > I come up with the new solution for namespace resolution for XPath
> > in
> > > > XMLBeans.
> > > > the idea is to use taget document's targetNamespace information in
> > XML
> > > > Schema(stored in schema class in XMLBeans generated class for XPath.
> > > > for instance, selectPath will internally use PurchaseOrderDocument
> > to
> > get
> > > > the targetNamespace information and use it for the default namespace
> > for
> > the
> > > > XPath.
> > > >
> > > >     PurchaseOrderDocument doc =
> > PurchaseOrderDocument.Factory.parse(new
> > > > File("xml/data/po.xml"));
> > > >     doc.selectPath("./purchaseOrder/shipTo[name = 'Helen Zoe']");
> > > >
> > > > This will be processed successfully.
> > > > And if URI is something like http://a.b.c/d/e, it will use e as the
> > prefix
> > > > for this
> > > > namespace.
> > > >
> > > > so
> > > >     doc.selectPath("./po:purchaseOrder/po:shipTo[po:name = 'Helen
> > Zoe']");
> > > > is also accepted.
> > > >
> > > > This is a convention, may not have to be supported.
> > > > I think this approach is quite appropriate for XML Schema assoiated
> > > > document.
> > > > Getting prefix information from target document are not so
> > satisfactory
> > for
> > > > it depends on the proper prefix usage of document.
> > > > This approach does not depend on the choice of prefix in the target
> > > > document.
> > > >
> > > > in order to achieve this, XMLBean code generation must be changed so
> > that it
> > > > provides the targetNamespace information in the generated class.
> > > > The current code generation does not maintaing this information.
> > > >
> > > > Also I found a minor problem of JAXEN, we need to change a line so
> > that
> > it
> > > > can handle default namaespace.
> > > >
> > > > nn
> > > >
> > > > followings are the relevent codes.
> > > >
> > > > -----
> > > >
> > > > package xquery.engine.impl;
> > > >
> > > > import java.io.File;
> > > > import org.apache.xmlbeans.XmlObject;
> > > > import com.example.po.*;
> > > >
> > > > public class POTest {
> > > >     private static String[] test_xpathes = new String[]{
> > > >   "//po:purchaseOrder",
> > > >   "./po:purchaseOrder/po:shipTo/po:name",
> > > >   "./po:purchaseOrder/po:shipTo[po:name = 'Helen Zoe']",
> > > >   "//purchaseOrder",
> > > >   "./purchaseOrder/shipTo/name",
> > > >   "./purchaseOrder/shipTo[name = 'Helen Zoe']",
> > > >   };
> > > >
> > > >     public static void test() throws Throwable {
> > > >  PurchaseOrderDocument doc = PurchaseOrderDocument.Factory.parse(new
> > > > File("xml/data/po.xml"));
> > > >  for (int i = 0; i < test_xpathes.length; i++) {
> > > >      String xpath = test_xpathes[i];
> > > >      System.out.println("\n===== test: xpath: "+xpath+" =====");
> > > >      filter(doc.selectPath(xpath));
> > > >  }
> > > >     }
> > > >     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);
> > > >  }
> > > >     }
> > > >
> > > >     public static void main(String argv[]) {
> > > >  try {
> > > >      test();
> > > >  } catch (Throwable err) {
> > > >      err.printStackTrace();
> > > >  }
> > > >     }
> > > > }
> > > >
> > > > -------
> > > >
> > > > package org.apache.xmlbeans.impl.xpath.jaxen;
> > > >
> > > > public class XBeansNamespace
> > > > {
> > > >     public static void addNamespaces(BaseXPath baseXPath, XmlObject
> > doc)
> > > > throws JaxenException {
> > > >  SchemaTypeImpl sh = (SchemaTypeImpl)doc.schemaType();
> > > >
> > > >  // in order to get targetNamespace, xmlbean code generation must be
> > > > modified slightly
> > > >  String uri = sh.getTargetNamespace();
> > > >
> > > >  // System.out.println(">> default namespace: "+uri);
> > > >  if (uri != null) {
> > > >      // default uri.
> > > >      // in order to use default uri, a minor fix of jaxen is
> > required.
> > > >      baseXPath.addNamespace("", uri);
> > > >
> > > >      // from uri, http://a.b.c/c/d, use d for the prefix
> > > >      // this is a common convention. may not have to be added..
> > > >      int idx = uri.lastIndexOf("/");
> > > >      if (idx >= 0) {
> > > >   String prefix = uri.substring(idx+1);
> > > >   baseXPath.addNamespace(prefix, uri);
> > > >      }
> > > >  }
> > > >     }
> > > >
> > > > }
> > > >
> > > > -------
> > > > 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
> > > >
> > > >     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) );
> > > >     }
> > > >
> > > > -----
> > > >
> > > > SchemaTypeCodePrinter.java
> > > >
> > > > package org.apache.xmlbeans.impl.schema;
> > > > ....
> > > >
> > > >    // nn, store target namespace uri of document
> > > >     void printSetParseContext(SchemaType sType) throws IOException {
> > > >         String shortName = sType.getShortJavaImplName();
> > > >         String baseClass = getBaseClass(sType);
> > > >  System.out.println(">> printsetParseContext, shortName:
> > "+shortName+",
> > > > baseClass: "+baseClass);
> > > >  if (sType.isDocumentType()) {
> > > >      QName name = sType.getDocumentElementName();
> > > >      String uri = name.getNamespaceURI();
> > > >      System.out.println(">> printsetParseContext, name: "+name+",
> > uri:
> > > > "+uri);
> > > >      if (uri != null && !uri.equals("")) {
> > > >   emit("org.apache.xmlbeans.impl.schema.SchemaTypeImpl sh =
> > > > (org.apache.xmlbeans.impl.schema.SchemaTypeImpl)schemaType();"); //
> > nn
> > > >   emit("sh.setParseContext(null, \""+uri+"\", false, false);"); //
> > nn
> > > >      }
> > > >  }
> > > >     }
> > > >     void printConstructor(SchemaType sType, String shortName) throws
> > > > IOException {
> > > >         emit("");
> > > >         emit("public " + shortName +
> > "(org.apache.xmlbeans.SchemaType
> > > > sType)");
> > > >         startBlock();
> > > >         emit("super(sType" + (sType.getSimpleVariety() ==
> > > > SchemaType.NOT_SIMPLE ?
> > > >                              "":
> > > >                              ", " + !sType.isSimpleType()) +
> > > >              ");");
> > > >  printSetParseContext(sType); // nn
> > > >         endBlock();
> > > >
> > > >         if (sType.getSimpleVariety() != SchemaType.NOT_SIMPLE)
> > > >         {
> > > >             emit("");
> > > >             emit("protected " + shortName +
> > "(org.apache.xmlbeans.SchemaType
> > > > sType, boolean b)");
> > > >             startBlock();
> > > >             emit("super(sType, b);");
> > > >             printSetParseContext(sType); // nn
> > > >             endBlock();
> > > >         }
> > > >     }
> > > >
> > > > ----
> > > > Jaxen (temporary) bug fix: to support defialy namespace:
> > > > package org.jaxen.expr;
> > > > ..
> > > > public class DefaultNameStep extends DefaultStep implements NameStep
> > {
> > > > ...
> > > >     public DefaultNameStep(IterableAxis axis,
> > > >                            String prefix,
> > > >                            String localName,
> > > >                            PredicateSet predicateSet) {
> > > >         super(axis, predicateSet);
> > > >
> > > >         this.prefix = prefix;
> > > >         this.localName = localName;
> > > >         this.matchesAnyName = "*".equals(localName);
> > > >  //        this.hasPrefix = (this.prefix != null &&
> > this.prefix.length()
> > >
> > > > 0); // nn
> > > >         this.hasPrefix = true; // should check default uri is
> > defined or
> > not
> > > >     }
> > > >
> > > > -
> > ---------------------------------------------------------------------
> > > > 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/
> >
> >
>
> - ---------------------------------------------------------------------
> 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/


Mime
View raw message