简体   繁体   English

如何在应用模板中将字符串与 XSLT1.0 中的数组进行比较

[英]How to compare string with array in XSLT1.0 in apply templates

Suppose, if I have "Test1|Test2|Test3" as a string and I want compare it with following:假设,如果我有"Test1|Test2|Test3"作为字符串,我想将它与以下内容进行比较:

<items>
<item>Test1</item>
<item>Test2</item>
<item>Test3</item>
</items>

Is it possible to check in apply template is it true of false?是否可以签入应用模板是否为真?

Thanks谢谢

Something like this:像这样的东西:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    
  <xsl:output method="xml" indent="yes"/>
    
  <xsl:variable name="arrayString" select="'Test1|Test2|Test3'"/>

  <xsl:template match="items">
    <xsl:copy>
      <xsl:apply-templates select="item[contains($arrayString,.)]"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="item">
    <xsl:copy>
      <xsl:value-of select="concat('Template_item : ', .)"/>
    </xsl:copy>
  </xsl:template>
  
</xsl:stylesheet>

See it working here: https://xsltfiddle.liberty-development.net/3Mvnt3H看到它在这里工作: https://xsltfiddle.liberty-development.net/3Mvnt3H

I would suggest you take precaution and use:我建议您采取预防措施并使用:

<xsl:apply-templates select="item[contains(concat('|', $yourString, '|'), concat('|', ., '|'))]"/>

Otherwise you may get false positives - for example, if your string is:否则,您可能会得到误报 - 例如,如果您的字符串是:

Test1|Test25|Test301

a simple contains() test will also pass all of these:一个简单的contains()测试也将通过所有这些:

<item>Test2</item>
<item>Test3</item>
<item>Test30</item>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM