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 03:22:33 GMT
> 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/


Mime
View raw message