Evgeni Dimitrov
Evgeni Dimitrov

Reputation: 22506

XPath working with empty nodes

If I got this XML(first tag is empty)

<messages>
    <message>                                                                                             
            <name>Kiro</name>                                                                       
            <acc></acc>                                                                     
            <date>20120506</date>                                                                       
            <template>TMPL_ACC_UP</template>                                                                    
        </message>                                                              
        <message>                                                                       
            <name>Third</name>                                                                  
            <acc>555</acc>                                                                  
            <date>20120507</date>                                                                       
            <template>TMPL_ACC_UP</template>                                                                
        </message>                                                              
</messages>

Now i take all accpunts with this XPath expression: "//message/acc/text()", and get node list of 1(empty nodes not included). I want to get empty string for the empty tag ['555','']. How I can achieve this? Thanks!

Upvotes: 0

Views: 905

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243529

In XPath 2.0 a one-liner like this can be used:

/*/message/acc/string()

However, this cannot be produced as the result of evaluating a single XPath 1.0 expression.

If you must use XPath 1.0, the solution is to select all /*/message/acc elements and then in the PL that is hosting XPath, output the string value of each of the selected elements.

For example, this XSLT 1.0 transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/">
  <xsl:text>[</xsl:text><xsl:apply-templates/><xsl:text>]</xsl:text>
 </xsl:template>
 <xsl:template match="acc">
    <xsl:if test="preceding::acc">, </xsl:if>
   <xsl:text>'</xsl:text><xsl:value-of select="."/><xsl:text>'</xsl:text>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

when applied on the provided XML document:

<messages>
    <message>
            <name>Kiro</name>
            <acc></acc>
            <date>20120506</date>
            <template>TMPL_ACC_UP</template>
        </message>
        <message>
            <name>Third</name>
            <acc>555</acc>
            <date>20120507</date>
            <template>TMPL_ACC_UP</template>
        </message>
</messages>

produces the wanted, correct result:

['', '555']

Upvotes: 2

DRCB
DRCB

Reputation: 2121

I would loop over nodes and extract their text as value

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>[
      <xsl:for-each select="//message/acc">
       '<xsl:value-of select="text()"/>',
      </xsl:for-each>]
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

Result: [ '', '555', ]

Upvotes: 1

Related Questions