2

Here is my XML :

<root>
<parameters>
<parameter>
    <key>sEmail</key>
    <value>cauchyjp@gmail.com</value>
    <type>string</type>
    <length>255</length>
</parameter>
<parameter>
    <key>sFirstName</key>
    <value>Bill</value>
    <type>string</type>
    <length>40</length>
</parameter>
</parameters>
<root>

I want to retrieve the value of the key sEmail and add it as an attribute to the root element using XML to XML xslt.

Here is the result expected :

<root email="cauchyjp@gmail.com">
<parameters>
    <parameter>
        <key>sEmail</key>
        <value>cauchyjp@gmail.com</value>
        <type>string</type>
        <length>255</length>
    </parameter>
    <parameter>
        <key>sFirstName</key>
        <value>Bill</value>
        <type>string</type>
        <length>40</length>
    </parameter>
</parameters>
<root>

Something similar to this link but with value of element instead of attributes.

Can someone help me ? Thanks

Community
  • 1
  • 1
Lucas
  • 271
  • 2
  • 4
  • 12

2 Answers2

2

You can test if a subelement's value is 'sEmail':

<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="parameters">
    <parameters>
      <xsl:attribute name="email">
        <xsl:value-of select="parameter[key='sEmail']/value"/>
      </xsl:attribute>
      <xsl:copy-of select="*"/>
    </parameters>
  </xsl:template>

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

</xsl:stylesheet> 
topskip
  • 16,207
  • 15
  • 67
  • 99
  • perfect, can accept your answer in 2min. Was trying to do the same but trhough a variable, don't know why it didn't worked – Lucas May 22 '12 at 10:15
  • @Lucas you are welcome to ask why your approach didn't work (in a new question). This might help understanding XSLT, it usually does for me. – topskip May 22 '12 at 10:19
2

Can be done in a cleaner way using AVT:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <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="/*">
  <root email="{*/*[key = 'sEmail']/value}">
    <xsl:apply-templates/>
  </root>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied to the provided XML document:

<root>
    <parameters>
        <parameter>
            <key>sEmail</key>
            <value>cauchyjp@gmail.com</value>
            <type>string</type>
            <length>255</length>
        </parameter>
        <parameter>
            <key>sFirstName</key>
            <value>Bill</value>
            <type>string</type>
            <length>40</length>
        </parameter>
    </parameters>
</root>

the wanted, correct result is produced:

<root email="cauchyjp@gmail.com">
   <parameters>
      <parameter>
         <key>sEmail</key>
         <value>cauchyjp@gmail.com</value>
         <type>string</type>
         <length>255</length>
      </parameter>
      <parameter>
         <key>sFirstName</key>
         <value>Bill</value>
         <type>string</type>
         <length>40</length>
      </parameter>
   </parameters>
</root>
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431