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!
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) > 0]">
<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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With