XPath filter is not an empty child

I need to filter out an XPath expression to capture only a specific 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 didn’t work. I need the same data returned from the following XPath expression ...

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

... but filtered to avoid elements with an empty @FileName attribute.

Thanks!

+6
xpath
source share
2 answers

Since FileName is a child, not an attribute, you need to access it as such, and not use the @ attribute attribute before the node name.

Try:

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

This will result in the selection of DocumentElement/QueryResults elements that have a child FileName element.

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

 <xsl:template match="DocumentElement/QueryResults[string-length(FileName) &gt; 0]"> 
+8
source share
 <xsl:template match="DocumentElement/QueryResults[FileName != '']"> 

This is just a hunch, and I have not worked with XPath / XSLT for a long time. However, if it is empty, then this should skip it. Although I prefer to use functions such as string-length , not all UAs support them (especially XSLT client parsers, which almost do not work with XPath and XSLT 1.0 at all, do not have any useful functions and functionality provided by XSLT 2.0 and XPath )

+3
source share

All Articles