• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4010
  • Last Modified:

XSLT if condition

i want to implement something like this

if (tag is [<firstname> or <lastname>])
{
  do string mainpulation(string within the tag)
}

in XSLT where I am trying to modify the values in an XML using xslt before processing using java

  <xsl:template match="firstname | lastname">
      <xsl:variable name="inname"  select = "."/>
       <!--***** I want to add the if condition here ********-->
      <firstname>
        <xsl:copy-of select="date:formatName($inname)"/>
      </firstname>
      </xsl:if>
  </xsl:template>

My if condition is ->
if (tag name is firstname)
then
<firstname>
.........
</firstname>

if (tag name is lastname)
then
<lastname>
.........
</lastname>

0
applekanna
Asked:
applekanna
  • 2
2 Solutions
 
anthonywjones66Commented:
This will probably achieve what you need assuming your input xml truely is using the tag names 'firstname' and 'lastname' and that is exactly the name of  the tag you want to output:-

 <xsl:template match="firstname | lastname">
   <xsl:copy>
        <xsl:copy-of select="date:formatName(.)"/>
   </xsl:copy>
</xsl:template>

Anthony.

0
 
applekannaAuthor Commented:
to use the formatName() I need to add global variables in the xml like
<xsl:variable> how do i add varables that within these

     <xsl:template match="*">
          <xsl:copy>
               <xsl:for-each select="@*">
                    <xsl:copy-of select="." />
               </xsl:for-each>
               <xsl:apply-templates />
          </xsl:copy>
     </xsl:template>

but accessible here
 <xsl:template match="firstname | lastname">
   <xsl:copy>
        <xsl:copy-of select="date:formatName(.)"/>
   </xsl:copy>
</xsl:template>
0
 
anthonywjones66Commented:
I'm not entirely sure I know what you are asking but I'm guessing you have something like this:-

  <msxsl:script language="JScript" implements-prefix="date">
    function formatName(nodelist) {
      //some code to format a name.
    }
  </msxsl:script>

in you xsl and you are asking how to add global variable accessible to javascript code.  You should be able to do this:-

  <msxsl:script language="JScript" implements-prefix="date">
    var myGlobal1
    var myGlobal2

    function init(value1, value2)
    {
      myGlobal1 = value1
      myGlobal2 = value2
    }

    function formatName(nodelist) {
      //some code to format a name.
    }
  </msxsl:script>

BTW are you sure you can't get the formatting done by XSL?

Also what does formatName return?  It  looks like xsl:value-of would be more appropriate than xsl:copy-of.

Anthony
0
 
ThogekCommented:
Given the possible complexities of implementing your if-statement within XSL...

Question:
Is there a reason for not simply implementing two separate short-and-simple templates, such as:

    <xsl:template match="firstname">
        <xsl:variable name="inname" select = "." />
        <firstname>
            <xsl:copy-of select="date:formatName($inname)" />
        </firstname>
    </xsl:template>

    <xsl:template match="lastname">
        <xsl:variable name="inname" select = "." />
        <lastname>
            <xsl:copy-of select="date:formatName($inname)" />
        </lastname>
    </xsl:template>
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now