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. ...
![]() |
| | LinkBack | Thread Tools |
|
#1
| |||
| |||
| 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> --------- |
|
#2
| |||
| |||
| * 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/ |
|
#3
| |||
| |||
| 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/ > |
|
#4
| |||
| |||
| * 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/ |
|
#5
| |||
| |||
| 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/ |
|
#6
| |||
| |||
| 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> <xsl therwise><xsl:apply-templates/> </xsl therwise></xsl:choose> </xsl:template> ///Peter -- XML FAQ: http://xml.silmaril.ie/ |
|
#7
| |||
| |||
| 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> > --------- > |
|
#8
| |||
| |||
| 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 |
![]() |
« Previous Thread
|
Next Thread »
| Thread Tools | |
| |
| ||||
| 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.




therwise>
