sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
sshahin@gmail.com's Avatar

XPath Convert Node to String


Question posted by: sshahin@gmail.com (Guest) on October 24th, 2005 05:35 PM
Is there a way to Convert a Node to a String using XPath 1.0? For
example, consider the following xml doc:

<?xml version="1.0" encoding="UTF-8"?>
<FaultTo xmlns="http://blah.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Address>http://www.altova.com</Address>

<ReferenceProperties>

<fabrikam:CustomerKey1 xmlns:fabrikam="http://blah.com">3333
<fabrikam:type>TYPE4</fabrikam:type>
</fabrikam:CustomerKey1>

<fabrikam:CustomerKey2
xmlns:fabrikam="http://blah.com">4444</fabrikam:CustomerKey2>

<fabrikam:CustomerKey3
xmlns:fabrikam="http://blah.com">6666</fabrikam:CustomerKey3>

</ReferenceProperties>

<ServiceName PortName="NCName">q:name</ServiceName>
</FaultTo>

I want to be able to extract and return everything contained within
<ReferenceProperties> as a string using xpath 1.0 (I do not wish to use
CDATA or any workarounds like this; basically the XML should be left
untouched with no modifications).

3 Answers Posted
Martin Honnen's Avatar
Guest - n/a Posts
#2: Re: XPath Convert Node to String



Join Bytes! wrote:
[color=blue]
> Is there a way to Convert a Node to a String using XPath 1.0?[/color]

Yes, you can certainly do
string(nodeset[1])
but that gives you what XPath 1.0 defines as the string value of that
node (see <http://www.w3.org/TR/xpath#function-string> and
<http://www.w3.org/TR/xpath#element-nodes>) while you seem to want to
serialize the contents to get back the XML markup of the contents.
There is no function in XPath 1.0 to serialize nodes.



--

Martin Honnen
http://JavaScript.FAQTs.com/
sshahin@gmail.com's Avatar
sshahin@gmail.com October 24th, 2005 07:05 PM
Guest - n/a Posts
#3: Re: XPath Convert Node to String

This is not what I am looking for. I am looking to return the
following:

<fabrikam:CustomerKey1 xmlns:fabrikam="http://blah.com">3333
<fabrikam:type>TYPE4</fabrikam:type>
</fabrikam:CustomerKey1>

<fabrikam:CustomerKey2
xmlns:fabrikam="http://blah.com">4444</fabrikam:CustomerKey2>
<fabrikam:CustomerKey3
xmlns:fabrikam="http://blah.com">6666</fabrikam:CustomerKey3>

I seem to agree with you that Xpath may not have a built-in function
for extracting an XML fragment as a string. If there is anyone else
out there who can provide input, I would greatly appreciat it.

Dimitre Novatchev's Avatar
Dimitre Novatchev October 25th, 2005 11:05 AM
Guest - n/a Posts
#4: Re: XPath Convert Node to String


<sshahin@gmail.com> wrote in message
news:1130176270.430699.205680@g47g2000cwa.googlegr oups.com...[color=blue]
> This is not what I am looking for. I am looking to return the
> following:
>
> <fabrikam:CustomerKey1 xmlns:fabrikam="http://blah.com">3333
> <fabrikam:type>TYPE4</fabrikam:type>
> </fabrikam:CustomerKey1>
>
> <fabrikam:CustomerKey2
> xmlns:fabrikam="http://blah.com">4444</fabrikam:CustomerKey2>
> <fabrikam:CustomerKey3
> xmlns:fabrikam="http://blah.com">6666</fabrikam:CustomerKey3>
>
> I seem to agree with you that Xpath may not have a built-in function
> for extracting an XML fragment as a string. If there is anyone else
> out there who can provide input, I would greatly appreciat it.[/color]


This is trivial using XSLT.

Read about the
xsl:copy-of

xslt instruction.



Cheers,
Dimitre Novatchev


 
Not the answer you were looking for? Post your question . . .
196,795 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 196,795 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors