Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath filter not empty child element

Tags:

xpath

I need to filter a XPath expression to grab only a certain attribute as not empty.

I tried this:

<xsl:template match="DocumentElement/QueryResults[string(@FileName)]">

and this:

<xsl:template match="DocumentElement/QueryResults[string-length(@FileName)>0]">

but it did not work. I need the same kind of data returning from the folloing XPath expression...

<xsl:template match="DocumentElement/QueryResults">

... but filtered to avoid items with empty attribute @FileName.

Thanks!

like image 588
Marcos Buarque Avatar asked Apr 19 '10 19:04

Marcos Buarque


2 Answers

Since FileName is a child element and not an attribute, you need to access it as such and not use the attribute qualifier @ in front of the node name.

Try:

<xsl:template match="DocumentElement/QueryResults[FileName]">

This will select the DocumentElement/QueryResults elements that have a FileName child element.

If, however, you always have a FileName child element (sometimes empty) and you want to select the non empty ones, try this:

<xsl:template match="DocumentElement/QueryResults[string-length(FileName) &gt; 0]">
like image 110
Oded Avatar answered Oct 23 '22 20:10

Oded


<xsl:template match="DocumentElement/QueryResults[FileName != '']">

That's just a quick guess, and I haven't worked with XPath/XSLT in a long time. Still, if it's empty, then that should skip over it. While I prefer to use the functions like string-length, not all UAs support them (notably client-side XSLT parsers that barely work with XPath and XSLT 1.0 at all, nevermind the useful functions and functionality that XSLT 2.0 and XPath provide).

like image 27
Dustin Avatar answered Oct 23 '22 19:10

Dustin