-2

Below is a good example, but how can I store values dynamically....Could you please explain?

 <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>

Look good....I have some another requirement. Could you please look into this?

 <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>

here According to the equal value of BookID, I want to add the amount value.....like for above example, if value of BookID is 0061AB, then the value of amount should be 18.

Dipta
  • 13
  • 6
  • 2
    Not clear what is the question? Please, edit and give us an example of what you want to achieve. – Dimitre Novatchev Oct 04 '12 at 15:55
  • 2
    You are thinking too much in terms of low-level mechanisms. If you can explain to us what you want the transformation to do (and not what mechanisms you want to use to write it), then you will be much closer to the XSLT way of writing it. – Michael Kay Oct 04 '12 at 17:07

1 Answers1

0

As others have mentioned, your question isn't very clear, but you can use a call template to reuse your 'country find' algorithm across an xml document containing both the candidate country and the search list (you could also use document to load split the candidate and search target into separate xml documents)

e.g. when applying the 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>

To the XML

<xml>
  <test>
    <countries>
      EG, KSA, UAE, AG
    </countries>
    <country>
      UAE
    </country>
  </test>
  <test>
    <countries>
      GBR, USA, DE, JP
    </countries>
    <country>
      AUS
    </country>
  </test>
</xml>

Result

Is UAE in EG, KSA, UAE, AG ? IN 
Is AUS in GBR, USA, DE, JP ? OUT
StuartLC
  • 104,537
  • 17
  • 209
  • 285
  • looks good...I am again providing you the requirement like this. 0061AB 16 here According to the equal value of , I want to add the value.....like for above example, if value of is 0061AB, then the value of should be 18. – Dipta Oct 05 '12 at 06:17
  • @Dipta I don't understand this new requirement? Do you want to add the constant '2' to an existing value? Is this related to your original question, or is this a new question? – StuartLC Oct 05 '12 at 06:26
  • I have added the requirement again...please check – Dipta Oct 05 '12 at 06:38