You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xalan.apache.org by Ori Doolman <Or...@SigValue.com> on 2007/03/08 08:43:45 UTC

Extracting a sub-tree as text using xpath

Dear friends,

I'm using Xalan 1.1 for extracting text from my xml using xpath
expression.

I would like to know how I can extract a whole sub-tree as text.

Until now, I had documents that look something like this:

 

<A>

            <B> my_data </B>

</A>

 

And I used the expression "/A/B/text()" in order to extract the text
"my_data".

 

Now, I need to extract several nodes (not text nodes) as text. For
example:

 

<A>

            <B> 

                        <C> my_data</C>

                        <D> my_other_data </D>

</B>

</A>

 

I would like an expression that extracts all the <B> sub-tree as text:

"<B><C>my_data</C><D> my_other_data </D></B>"

 

I think it has something to do with the "string()" function, but I'm not
sure how to use it.

 

 

Thank you,

Ori Doolman,


RE: Extracting a sub-tree as text using xpath

Posted by Ori Doolman <Or...@SigValue.com>.
Dave,

I looked again at the example and now I understand what you mean.

For some reason I didn't see it before.

Thanks for the help,

Ori.

 


 

Ori Doolman wrote:
> Thank you, Dave.
> Yes, I probably mean markup (which is a text in the format of XML
> including all '<' and '>' tags. Not the text contained only within the
> text nodes).
>
> Is there any complete ready made function that serializes the NodeSet?
> It seems like a very common problem. The example doesn't refer to all
> node types and doesn't use escaping characters.

I don't understand what you mean by "refer to all node types and doesn't
use escaping characters."  The sample will certainly serialize any node
type other than the root node or an attribute node, and it will escape
characters if that's necessary to generate well-formed markup.

> Maybe another option is selecting a single node (only the root of the
> sub-tree) and convert the sub-tree to a Xerces DOM tree. Is this
> possible?

It's possible, but a lot more work than just following what the sample
does.

Dave


Re: Extracting a sub-tree as text using xpath

Posted by David Bertoni <db...@apache.org>.
Ori Doolman wrote:
> Thank you, Dave.
> Yes, I probably mean markup (which is a text in the format of XML
> including all '<' and '>' tags. Not the text contained only within the
> text nodes).
> 
> Is there any complete ready made function that serializes the NodeSet?
> It seems like a very common problem. The example doesn't refer to all
> node types and doesn't use escaping characters.

I don't understand what you mean by "refer to all node types and doesn't 
use escaping characters."  The sample will certainly serialize any node 
type other than the root node or an attribute node, and it will escape 
characters if that's necessary to generate well-formed markup.

> Maybe another option is selecting a single node (only the root of the
> sub-tree) and convert the sub-tree to a Xerces DOM tree. Is this
> possible?

It's possible, but a lot more work than just following what the sample does.

Dave

RE: Extracting a sub-tree as text using xpath

Posted by Ori Doolman <Or...@SigValue.com>.
Thank you, Dave.
Yes, I probably mean markup (which is a text in the format of XML
including all '<' and '>' tags. Not the text contained only within the
text nodes).

Is there any complete ready made function that serializes the NodeSet?
It seems like a very common problem. The example doesn't refer to all
node types and doesn't use escaping characters.
Maybe another option is selecting a single node (only the root of the
sub-tree) and convert the sub-tree to a Xerces DOM tree. Is this
possible?
If I have a Xerces DOM tree, I know how to serialize it easily.

If none of the options above is available, I guess I would have to write
the complete SerializeNodeSet by myself.

Ori.


-----Original Message-----
From: David Bertoni [mailto:dbertoni@apache.org] 
Sent: Wednesday, March 14, 2007 5:01 PM
To: xalan-c-users@xml.apache.org
Subject: Re: Extracting a sub-tree as text using xpath

Ori Doolman wrote:
> Dave,
> What I need is to have in my hand (or code) a string that contains a
> sub-tree of the document (as XML).
> Of course I could just parse the document using Xerces and go to the
> node I need and serialize this sub-tree to text.
> But I want to do this using Xalan because there are other XPath
features
> I need.
> 
> I think that what you are suggesting is selecting all sub-tree nodes
> with /A/B//* and then go over the returned NodeRefList and create the
> XML text according to the node type (is there a ready made function
API
> for this common procedure??).

Yes, which is why I referred you to the SerializeNodeSet sample, which
does 
exactly that.  Did you look at it?  Also, when you say "XML text," it's 
ambiguous.  I think you mean markup, but I'm not sure.

> 
> So I'm beginning to understand that xpath can only be used for
selecting
> a collection of nodes from a document. The result of selected nodes
> doesn't refer to their original hierarchy in the DOM document tree. 

I don't know what to make of this comment, since it seems to contradict
itself.

An XPath location path addresses nodes in an instance of the data model.
A 
location path does not create anything, so the selected nodes _are_ the 
nodes in the original source tree.  However, XPath does not operate on 
markup, so there is no way to go back to the original markup.  The best
you 
can do is create new markup for any selected nodes.

