tags:

views:

175

answers:

4

I have an xml file in which there is tag namely, <Gender/> It carries either 'M' or 'F' as data, now my work is to test the value and write <Gender_Tag>Male</Gender_Tag> or <Gender_Tag>Female</Gender_Tag> according to the values M or F respectively .. I tried this code .. It used to work in other circumstances..

<xsl:template match="root/details/Gender">
    <xsl:if test="Gender='M'">
        <Gender_Tag>
            <xsl:text>Male</xsl:text>
        </Gender_Tag>
    </xsl:if>
    <xsl:if test="Gender='F'">
        <Gender_Tag>
            <xsl:text>Female</xsl:text>
        </Gender_Tag>
    </xsl:if>
</xsl:template>

And here is my XML input ..

<root>
   <details>
    <Gender>M</Gender>
    <nickname/>
    <Alias>123aaaaaaaaa</Alias>
    <AlternatePhone>54654231aaaaa</AlternatePhone>
    <Fname>abc</Fname>
    <Lname>xyz</Lname>
    <PhoneNumber>345313213</PhoneNumber>
    <BirthDate/>
   </details>
</root>

Thank you every one, who all answered .. It was a bug which was troubling me .. :)

Code is working properly ... thank you "stackoverflow" :-)

+4  A: 

Untested, but may work...

<xsl:template match="root/details/Gender">
  <xsl:if test="text()='M'">
    <Gender_Tag>Male</Gender_Tag>
  </xsl:if>
  <xsl:if test="text()='F'">
    <Gender_Tag>Female</Gender_Tag>
  </xsl:if>
</xsl:template>
Scoregraphic
Its not working .. am only getting a space introduced in that place .. if I try to test an attribute value .. I can easily do it .. but I don't know what is the problem with comparing the value of the element ..
infant programmer
You need to add a sample of the XML you're trying to parse.
Murph
Thanx for the suggestion ..
infant programmer
+3  A: 
<xsl:template match="root/details/Gender">
    <xsl:choose>
        <xsl:when test="normalize-space(text())='M'">
            <Gender_Tag>Male</Gender_Tag>
        </xsl:when>
        <xsl:otherwise>
            <Gender_Tag>Female</Gender_Tag>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

My example differs in two points from Scoregraphic's:

  1. It uses xsl:choose to ensure, that only one Gender_Tag element is created (that also means, that if the text is not 'M', it is always a Female)

  2. Use of normalize-space() strips white space around the text content of the element.

Boldewyn
`text()` can never be F and M in common, but the use of `<xsl:choose>` is fine
Scoregraphic
thanks for informative suggestion .. :)
infant programmer
+2  A: 

Without seeing XML its hard to be certain, but I think your sample XSLT should be:

<xsl:template match="root/details/Gender">    
   <xsl:if test=".='M'">
      <Gender_Tag><xsl:text>Male</xsl:text></Gender_Tag>
   </xsl:if>
   <xsl:if test=".='F'">
      <Gender_Tag><xsl:text>Female</xsl:text></Gender_Tag>
   </xsl:if>
</xsl:template>

Use of choose as per another answer would be better (though I think it should be two explicit when clauses rather than a when and an otherwise)

Murph
Thanks for the suggestion ..
infant programmer
+7  A: 

All relative paths expressed in a template are evaluated against the current node. Your template match Gender elements, so Gender='M' returns true if there is any Gender's child named 'Gender' with the value 'M'. I guess this is not the case...

Use the dot to express the current node (here a Gender element):

<xsl:template match="root/details/Gender">
  <Gender_Tag>  
    <xsl:choose>
      <xsl:when test=".='M'">
        <xsl:text>Male</xsl:text>
      </xsl:when>
      <xsl:otherwise>
       <xsl:text>Female</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </Gender_Tag>
</xsl:template>

EDIT: You may use two templates too

<xsl:template match="root/details/Gender[.='M']">
  <Gender_Tag>Male</Gender_Tag>
</xsl:template>
<xsl:template match="root/details/Gender[.='F']">
  <Gender_Tag>Female</Gender_Tag>
</xsl:template>
Erlock
It worked dude .. Thank you very very very much .....................
infant programmer