XSLT: contains () for multiple lines

I have a variable in XSLT under the name variable_namethat I am trying to set to 1if the Product in question has attributes named A or B or both A and B.

<xsl:variable name="variable_name">
  <xsl:for-each select="product/attributes">
    <xsl:if test="@attributename='A' or @attributename='B'">
      <xsl:value-of select="1"/>
    </xsl:if>
  </xsl:for-each>
</xsl:variable>

Is there a way to match multiple lines using an if statement, since mine just matches if A is present or B is present. If both A and B are present, it does not set the variable to 1. Any help on this will be appreciated as I new to XSLT.

+5
source share
2 answers

You can use the xsl: select statement, something like switching in common programming languages:

Example:

<xsl:variable name="variable_name">
  <xsl:for-each select="product/attributes">
  <xsl:choose>
    <xsl:when test="@attributename='A'">
      1
    </xsl:when>
    <xsl:when test=" @attributename='B'">
      1
    </xsl:when>
    <!--... add other options here-->
    <xsl:otherwise>1</xsl:otherwise>
  </xsl:choose>
  </xsl:for-each>
</xsl:variable> 

variable_name product/attributes.

... http://www.w3schools.comwww.w3schools.com/xsl/el_choose.asp

EDIT: ( ) OP:

<xsl:variable name="variable_name">
  <xsl:for-each select="product/attributes">
    <xsl:if test="contains(text(), 'A') or contains(text(), 'B')">
       1
    </xsl:if>
  </xsl:for-each>
</xsl:variable> 

, xml, xslt.

+13

...

"" XML (, <element x="1" x="2" />)?

, ? XML xmllint - , , .

xmllint --valid the-xml-file.xml

, .

0

All Articles