1

I have dynamic generated XML as below which populate node values in run-time.

<?xml version="1.0" encoding="utf-8" ?>
<master>
  <child>
    <category1>Category1_A</category1>
    <category2>Category2_B </category2>
  </child>
</master>

I have a category code configuration key in my web.config as below

<add  key="Code"  value="A1|A2" />

Below is my XSLT & i know this is not well formatted.

And I have following issues in this XSLT

  1. How I can pass config key in below function since it’s not in XML.
  2. If this method return false then I wants to return String message from XSLT attribute like “Sorry, Combination doesn't match.”
  3. I know it’s pretty bit confusing but I know this is much interesting.

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxsl="urn:schemas-microsoft-com:xslt"
        xmlns:user="urn:my-scripts">
    
      <msxsl:script language="C#" implements-prefix="user">
        <![CDATA[
         public string checkCategory (string category1,string category2)
         {
        if( (category1=="Category1_A" && category1==" Category2_B") && ConfigurationManager.AppSetting["Code"].contains("A1"))
           return true;
           else
          return false;
         }
          ]]>
      </msxsl:script>
      <xsl:template match="master">
        <child>
          <circumference>
            <xsl:value-of select="user: checkCategory (category1,category2)"/>
            <!--if method return false then : return Sorry, Combination doesn’t match.”-->
          </circumference>
        </child>
      </xsl:template>
    </xsl:stylesheet>
    
2
  • From what you've posted, it looks like you want to compare the XML content to a config string, and nothing else. Why are you doing that with XSLT as opposed to any of the other XML-handling technologies? Commented Mar 6, 2013 at 21:17
  • @AnnL.: it's previous implemented logic in a project & i am doing some changes on this. Commented Mar 7, 2013 at 5:24

1 Answer 1

1

If your function is returning true and false, then you should change the return type to bool:

<msxsl:script language="C#" implements-prefix="user">
    <![CDATA[
     public bool checkCategory (string category1,string category2)
     {
       if( (category1=="Category1_A" && category2==" Category2_B") && ConfigurationManager.AppSetting["Code"].contains("A1"))
         return true;
       else
         return false;
     }
   ]]>
</msxsl:script>

and you can simplify the code a bit:

<msxsl:script language="C#" implements-prefix="user">
   <![CDATA[
     public bool checkCategory (string category1,string category2)
     {
        return (category1 == "Category1_A" && category2 == "Category2_B") &&
                 ConfigurationManager.AppSetting["Code"].contains("A1");
     }
   ]]>
</msxsl:script>

Then you could just use an xsl:if:

  <xsl:template match="master">
    <child>
      <circumference>
        <xsl:if select="user:checkCategory(category1,category2)">
           <xsl:text>Sorry, Combination doesn’t match.</xsl:text>
        </xsl:if>
      </circumference>
    </child>
  </xsl:template>
Sign up to request clarification or add additional context in comments.

2 Comments

You placed this comment in my answer when you tried to edit my answer: "i have modified : Second one to be category2 ..but still how we can use ConfigurationManager.AppSetting["Code"].contains("A1")) in xslt because its not present in XML ??" Have you tried using the full namespace to the configuration manager class? Otherwise, you could try passing in the value as an XSLT parameter to the XSLT.
Can you make this more clear in ConfigurationManager.AppSetting["Code"].contains("A1"))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.