I want to check if in my XML exists node that has type attribute containing string type_attachment_.

Is it a correct way to check it?

<xsl:if test="count(*[contains(@Type, 'type_attachment_')]) &gt; 0">
   something
</xsl:if>

I don't know how nested can this node be. It can be for example as simple as that:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl"?>

<hello-world>
 <greeter>
  <dsdsds>An XSLT Programmer
   <greeting type = 'type_attachment_'>Hello, World!
   </greeting>
  </dsdsds>
 </greeter>
</hello-world>

but can also contain this node nested in different other elements.

有帮助吗?

解决方案

Expressions that match existing nodes are truthy. Expressions that do not match any nodes are falsy.

Therefore, you don't need to count the set of nodes returned. Simply test to see if anything matches.

<xsl:if test="*[contains(@Type, 'type_attachment')]">
   something
</xsl:if>

其他提示

Find out an example:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:param name="filt">
    <filters>
      <ritem type="type_attachment_" relateditemnumber="8901037"/>
      <ritem relateditemnumber="8901038"/>
      <ritem type="type_attachment_" relateditemnumber="8901039"/>
      <ritem relateditemnumber="8901040"/>
    </filters>
  </xsl:param>

  <xsl:template match="/">
    <xsl:for-each select="$filt/filters/ritem[@type='type_attachment_']">
      <xsl:copy-of select="."/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

OUTPUT:

<ritem type="type_attachment_" relateditemnumber="8901037"/>
<ritem type="type_attachment_" relateditemnumber="8901039"/>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top