Discussion:
[XOM-interest] Retrieving the path from root of an Element
Benoît Thiébault
2012-07-11 08:29:13 UTC
Permalink
Hi everyone,

I am new to XOM and so far, I managed to use it successfully.
There is however something that I would like to do and can't find a way
to do it.

I want to retrieve, as a String, the path to root of a given Element.
For instance, consider the following XML file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>
<title lang="eng">Harry Potter</title>
<price>29.99</price>
</book>
</bookstore>

When reading the Element associated with the <title> tag, I would like
to build the String: /bookstore/book/title.
I thought the following line would work :
element.getValue();

But this only returns a bunch of spaces (it seems to be the indentation
spaces of the XML file)...
What did I do wrong?
Benoît Thiébault
2012-07-11 11:34:55 UTC
Permalink
Hi everyone,

I found a solution to my problem but it is quite ugly.
I would have prefered that function to be included in XOM... Isn't there
another way?

private String buildElementPath(final Element element) {
final StringBuilder sb = new StringBuilder("/");

sb.append(element.getQualifiedName());
Node currentParentNode = element.getParent();
Element currentParentElement = null;
if (currentParentNode instanceof Element) {
currentParentElement = (Element) currentParentNode;
}

while (currentParentElement != null) {
sb.insert(0, currentParentElement.getQualifiedName());
sb.insert(0, "/");
currentParentNode = currentParentNode.getParent();
if (currentParentNode instanceof Element) {
currentParentElement = (Element) currentParentNode;
} else {
currentParentElement = null;
}
}

return sb.toString();
}
Post by Benoît Thiébault
Hi everyone,
I am new to XOM and so far, I managed to use it successfully.
There is however something that I would like to do and can't find a way
to do it.
I want to retrieve, as a String, the path to root of a given Element.
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>
<title lang="eng">Harry Potter</title>
<price>29.99</price>
</book>
</bookstore>
When reading the Element associated with the <title> tag, I would like
to build the String: /bookstore/book/title.
element.getValue();
But this only returns a bunch of spaces (it seems to be the indentation
spaces of the XML file)...
What did I do wrong?
_______________________________________________
XOM-interest mailing list
XOM-interest at lists.ibiblio.org
http://lists.ibiblio.org/mailman/listinfo/xom-interest
Michael Kay
2012-07-11 11:51:44 UTC
Permalink
Post by Benoît Thiébault
I would have prefered that function to be included in XOM... Isn't there
another way?
Yes, there is another way. Use the XPath 2.0 expression

string-join(ancestor-or-self::*/name(), '/')

(You can run XPath 2.0 against XOM by using Saxon as your XPath engine).

Michael Kay
Saxonica
Benoît Thiébault
2012-07-11 13:02:11 UTC
Permalink
Ok, thanks.
How do you use Saxon as your XPath engine?
Post by Michael Kay
Post by Benoît Thiébault
I would have prefered that function to be included in XOM... Isn't there
another way?
Yes, there is another way. Use the XPath 2.0 expression
string-join(ancestor-or-self::*/name(), '/')
(You can run XPath 2.0 against XOM by using Saxon as your XPath engine).
Michael Kay
Saxonica
_______________________________________________
XOM-interest mailing list
XOM-interest at lists.ibiblio.org
http://lists.ibiblio.org/mailman/listinfo/xom-interest
--
Dr Beno?t Thi?bault
Project Manager

Artenum Toulouse - Science & Groupware
http://www.artenum.com

B?timent Calfocenter
10, rue Marguerite-Long
31320 Castanet-Tolosan
France
Phone: +33 (0)5 82 95 19 00
Michael Kay
2012-07-11 13:10:00 UTC
Permalink
See here:

http://www.saxonica.com/documentation/xpath-api/s9api-xpath.xml

To use a XOM source document, just change step 2, to call
DocumentBuilder.wrap(xomdoc) where xomdoc is the XOM document node.

Note that Saxon-PE and Saxon-EE support XOM "out of the box", but to use
XOM with Saxon-HE you will have to compile the Saxon/XOM interface
source code yourself.

Michael Kay
Saxonica
Post by Benoît Thiébault
Ok, thanks.
How do you use Saxon as your XPath engine?
Post by Michael Kay
Post by Benoît Thiébault
I would have prefered that function to be included in XOM... Isn't there
another way?
Yes, there is another way. Use the XPath 2.0 expression
string-join(ancestor-or-self::*/name(), '/')
(You can run XPath 2.0 against XOM by using Saxon as your XPath engine).
Michael Kay
Saxonica
_______________________________________________
XOM-interest mailing list
XOM-interest at lists.ibiblio.org
http://lists.ibiblio.org/mailman/listinfo/xom-interest
Benoît Thiébault
2012-07-11 13:10:44 UTC
Permalink
Thanks for your quick answers!
Post by Michael Kay
http://www.saxonica.com/documentation/xpath-api/s9api-xpath.xml
To use a XOM source document, just change step 2, to call
DocumentBuilder.wrap(xomdoc) where xomdoc is the XOM document node.
Note that Saxon-PE and Saxon-EE support XOM "out of the box", but to use
XOM with Saxon-HE you will have to compile the Saxon/XOM interface
source code yourself.
Michael Kay
Saxonica
Post by Benoît Thiébault
Ok, thanks.
How do you use Saxon as your XPath engine?
Post by Michael Kay
Post by Benoît Thiébault
I would have prefered that function to be included in XOM... Isn't there
another way?
Yes, there is another way. Use the XPath 2.0 expression
string-join(ancestor-or-self::*/name(), '/')
(You can run XPath 2.0 against XOM by using Saxon as your XPath engine).
Michael Kay
Saxonica
_______________________________________________
XOM-interest mailing list
XOM-interest at lists.ibiblio.org
http://lists.ibiblio.org/mailman/listinfo/xom-interest
_______________________________________________
XOM-interest mailing list
XOM-interest at lists.ibiblio.org
http://lists.ibiblio.org/mailman/listinfo/xom-interest
--
Dr Beno?t Thi?bault
Project Manager

Artenum Toulouse - Science & Groupware
http://www.artenum.com

B?timent Calfocenter
10, rue Marguerite-Long
31320 Castanet-Tolosan
France
Phone: +33 (0)5 82 95 19 00
Continue reading on narkive:
Loading...