如何使用XSLT在数组中动态存储值并对值进行比较?

如何使用XSLT在数组中动态存储值并对值进行比较?,xslt,Xslt,下面是一个很好的例子,但我如何动态存储值……请您解释一下 <xsl:variable name="countries" select="'EG, KSA, UAE, AG'" /> <xsl:variable name="country" select="'KSA'" /> <xsl:choose> <xsl:when test=" contains( concat(', ', normaliz

下面是一个很好的例子,但我如何动态存储值……请您解释一下

 <xsl:variable name="countries" select="'EG, KSA, UAE, AG'" />
   <xsl:variable name="country"   select="'KSA'" />
     <xsl:choose>
      <xsl:when test="
      contains(
       concat(', ', normalize-space($countries), ', ')
        concat(', ', $country, ', ')
       )
     ">
 <xsl:text>IN</xsl:text>
 </xsl:when>
 <xsl:otherwise>
 <xsl:text>OUT</xsl:text>
</xsl:otherwise>

在里面
出来

看起来不错……我还有其他要求。你能调查一下这个吗

 <xml>
   <test>
    <BookID>
      0061AB
    </BookID>
    <amount>
      16
    </amount>
   </test>
   <test>
    <BookID>
      0062CD
    </BookID>
    <amount>
      2
    </amount>
   </test>
   <test>
    <BookID>
      0061AB
    </BookID>
    <amount>
      2
    </amount>
   </test>
 </xml>

0061AB
16
0062CD
2.
0061AB
2.

这里根据BookID的相等值,我想加上amount值……比如上面的例子,如果BookID的值是0061AB,那么amount的值应该是18。

正如其他人提到的,你的问题不是很清楚,但是,您可以使用调用模板在包含候选国和搜索列表的xml文档中重复使用“国家/地区查找”算法(您也可以使用
document
将候选国和搜索目标加载到单独的xml文档中)

e、 g.应用XSLT时:

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

  <xsl:template match="/xml">
    <xsl:apply-templates select="test"/>
  </xsl:template>

  <xsl:template match="test">
    <xsl:call-template name="FindCountry">
      <xsl:with-param name="countries" select="normalize-space(countries/text())"/>
      <xsl:with-param name="country" select="normalize-space(country/text())"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="FindCountry" xml:space="default">
    <xsl:param name="countries" />
    <xsl:param name="country" />
    Is <xsl:value-of select="$country" /> in <xsl:value-of select="$countries" /> ?
    <xsl:choose>
      <xsl:when test="contains(concat(', ', $countries, ', '),
                               concat(', ', $country, ', '))">
        <xsl:text>IN</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>OUT</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

不清楚问题是什么?请编辑并给我们一个你想要实现的例子。你对低级机制的考虑太多了。如果您可以向我们解释您希望转换做什么(而不是要使用什么机制来编写转换),那么您将更接近XSLT编写转换的方式。看起来不错……我再次向您提供这样的需求。0061AB 16在这里根据的相等值,我想加上这个值……比如上面的例子,如果的值是0061AB,那么的值应该是18。@Dipta我不理解这个新要求?是否要将常量“2”添加到现有值?这是与您原来的问题有关,还是新问题?我已再次添加了要求…请检查
<xml>
  <test>
    <countries>
      EG, KSA, UAE, AG
    </countries>
    <country>
      UAE
    </country>
  </test>
  <test>
    <countries>
      GBR, USA, DE, JP
    </countries>
    <country>
      AUS
    </country>
  </test>
</xml>
Is UAE in EG, KSA, UAE, AG ? IN 
Is AUS in GBR, USA, DE, JP ? OUT