Dave

> 
> Am I right here?
> 
> 
> 
> -----Original Message-----
> From: David Bertoni [mailto:dbertoni@apache.org] 
> Sent: Tuesday, March 13, 2007 12:10 AM
> To: xalan-c-users@xml.apache.org
> Subject: Re: Extracting a sub-tree as text using xpath
> 
> Ori Doolman wrote:
>> Someone, please?
>>
> I think what you mean is that you want the results as markup, and not
as
> 
> text, in the sense of text nodes in the data model.
> 
> Take a look at the SerializeNodeSet sample for more information.
> 
> Dave
> 


Re: Extracting a sub-tree as text using xpath

Posted by David Bertoni <db...@apache.org>.
Ori Doolman wrote:
> Dave,
> What I need is to have in my hand (or code) a string that contains a
> sub-tree of the document (as XML).
> Of course I could just parse the document using Xerces and go to the
> node I need and serialize this sub-tree to text.
> But I want to do this using Xalan because there are other XPath features
> I need.
> 
> I think that what you are suggesting is selecting all sub-tree nodes
> with /A/B//* and then go over the returned NodeRefList and create the
> XML text according to the node type (is there a ready made function API
> for this common procedure??).

Yes, which is why I referred you to the SerializeNodeSet sample, which does 
exactly that.  Did you look at it?  Also, when you say "XML text," it's 
ambiguous.  I think you mean markup, but I'm not sure.

> 
> So I'm beginning to understand that xpath can only be used for selecting
> a collection of nodes from a document. The result of selected nodes
> doesn't refer to their original hierarchy in the DOM document tree. 

I don't know what to make of this comment, since it seems to contradict itself.

An XPath location path addresses nodes in an instance of the data model.  A 
location path does not create anything, so the selected nodes _are_ the 
nodes in the original source tree.  However, XPath does not operate on 
markup, so there is no way to go back to the original markup.  The best you 
can do is create new markup for any selected nodes.

Dave

> 
> Am I right here?
> 
> 
> 
> -----Original Message-----
> From: David Bertoni [mailto:dbertoni@apache.org] 
> Sent: Tuesday, March 13, 2007 12:10 AM
> To: xalan-c-users@xml.apache.org
> Subject: Re: Extracting a sub-tree as text using xpath
> 
> Ori Doolman wrote:
>> Someone, please?
>>
> I think what you mean is that you want the results as markup, and not as
> 
> text, in the sense of text nodes in the data model.
> 
> Take a look at the SerializeNodeSet sample for more information.
> 
> Dave
> 


RE: Extracting a sub-tree as text using xpath

Posted by Ori Doolman <Or...@SigValue.com>.
Dave,
What I need is to have in my hand (or code) a string that contains a
sub-tree of the document (as XML).
Of course I could just parse the document using Xerces and go to the
node I need and serialize this sub-tree to text.
But I want to do this using Xalan because there are other XPath features
I need.

I think that what you are suggesting is selecting all sub-tree nodes
with /A/B//* and then go over the returned NodeRefList and create the
XML text according to the node type (is there a ready made function API
for this common procedure??).

So I'm beginning to understand that xpath can only be used for selecting
a collection of nodes from a document. The result of selected nodes
doesn't refer to their original hierarchy in the DOM document tree. 

Am I right here?



-----Original Message-----
From: David Bertoni [mailto:dbertoni@apache.org] 
Sent: Tuesday, March 13, 2007 12:10 AM
To: xalan-c-users@xml.apache.org
Subject: Re: Extracting a sub-tree as text using xpath

Ori Doolman wrote:
> Someone, please?
> 
I think what you mean is that you want the results as markup, and not as

text, in the sense of text nodes in the data model.

Take a look at the SerializeNodeSet sample for more information.

Dave

Re: Extracting a sub-tree as text using xpath

Posted by David Bertoni <db...@apache.org>.
Ori Doolman wrote:
> Someone, please?
> 
I think what you mean is that you want the results as markup, and not as 
text, in the sense of text nodes in the data model.

Take a look at the SerializeNodeSet sample for more information.

Dave

RE: Extracting a sub-tree as text using xpath

Posted by Ori Doolman <Or...@SigValue.com>.
Yes, I tried it.
/A/text() returns nothing, because it returns the text node under A which is empty.


-----Original Message-----
From: Wyles Eric - ewyles [mailto:Eric.Wyles@acxiom.com] 
Sent: Monday, March 12, 2007 11:59 PM
To: xalan-c-users@xml.apache.org
Subject: RE: Extracting a sub-tree as text using xpath

Have you tried /A/text()?

