You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@xmlbeans.apache.org by Pascal Maugeri <pa...@gmail.com> on 2008/02/22 17:39:30 UTC
Applying XPath to an XML with or without namespace
Hi
I would like to have some clarifications on how to apply XPath query on a
XML document depending if it comes with or without a namespace definition.
When I run the following code executing the XPath query /a/b on a document
with no namespace:
String doc = "<a><b/></a>";
String query = "/a/b";
XmlObject xml = XmlObject.Factory.parse(doc);
XmlCursor cursor = xml.newCursor();
cursor.push();
cursor.selectPath(query);
it gives me the correct result (<c/>).
But if if the document comes with a namespace, for instance:
<a xmlns="mynamespace">
<b/>
</a>
the same code does not give me any result.
After investigating I found that a working code could be:
String doc = "<a xmlns="mynamespace"><b/></a>";
String query = "declare namespace xs='mynamespace';/xs:a/xs:b";
XmlObject xml = XmlObject.Factory.parse(doc);
XmlCursor cursor = xml.newCursor();
cursor.push();
cursor.selectPath(query);
Is there a better way to do this without having to set the prefix "xs:" in
the Xpath query for each level ? I would like to find a generic way to
handle XML documents with or without namespace.
Regards,
Pascal
Re: Applying XPath to an XML with or without namespace
Posted by Psoroulas John <ps...@telecom.ntua.gr>.
Pascal,
here is an example,
suppose that you want to locate the element "b" which is a child of element
"a", the xpath expression should be written as follows:
//*[local-name()="a"]/*[local-name()="b"]
Regards,
John
On Mon, February 25, 2008 12:43, Pascal Maugeri wrote:
> Hi John
>
>
> I see what you mean but could give me an example of a query with
> local-name() ? Thank you it is very interesting.
>
>
> Regards,
> Pascal
>
>
> On Sat, Feb 23, 2008 at 11:21 AM, Psoroulas John
> <ps...@telecom.ntua.gr>
> wrote:
>
>
>> Pascal,
>>
>>
>> the xpath function "local-name()" is may the proper one for your
>> purposes, take a look.
>>
>> Regards,
>>
>>
>> John
>>
>>
>>
>>
>> On Fri, February 22, 2008 21:14, Wing Yew Poon wrote:
>>
>>> Sorry, typo - the query should have been "declare default element
>>> namespace 'mynamespace'; /a/b".
>>>
>>> ________________________________
>>>
>>>
>>>
>>> From: Wing Yew Poon [mailto:wpoon@bea.com]
>>> Sent: Friday, February 22, 2008 11:11 AM
>>> To: user@xmlbeans.apache.org
>>> Subject: RE: Applying XPath to an XML with or without namespace
>>>
>>>
>>>
>>>
>>> Pascal,
>>> in <a xmlns="mynamespace"> <b/>
>>> </a>
>>> what you have is every element within and including a being qualified
>>> as having the namespace "mynamespace". Thus, the XPath must qualify
>>> the elements too. This is only logical. There are a couple of ways you
>>> can qualify the elements in the XPath. You can use the prolog you have
>>> in "declare namespace xs='mynamespace';/xs:a/xs:b"; you can also use
>>> "declare default namespace 'mynamespace; /a/b".
>>> The bottom line is that the path must follow the structure of the
>>> document. If elements/attributes are namespace-qualified in the
>> document,
>>> then they must be too in the path. - Wing Yew
>>>
>>>
>>>
>>> ________________________________
>>>
>>>
>>>
>>> From: Pascal Maugeri [mailto:pascal.maugeri@gmail.com]
>>> Sent: Friday, February 22, 2008 8:40 AM
>>> To: user@xmlbeans.apache.org
>>> Subject: Applying XPath to an XML with or without namespace
>>>
>>>
>>>
>>>
>>> Hi
>>>
>>>
>>>
>>> I would like to have some clarifications on how to apply XPath query
>>> on a XML document depending if it comes with or without a namespace
>>> definition.
>>>
>>> When I run the following code executing the XPath query /a/b on a
>>> document with no namespace:
>>>
>>> String doc = "<a><b/></a>";
>>> String query = "/a/b";
>>> XmlObject xml = XmlObject.Factory.parse(doc);
>>> XmlCursor cursor = xml.newCursor();
>>> cursor.push(); cursor.selectPath(query);
>>>
>>> it gives me the correct result (<c/>).
>>>
>>> But if if the document comes with a namespace, for instance:
>>>
>>>
>>>
>>> <a xmlns="mynamespace">
>>> <b/>
>>> </a>
>>>
>>>
>>>
>>> the same code does not give me any result.
>>>
>>> After investigating I found that a working code could be:
>>>
>>>
>>>
>>> String doc = "<a xmlns="mynamespace"><b/></a>";
>>> String query = "declare namespace xs='mynamespace';/xs:a/xs:b";
>>> XmlObject xml = XmlObject.Factory.parse(doc);
>>> XmlCursor cursor = xml.newCursor();
>>> cursor.push(); cursor.selectPath(query);
>>>
>>> Is there a better way to do this without having to set the prefix
>>> "xs:"
>>> in the Xpath query for each level ? I would like to find a generic way
>>>
>> to
>>> handle XML documents with or without namespace.
>>>
>>> Regards,
>>> Pascal
>>>
>>>
>>>
>>> Notice: This email message, together with any attachments, may
>>> contain information of BEA Systems, Inc., its subsidiaries and
>>> affiliated
>> entities,
>>> that may be confidential, proprietary, copyrighted and/or legally
>>> privileged, and is intended solely for the use of the individual or
>> entity
>>> named in this message. If you are not the intended recipient, and
>>> have received this message in error, please immediately return this by
>>> email and then delete it.
>>>
>>> Notice: This email message, together with any attachments, may
>>> contain information of BEA Systems, Inc., its subsidiaries and
>>> affiliated entities, that may be confidential, proprietary,
>>> copyrighted and/or legally privileged, and is intended solely for the
>>> use of the individual or entity named in this message. If you are not
>>> the intended recipient, and have received this message in error,
>>> please immediately return this by email and then delete it.
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
>> For additional commands, e-mail: user-help@xmlbeans.apache.org
>>
>>
>>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org
VTD-XML 2.3
Posted by jz...@ximpleware.com.
VTD-XML 2.3 (http://vtd-xml.sf.net) is now released. To download the latest
version please visit
http://sourceforge.net/project/showfiles.php?group_id=110612&package_id=1201
72.
Below is a list of new features and enhancements in this version.
* VTDException is now introduced as the root class for all other VTD-XML's
exception classes (per suggestion of Max Rahder).
* Transcoding capability is now added for inter-document cut and paste. You
can cut a chuck of bytes in a UTF-8 encoded document and paste it into a
UTF-16 encoded document and the output document is still well-formed.
* ISO-8859-10, ISO-8859-11, ISO-8859-12, ISO-8859-13, ISO-8859-14 and
ISO-8859-15 support has now been added
* Zero length Text node is now possible.
* Ability to dump in-memory copy of text is added.
* Various code cleanup, enhancement and bug fixes.
Below are some new articles related to VTD-XML
*Index XML documents with VTD-XML http://xml.sys-con.com/read/453082.htm
*Manipulate XML content the Ximple Way http://www.devx.com/xml/Article/36379
* VTD-XML: A new vision of XML
http://www.developer.com/xml/article.php/3714051
* VTD-XML: XML Processing for the future
http://www.codeproject.com/KB/cs/vtd-xml_examples.aspx
If you (or someone you know) like the concept of VTD-XML, think that it can
help solve enterprises' XML processing related issues (particularly those
related to SOA), and would like to directly influence and contribute to the
development of the future of Internet, please email me
(crackeur@comcast.net). We are looking for open source software developers
and project management people to take VTD-XML to the next level.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org
Re: Applying XPath to an XML with or without namespace
Posted by Pascal Maugeri <pa...@gmail.com>.
Hi John
I see what you mean but could give me an example of a query with
local-name() ?
Thank you it is very interesting.
Regards,
Pascal
On Sat, Feb 23, 2008 at 11:21 AM, Psoroulas John <ps...@telecom.ntua.gr>
wrote:
> Pascal,
>
> the xpath function "local-name()" is may the proper one for your purposes,
> take a look.
>
> Regards,
>
> John
>
>
>
> On Fri, February 22, 2008 21:14, Wing Yew Poon wrote:
> > Sorry, typo - the query should have been "declare default element
> > namespace 'mynamespace'; /a/b".
> >
> > ________________________________
> >
> >
> > From: Wing Yew Poon [mailto:wpoon@bea.com]
> > Sent: Friday, February 22, 2008 11:11 AM
> > To: user@xmlbeans.apache.org
> > Subject: RE: Applying XPath to an XML with or without namespace
> >
> >
> >
> > Pascal,
> > in <a xmlns="mynamespace">
> > <b/>
> > </a>
> > what you have is every element within and including a being qualified as
> > having the namespace "mynamespace". Thus, the XPath must qualify the
> > elements too. This is only logical. There are a couple of ways you can
> > qualify the elements in the XPath. You can use the prolog you have in
> > "declare namespace xs='mynamespace';/xs:a/xs:b"; you can also use
> > "declare default namespace 'mynamespace; /a/b".
> > The bottom line is that the path must follow the structure of the
> > document. If elements/attributes are namespace-qualified in the
> document,
> > then they must be too in the path. - Wing Yew
> >
> >
> >
> > ________________________________
> >
> >
> > From: Pascal Maugeri [mailto:pascal.maugeri@gmail.com]
> > Sent: Friday, February 22, 2008 8:40 AM
> > To: user@xmlbeans.apache.org
> > Subject: Applying XPath to an XML with or without namespace
> >
> >
> >
> > Hi
> >
> >
> > I would like to have some clarifications on how to apply XPath query on
> > a XML document depending if it comes with or without a namespace
> > definition.
> >
> > When I run the following code executing the XPath query /a/b on a
> > document with no namespace:
> >
> > String doc = "<a><b/></a>";
> > String query = "/a/b";
> > XmlObject xml = XmlObject.Factory.parse(doc);
> > XmlCursor cursor = xml.newCursor();
> > cursor.push(); cursor.selectPath(query);
> >
> > it gives me the correct result (<c/>).
> >
> > But if if the document comes with a namespace, for instance:
> >
> >
> > <a xmlns="mynamespace">
> > <b/>
> > </a>
> >
> >
> > the same code does not give me any result.
> >
> > After investigating I found that a working code could be:
> >
> >
> > String doc = "<a xmlns="mynamespace"><b/></a>";
> > String query = "declare namespace xs='mynamespace';/xs:a/xs:b";
> > XmlObject xml = XmlObject.Factory.parse(doc);
> > XmlCursor cursor = xml.newCursor();
> > cursor.push(); cursor.selectPath(query);
> >
> > Is there a better way to do this without having to set the prefix "xs:"
> > in the Xpath query for each level ? I would like to find a generic way
> to
> > handle XML documents with or without namespace.
> >
> > Regards,
> > Pascal
> >
> >
> > Notice: This email message, together with any attachments, may contain
> > information of BEA Systems, Inc., its subsidiaries and affiliated
> entities,
> > that may be confidential, proprietary, copyrighted and/or legally
> > privileged, and is intended solely for the use of the individual or
> entity
> > named in this message. If you are not the intended recipient, and have
> > received this message in error, please immediately return this by email
> > and then delete it.
> >
> > Notice: This email message, together with any attachments, may contain
> > information of BEA Systems, Inc., its subsidiaries and affiliated
> > entities, that may be confidential, proprietary, copyrighted and/or
> > legally privileged, and is intended solely for the use of the individual
> > or entity named in this message. If you are not the intended recipient,
> > and have received this message in error, please immediately return this
> > by email and then delete it.
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
> For additional commands, e-mail: user-help@xmlbeans.apache.org
>
>
RE: Applying XPath to an XML with or without namespace
Posted by Psoroulas John <ps...@telecom.ntua.gr>.
Pascal,
the xpath function "local-name()" is may the proper one for your purposes,
take a look.
Regards,
John
On Fri, February 22, 2008 21:14, Wing Yew Poon wrote:
> Sorry, typo - the query should have been "declare default element
> namespace 'mynamespace'; /a/b".
>
> ________________________________
>
>
> From: Wing Yew Poon [mailto:wpoon@bea.com]
> Sent: Friday, February 22, 2008 11:11 AM
> To: user@xmlbeans.apache.org
> Subject: RE: Applying XPath to an XML with or without namespace
>
>
>
> Pascal,
> in <a xmlns="mynamespace">
> <b/>
> </a>
> what you have is every element within and including a being qualified as
> having the namespace "mynamespace". Thus, the XPath must qualify the
> elements too. This is only logical. There are a couple of ways you can
> qualify the elements in the XPath. You can use the prolog you have in
> "declare namespace xs='mynamespace';/xs:a/xs:b"; you can also use
> "declare default namespace 'mynamespace; /a/b".
> The bottom line is that the path must follow the structure of the
> document. If elements/attributes are namespace-qualified in the document,
> then they must be too in the path. - Wing Yew
>
>
>
> ________________________________
>
>
> From: Pascal Maugeri [mailto:pascal.maugeri@gmail.com]
> Sent: Friday, February 22, 2008 8:40 AM
> To: user@xmlbeans.apache.org
> Subject: Applying XPath to an XML with or without namespace
>
>
>
> Hi
>
>
> I would like to have some clarifications on how to apply XPath query on
> a XML document depending if it comes with or without a namespace
> definition.
>
> When I run the following code executing the XPath query /a/b on a
> document with no namespace:
>
> String doc = "<a><b/></a>";
> String query = "/a/b";
> XmlObject xml = XmlObject.Factory.parse(doc);
> XmlCursor cursor = xml.newCursor();
> cursor.push(); cursor.selectPath(query);
>
> it gives me the correct result (<c/>).
>
> But if if the document comes with a namespace, for instance:
>
>
> <a xmlns="mynamespace">
> <b/>
> </a>
>
>
> the same code does not give me any result.
>
> After investigating I found that a working code could be:
>
>
> String doc = "<a xmlns="mynamespace"><b/></a>";
> String query = "declare namespace xs='mynamespace';/xs:a/xs:b";
> XmlObject xml = XmlObject.Factory.parse(doc);
> XmlCursor cursor = xml.newCursor();
> cursor.push(); cursor.selectPath(query);
>
> Is there a better way to do this without having to set the prefix "xs:"
> in the Xpath query for each level ? I would like to find a generic way to
> handle XML documents with or without namespace.
>
> Regards,
> Pascal
>
>
> Notice: This email message, together with any attachments, may contain
> information of BEA Systems, Inc., its subsidiaries and affiliated entities,
> that may be confidential, proprietary, copyrighted and/or legally
> privileged, and is intended solely for the use of the individual or entity
> named in this message. If you are not the intended recipient, and have
> received this message in error, please immediately return this by email
> and then delete it.
>
> Notice: This email message, together with any attachments, may contain
> information of BEA Systems, Inc., its subsidiaries and affiliated
> entities, that may be confidential, proprietary, copyrighted and/or
> legally privileged, and is intended solely for the use of the individual
> or entity named in this message. If you are not the intended recipient,
> and have received this message in error, please immediately return this
> by email and then delete it.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org
Re: Applying XPath to an XML with or without namespace
Posted by Pascal Maugeri <pa...@gmail.com>.
It works as expected.
Thanks for your help !
Regards,
Pascal
On Fri, Feb 22, 2008 at 8:14 PM, Wing Yew Poon <wp...@bea.com> wrote:
> Sorry, typo - the query should have been "declare default element
> namespace 'mynamespace'; /a/b".
>
> ------------------------------
> *From:* Wing Yew Poon [mailto:wpoon@bea.com]
> *Sent:* Friday, February 22, 2008 11:11 AM
> *To:* user@xmlbeans.apache.org
> *Subject:* RE: Applying XPath to an XML with or without namespace
>
> Pascal,
> in
> <a xmlns="mynamespace">
> <b/>
> </a>
> what you have is every element within and including a being qualified as
> having the namespace "mynamespace". Thus, the XPath must qualify the
> elements too. This is only logical.
> There are a couple of ways you can qualify the elements in the XPath. You
> can use the prolog you have in
> "declare namespace xs='mynamespace';/xs:a/xs:b"; you can also use "declare
> default namespace 'mynamespace; /a/b".
> The bottom line is that the path must follow the structure of the
> document. If elements/attributes are namespace-qualified in the document,
> then they must be too in the path.
> - Wing Yew
>
> ------------------------------
> *From:* Pascal Maugeri [mailto:pascal.maugeri@gmail.com]
> *Sent:* Friday, February 22, 2008 8:40 AM
> *To:* user@xmlbeans.apache.org
> *Subject:* Applying XPath to an XML with or without namespace
>
> Hi
>
> I would like to have some clarifications on how to apply XPath query on a
> XML document depending if it comes with or without a namespace definition.
>
> When I run the following code executing the XPath query /a/b on a document
> with no namespace:
>
> String doc = "<a><b/></a>";
> String query = "/a/b";
> XmlObject xml = XmlObject.Factory.parse(doc);
> XmlCursor cursor = xml.newCursor();
> cursor.push();
> cursor.selectPath(query);
>
> it gives me the correct result (<c/>).
>
> But if if the document comes with a namespace, for instance:
>
> <a xmlns="mynamespace">
> <b/>
> </a>
>
> the same code does not give me any result.
>
> After investigating I found that a working code could be:
>
> String doc = "<a xmlns="mynamespace"><b/></a>";
> String query = "declare namespace xs='mynamespace';/xs:a/xs:b";
> XmlObject xml = XmlObject.Factory.parse(doc);
> XmlCursor cursor = xml.newCursor();
> cursor.push();
> cursor.selectPath(query);
>
> Is there a better way to do this without having to set the prefix "xs:" in
> the Xpath query for each level ? I would like to find a generic way to
> handle XML documents with or without namespace.
>
> Regards,
> Pascal
>
> Notice: This email message, together with any attachments, may contain
> information of BEA Systems, Inc., its subsidiaries and affiliated entities,
> that may be confidential, proprietary, copyrighted and/or legally
> privileged, and is intended solely for the use of the individual or entity
> named in this message. If you are not the intended recipient, and have
> received this message in error, please immediately return this by email and
> then delete it.
>
> Notice: This email message, together with any attachments, may contain
> information of BEA Systems, Inc., its subsidiaries and affiliated entities,
> that may be confidential, proprietary, copyrighted and/or legally
> privileged, and is intended solely for the use of the individual or entity
> named in this message. If you are not the intended recipient, and have
> received this message in error, please immediately return this by email and
> then delete it.
>
RE: Applying XPath to an XML with or without namespace
Posted by Wing Yew Poon <wp...@bea.com>.
Sorry, typo - the query should have been "declare default element
namespace 'mynamespace'; /a/b".
________________________________
From: Wing Yew Poon [mailto:wpoon@bea.com]
Sent: Friday, February 22, 2008 11:11 AM
To: user@xmlbeans.apache.org
Subject: RE: Applying XPath to an XML with or without namespace
Pascal,
in
<a xmlns="mynamespace">
<b/>
</a>
what you have is every element within and including a being qualified as
having the namespace "mynamespace". Thus, the XPath must qualify the
elements too. This is only logical.
There are a couple of ways you can qualify the elements in the XPath.
You can use the prolog you have in
"declare namespace xs='mynamespace';/xs:a/xs:b"; you can also use
"declare default namespace 'mynamespace; /a/b".
The bottom line is that the path must follow the structure of the
document. If elements/attributes are namespace-qualified in the
document, then they must be too in the path.
- Wing Yew
________________________________
From: Pascal Maugeri [mailto:pascal.maugeri@gmail.com]
Sent: Friday, February 22, 2008 8:40 AM
To: user@xmlbeans.apache.org
Subject: Applying XPath to an XML with or without namespace
Hi
I would like to have some clarifications on how to apply XPath query on
a XML document depending if it comes with or without a namespace
definition.
When I run the following code executing the XPath query /a/b on a
document with no namespace:
String doc = "<a><b/></a>";
String query = "/a/b";
XmlObject xml = XmlObject.Factory.parse(doc);
XmlCursor cursor = xml.newCursor();
cursor.push();
cursor.selectPath(query);
it gives me the correct result (<c/>).
But if if the document comes with a namespace, for instance:
<a xmlns="mynamespace">
<b/>
</a>
the same code does not give me any result.
After investigating I found that a working code could be:
String doc = "<a xmlns="mynamespace"><b/></a>";
String query = "declare namespace xs='mynamespace';/xs:a/xs:b";
XmlObject xml = XmlObject.Factory.parse(doc);
XmlCursor cursor = xml.newCursor();
cursor.push();
cursor.selectPath(query);
Is there a better way to do this without having to set the prefix "xs:"
in the Xpath query for each level ? I would like to find a generic way
to handle XML documents with or without namespace.
Regards,
Pascal
Notice: This email message, together with any attachments, may contain
information of BEA Systems, Inc., its subsidiaries and affiliated
entities, that may be confidential, proprietary, copyrighted and/or
legally privileged, and is intended solely for the use of the individual
or entity named in this message. If you are not the intended recipient,
and have received this message in error, please immediately return this
by email and then delete it.
Notice: This email message, together with any attachments, may contain information of BEA Systems, Inc., its subsidiaries and affiliated entities, that may be confidential, proprietary, copyrighted and/or legally privileged, and is intended solely for the use of the individual or entity named in this message. If you are not the intended recipient, and have received this message in error, please immediately return this by email and then delete it.
RE: Applying XPath to an XML with or without namespace
Posted by Wing Yew Poon <wp...@bea.com>.
Pascal,
in
<a xmlns="mynamespace">
<b/>
</a>
what you have is every element within and including a being qualified as
having the namespace "mynamespace". Thus, the XPath must qualify the
elements too. This is only logical.
There are a couple of ways you can qualify the elements in the XPath.
You can use the prolog you have in
"declare namespace xs='mynamespace';/xs:a/xs:b"; you can also use
"declare default namespace 'mynamespace; /a/b".
The bottom line is that the path must follow the structure of the
document. If elements/attributes are namespace-qualified in the
document, then they must be too in the path.
- Wing Yew
________________________________
From: Pascal Maugeri [mailto:pascal.maugeri@gmail.com]
Sent: Friday, February 22, 2008 8:40 AM
To: user@xmlbeans.apache.org
Subject: Applying XPath to an XML with or without namespace
Hi
I would like to have some clarifications on how to apply XPath query on
a XML document depending if it comes with or without a namespace
definition.
When I run the following code executing the XPath query /a/b on a
document with no namespace:
String doc = "<a><b/></a>";
String query = "/a/b";
XmlObject xml = XmlObject.Factory.parse(doc);
XmlCursor cursor = xml.newCursor();
cursor.push();
cursor.selectPath(query);
it gives me the correct result (<c/>).
But if if the document comes with a namespace, for instance:
<a xmlns="mynamespace">
<b/>
</a>
the same code does not give me any result.
After investigating I found that a working code could be:
String doc = "<a xmlns="mynamespace"><b/></a>";
String query = "declare namespace xs='mynamespace';/xs:a/xs:b";
XmlObject xml = XmlObject.Factory.parse(doc);
XmlCursor cursor = xml.newCursor();
cursor.push();
cursor.selectPath(query);
Is there a better way to do this without having to set the prefix "xs:"
in the Xpath query for each level ? I would like to find a generic way
to handle XML documents with or without namespace.
Regards,
Pascal
Notice: This email message, together with any attachments, may contain information of BEA Systems, Inc., its subsidiaries and affiliated entities, that may be confidential, proprietary, copyrighted and/or legally privileged, and is intended solely for the use of the individual or entity named in this message. If you are not the intended recipient, and have received this message in error, please immediately return this by email and then delete it.