mrrsb
mrrsb

Reputation: 681

XSLT not contains multiple String

In XSLT, is it possible to do condition check like:

<xsl:if test='node != contains("Apple, Banana, Carrot")'>..</xsl>

So, if the node have the following string it will not view in the list. the contains(..) string dynamically created.

For instance..

XML

<?xml version="1.0"?>
<list>
    <node>Apple</node>
    <node>Banana</node>
    <node>Carrot</node>
    <node>Dog</node>
    <node>Elephant</node>
    <node>Fox</node>
    <node>Golf</node>
</list>

the contains("<% script %>") - sorry the code will not work here. But it will just add a string separated by comma.

Upvotes: 1

Views: 3068

Answers (1)

zx485
zx485

Reputation: 29052

Use three predicates:

<xsl:if test="contains(text(),'Apple')) or contains(text(),'Banana')) or contains(text(),'Carrot'))">..</xsl>

Referring to your updated question:

Naming your keyword file keyword.xml:

<?xml version="1.0"?>
<list>
    <node>Apple</node>
    <node>Banana</node>
    <node>Carrot</node>
    <node>Dog</node>
    <node>Elephant</node>
    <node>Fox</node>
    <node>Golf</node>
</list>

and an arbitrary data test file data.xml

<?xml version="1.0" encoding="UTF-8"?>
<Data>
    <a>Apple</a>
    <b>Carrot</b>
    <c>Crap</c>
</Data>

you can get the desired nodes using the following XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="inc" select="document('keyword.xml')/list" />

  <xsl:template match="/Data">
    <xsl:apply-templates select="node()|@*" />
  </xsl:template>

  <xsl:template match="*">
    <xsl:value-of select="name()" />      <!-- only for debugging/output purposes -->
    <xsl:variable name="cur" select="text()" />
    <xsl:if test="$inc/node/text()[contains(., $cur)]">..</xsl:if>
  </xsl:template>

</xsl:stylesheet>

The essential expression is

$inc/node/text()[contains(., $cur)]

which checks if the current text() node saved by the <xsl:variable...> expression is contained in the keyword.xml file under list/node/text().

So in this test case the output is:

<?xml version="1.0"?>

    a..
    b..
    c

Upvotes: 3

Related Questions