> -----Original Message-----
> From: Ori Doolman [mailto:OriD@SigValue.com]
> Sent: Monday, March 12, 2007 4:54 PM
> To: xalan-c-users@xml.apache.org
> Subject: RE: Extracting a sub-tree as text using xpath
> 
> Someone, please?
> 
> 
> 
> -----הודעה מקורית-----
> מאת: Ori Doolman
> נשלח: ה 08/03/2007 09:43
> אל: xalan-c-users@xml.apache.org
> נושא: Extracting a sub-tree as text using xpath
> 
> Dear friends,
> 
> I'm using Xalan 1.1 for extracting text from my xml using xpath
> expression.
> 
> I would like to know how I can extract a whole sub-tree as text.
> 
> Until now, I had documents that look something like this:
> 
> 
> 
> <A>
> 
>             <B> my_data </B>
> 
> </A>
> 
> 
> 
> And I used the expression "/A/B/text()" in order to extract the text
> "my_data".
> 
> 
> 
> Now, I need to extract several nodes (not text nodes) as text. For
> example:
> 
> 
> 
> <A>
> 
>             <B>
> 
>                         <C> my_data</C>
> 
>                         <D> my_other_data </D>
> 
> </B>
> 
> </A>
> 
> 
> 
> I would like an expression that extracts all the <B> sub-tree as text:
> 
> "<B><C>my_data</C><D> my_other_data </D></B>"
> 
> 
> 
> I think it has something to do with the "string()" function, but I'm not
> sure how to use it.
> 
> 
> 
> 
> 
> Thank you,
> 
> Ori Doolman,
> 
***************************************************************************
The information contained in this communication is confidential, is
intended only for the use of the recipient named above, and may be legally
privileged.

If the reader of this message is not the intended recipient, you are
hereby notified that any dissemination, distribution or copying of this
communication is strictly prohibited.

If you have received this communication in error, please resend this
communication to the sender and delete the original message or any copy
of it from your computer system.

Thank You.
****************************************************************************

RE: Extracting a sub-tree as text using xpath

Posted by Wyles Eric - ewyles <Er...@acxiom.com>.
Have you tried /A/text()?

> -----Original Message-----
> From: Ori Doolman [mailto:OriD@SigValue.com]
> Sent: Monday, March 12, 2007 4:54 PM
> To: xalan-c-users@xml.apache.org
> Subject: RE: Extracting a sub-tree as text using xpath
> 
> Someone, please?
> 
> 
> 
> -----הודעה מקורית-----
> מאת: Ori Doolman
> נשלח: ה 08/03/2007 09:43
> אל: xalan-c-users@xml.apache.org
> נושא: Extracting a sub-tree as text using xpath
> 
> Dear friends,
> 
> I'm using Xalan 1.1 for extracting text from my xml using xpath
> expression.
> 
> I would like to know how I can extract a whole sub-tree as text.
> 
> Until now, I had documents that look something like this:
> 
> 
> 
> <A>
> 
>             <B> my_data </B>
> 
> </A>
> 
> 
> 
> And I used the expression "/A/B/text()" in order to extract the text
> "my_data".
> 
> 
> 
> Now, I need to extract several nodes (not text nodes) as text. For
> example:
> 
> 
> 
> <A>
> 
>             <B>
> 
>                         <C> my_data</C>
> 
>                         <D> my_other_data </D>
> 
> </B>
> 
> </A>
> 
> 
> 
> I would like an expression that extracts all the <B> sub-tree as text:
> 
> "<B><C>my_data</C><D> my_other_data </D></B>"
> 
> 
> 
> I think it has something to do with the "string()" function, but I'm not
> sure how to use it.
> 
> 
> 
> 
> 
> Thank you,
> 
> Ori Doolman,
> 
***************************************************************************
The information contained in this communication is confidential, is
intended only for the use of the recipient named above, and may be legally
privileged.

If the reader of this message is not the intended recipient, you are
hereby notified that any dissemination, distribution or copying of this
communication is strictly prohibited.

If you have received this communication in error, please resend this
communication to the sender and delete the original message or any copy
of it from your computer system.

Thank You.
****************************************************************************

RE: Extracting a sub-tree as text using xpath

Posted by Ori Doolman <Or...@SigValue.com>.
Someone, please?



-----הודעה מקורית-----
מאת: Ori Doolman
נשלח: ה 08/03/2007 09:43
אל: xalan-c-users@xml.apache.org
נושא: Extracting a sub-tree as text using xpath
 
Dear friends,

I'm using Xalan 1.1 for extracting text from my xml using xpath
expression.

I would like to know how I can extract a whole sub-tree as text.

Until now, I had documents that look something like this:

 

<A>

            <B> my_data </B>

</A>

 

And I used the expression "/A/B/text()" in order to extract the text
"my_data".

 

Now, I need to extract several nodes (not text nodes) as text. For
example:

 

<A>

            <B> 

                        <C> my_data</C>

                        <D> my_other_data </D>

</B>

</A>

 

I would like an expression that extracts all the <B> sub-tree as text:

"<B><C>my_data</C><D> my_other_data </D></B>"

 

I think it has something to do with the "string()" function, but I'm not
sure how to use it.

 

 

Thank you,

Ori Doolman,