Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

xml - How to compare against multiple strings in xslt

For comparing an xml string value against multiple strings, I am doing the following.

<xsl:if test="/Lines/@name = 'John' or /Lines/@name = 'Steve' or /Lines/@name = 'Marc' " >

Can any one tell me, instead of using 'or' in the above case, how can I check whether a string is existing in an set of strings using xslt.

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Three ways of doing this:

  1. Use a pipe (or other appropriate character) delimited string

...

 <xsl:template match=
  "Lines[contains('|John|Steve|Mark|',
                  concat('|', @name, '|')
                 )
         ]
  ">
     <!-- Appropriate processing here -->
 </xsl:template>

.2. Test against an externally passed parameter. If the parameter is not externally set, and we are using XSLT 1.0, the xxx:node-set() extension function needs to be used to convert it to normal node-set, before accessing its children

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <!-- externally-specified parameter -->
 <xsl:param name="pNames">
  <n>John</n>
  <n>Steve</n>
  <n>Mark</n>
 </xsl:param>

 <xsl:template match="Lines">
  <xsl:if test="@name = $pNames/*">
     <!-- Appropriate processing here -->
    </xsl:if>
 </xsl:template>
</xsl:stylesheet>

.3. In XSLT 2.0 compare against a sequence of strings

 <xsl:template match="Lines[@name=('John','Steve','Mark')]">
     <!-- Appropriate processing here -->
 </xsl:template>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...