Frage

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.

War es hilfreich?

Lösung

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>

Andere Tipps

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"/>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top