tags:

views:

34

answers:

3

My system produces a XML that contain a node that, depending on the type of event, came with different name. The name can be <floatRate> or <fixedRate>. The path is always the same, only the node name that is different.

I need a transformation that can populate one field based on that name. The field will be called <type> and the contend must be float or fixed, based on the node name. Can this be done?

A: 

Select/mach expressions can do boolean "or", as in

match="floatRate|fixedRate"

Edit your question and add samples of input, expected output and what you've already tried to get more help

Jim Garrison
This isn't "boolean or" this is the XPath union operator.
Dimitre Novatchev
I didn't realize that select/mach expressions can do boolean (or union). More, that this is the simpliest way to do what i need.Tanks for the help. With this and the examples that the other give, i believe i find a working solution.
Daniel Corsi
A: 

Suppose we have this document as input:

<result>
    <floatRate>1.1</floatRate>
</result>

This stylesheet:

<xsl:stylesheet
          xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
          version="1.0">
    <xsl:template match="result">
        <type>
        <xsl:value-of select="substring-before(name(floatRate|fixedRate),'Rate')"/>
        </type>
    </xsl:template>
    <xsl:template match="text()"/>
</xsl:stylesheet>

Output:

<type>float</type>
Alejandro
I don't get the need for the <xsl:template match="text()"/> expression. What is it for?
Daniel Corsi
@Daniel Corsi: That template overwrite built-in template for text nodes (just output its value). In this case, is not really necessary: `result` template breaks the recursion before reaching the text node of `floatRate`.
Alejandro
+1  A: 

This transformation:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="fixedRate|floatRate">
   <type><xsl:value-of select=
           "substring-before(name(),'Rate')"/></type>
 </xsl:template>
</xsl:stylesheet>

when applied on this XML document:

<rates>
    <fixedRate>2.3</fixedRate>
    <floatRate>1.1</floatRate>
</rates>

produces the wanted result:

<rates>
   <type>fixed</type>
   <type>float</type>
</rates>
Dimitre Novatchev
I believe i'll use something like this. Tanks again Dimitre.
Daniel Corsi