English 中文(简体)
XSLT:How to deal with testing the value of an element?
原标题:

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

最佳回答

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>
问题回答
<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.

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>

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)





相关问题
When test hanging in an infinite loop

I m tokenising a string with XSLT 1.0 and trying to prevent empty strings from being recognised as tokens. Here s the entire function, based on XSLT Cookbook: <xsl:template name="tokenize"> ...

quick xslt for-each question

Let s say I have an XML document that has this: <keywords> <keyword>test</keyword> <keyword>test2</keyword> <keyword>test3</keyword> <keyword>test4</...

XSLT Transform XML with Namespaces

I m trying to transform some XML into HTML using XSLT. Problem: I can t get it to work. Can someone tell me what I m doing wrong? XML: <ArrayOfBrokerage xmlns:i="http://www.w3.org/2001/...

XSLT output to HTML

In my XSLT file, I have the following: <input type="button" value= <xsl:value-of select="name">> It s an error as it violates XML rule. What I actually want is having a value from an ...

Mangling IDs and References to IDs in XML

I m trying to compose xml elements into each other, and the problem I am having is when there s the same IDs. Basically what I need to do is mangle all the IDs in an xml file, as well as the ...

Sharepoint 2007 Data view Webpart custom parameters

I m sort of new to the custom parameters that can be setup on a DataView Webpart. There are 6 options: - None - Control - Cookie - Form - QueryString - Server Variable I think that None, Cookie and ...

热门标签