Objectmix
Tags Register Mark Forums Read

text() does not match empty string? : XML SOAP

This is a discussion on text() does not match empty string? within the XML SOAP forums in Framework and Interface Programming category; This is a newbie question. Thanks in advance for any suggestions. I have a simple XML file like below. It is a list of books with corresponding author names. For some books, the author name is empty string. I want to search all the authors, so I called selectNodes with the following XPath expression. /library/book/author/text() This gives me a list like this: Author A Author B But it does not give me the 3rd Author which is an empty string. I want the empty string to come in the list. How do I change the XPath expression to do this. ...


Object Mix > Framework and Interface Programming > XML SOAP > text() does not match empty string?

Reply

 

LinkBack Thread Tools
  #1  
Old 07-21-2007, 01:42 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default text() does not match empty string?

This is a newbie question. Thanks in advance for any suggestions.

I have a simple XML file like below. It is a list of books
with corresponding author names. For some books,
the author name is empty string.

I want to search all the authors, so I called selectNodes
with the following XPath expression.

/library/book/author/text()

This gives me a list like this:

Author A
Author B

But it does not give me the 3rd Author which
is an empty string. I want the empty string to
come in the list. How do I change the XPath
expression to do this.

XML file:
---------
<library>
<book>
<name>Book A</name>
<author>Author A</author>
</book>
<book>
<name>Book B</name>
<author>Author B</author>
</book>
<book>
<name>Book C</name>
<author></author>
</book>
</library>
---------

Reply With Quote
  #2  
Old 07-21-2007, 02:36 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: text() does not match empty string?

* roundrobin wrote in microsoft.public.xml:
>But it does not give me the 3rd Author which
>is an empty string. I want the empty string to
>come in the list. How do I change the XPath
>expression to do this.


Evaluating your expression yields a list of nodes, not a list of
strings. You should select the author elements instead and use
higher-level program logic to get the value. It's not possible to
otherwise do what you want.
--
Björn Höhrmann · mailto:bjoern@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
Reply With Quote
  #3  
Old 07-21-2007, 03:32 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: text() does not match empty string?

Yes, I get a list of nodes and from that list I get
the text of each node which gives me those strings.
I wrote this in short.

It will be helpful if you can give me a more specific
answer. Thanks.

"Bjoern Hoehrmann" wrote:

> * roundrobin wrote in microsoft.public.xml:
> >But it does not give me the 3rd Author which
> >is an empty string. I want the empty string to
> >come in the list. How do I change the XPath
> >expression to do this.

>
> Evaluating your expression yields a list of nodes, not a list of
> strings. You should select the author elements instead and use
> higher-level program logic to get the value. It's not possible to
> otherwise do what you want.
> --
> Björn Höhrmann · mailto:bjoern@hoehrmann.de · http://bjoern.hoehrmann.de
> Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
> 68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
>

Reply With Quote
  #4  
Old 07-21-2007, 03:55 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: text() does not match empty string?

* roundrobin wrote in microsoft.public.xml:
>It will be helpful if you can give me a more specific
>answer. Thanks.
>
>Yes, I get a list of nodes and from that list I get
>the text of each node which gives me those strings.
>I wrote this in short.


for each element $e in xpath(/library/book/author) do
say xpath(string($e))
--
Björn Höhrmann · mailto:bjoern@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
Reply With Quote
  #5  
Old 07-21-2007, 06:30 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: text() does not match empty string?

roundrobin wrote:
> Yes, I get a list of nodes and from that list I get
> the text of each node which gives me those strings.
> I wrote this in short.
>
> It will be helpful if you can give me a more specific
> answer.


Which XML parser/XPath API and which programming language do you use?

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Reply With Quote
  #6  
Old 07-21-2007, 08:35 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: text() does not match empty string?

roundrobin wrote:
> This is a newbie question. Thanks in advance for any suggestions.
>
> I have a simple XML file like below. It is a list of books
> with corresponding author names. For some books,
> the author name is empty string.
>
> I want to search all the authors, so I called selectNodes
> with the following XPath expression.
>
> /library/book/author/text()
>
> This gives me a list like this:
>
> Author A
> Author B
>
> But it does not give me the 3rd Author which
> is an empty string.


That's correct. There is no text node in the third author, so there is
nothing to output :-)

> I want the empty string to
> come in the list. How do I change the XPath
> expression to do this.


As Bjoern has described, you need additional logic to do something with
author elements, like test them for content. In XSLT this might be:

<xsl:template match="author">
<xsl:choose>
<xsl:when test=".=''">
<xsl:text>No author name</xsl:text>
</xsl:when>
<xsltherwise>
<xsl:apply-templates/>
</xsltherwise>
</xsl:choose>
</xsl:template>

///Peter
--
XML FAQ: http://xml.silmaril.ie/
Reply With Quote
  #7  
Old 07-23-2007, 08:54 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default RE: text() does not match empty string?

Thank you all for replying my question.

What I understood from the replies is that it is not
a matter of simply changing the xpath expression.
Rather I have to transform the empty texts to something
else like "No author name" beforehand. Thanks Peter.

Bjoern's reply is not clear to me. Do you mean that
- first I call selectNodes with /library/book/author
- then for each node I call selectNodes with string()?

To answer Martin's question, I am using Visual C++
and MSXML 6.0.


"roundrobin" wrote:

> This is a newbie question. Thanks in advance for any suggestions.
>
> I have a simple XML file like below. It is a list of books
> with corresponding author names. For some books,
> the author name is empty string.
>
> I want to search all the authors, so I called selectNodes
> with the following XPath expression.
>
> /library/book/author/text()
>
> This gives me a list like this:
>
> Author A
> Author B
>
> But it does not give me the 3rd Author which
> is an empty string. I want the empty string to
> come in the list. How do I change the XPath
> expression to do this.
>
> XML file:
> ---------
> <library>
> <book>
> <name>Book A</name>
> <author>Author A</author>
> </book>
> <book>
> <name>Book B</name>
> <author>Author B</author>
> </book>
> <book>
> <name>Book C</name>
> <author></author>
> </book>
> </library>
> ---------
>

Reply With Quote
  #8  
Old 07-23-2007, 09:45 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: text() does not match empty string?

roundrobin wrote:
> Thank you all for replying my question.
>
> What I understood from the replies is that it is not
> a matter of simply changing the xpath expression.
> Rather I have to transform the empty texts to something
> else like "No author name" beforehand. Thanks Peter.


Nope.

> Bjoern's reply is not clear to me. Do you mean that
> - first I call selectNodes with /library/book/author
> - then for each node I call selectNodes with string()?


No, you just obtain the string content:

<xsl:for-each select="/library/book/author">
<xsl:value-of select="."/>
</xsl:for-each>

> To answer Martin's question, I am using Visual C++
> and MSXML 6.0.


Best regards, Julian
Reply With Quote
Reply

Thread Tools


Similar Threads

Thread Thread Starter Forum Replies Last Post
Exact String match usenet awk 5 10-12-2007 08:42 AM
Should an empty regex match everything usenet Perl 6 10-09-2007 06:41 AM
match string by re using some pattern usenet Perl 16 07-26-2007 04:30 PM
String functions run on empty string usenet Inetserver 1 07-11-2007 01:10 PM
Regular Exp. Match a dir string usenet Perl 1 07-31-2003 03:39 AM


All times are GMT -5. The time now is 09:36 AM.