0

I have an xml as below.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
  <h2>My CD Collection</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th>Title</th>
      <th>Artist</th>
    </tr>
    <xsl:for-each select="catalog/cd">
    <tr>
      <td><xsl:value-of select="title"/></td>
      <td><xsl:value-of select="artist"/></td>
    </tr>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet> 

3 Answers 3

1

This is a complete, yet short and easy transformation:

<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="attributeName[.='salience']">
  <salience>
   <xsl:value-of select="../value"/>
  </salience>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

and when applied on the provided XML document:

<attributes>
    <attribute>
        <attributeName>agenda-group</attributeName>
        <value>common</value>
    </attribute>
    <attribute>
        <attributeName>auto-focus</attributeName>
        <value>true</value>
    </attribute>
    <attribute>
        <attributeName>no-loop</attributeName>
        <value>true</value>
    </attribute>
    <attribute>
        <attributeName>salience</attributeName>
        <value>73</value>
    </attribute>
</attributes>

the wanted, correct result is produced:

<salience>73</salience>
Sign up to request clarification or add additional context in comments.

2 Comments

I have tried the above Approach.But its not working for me.But i have tried the below solution which is working for me but dont know whether this is optimized solution or not. YOur help would be appreciated.Please find my below solution
@srinivas: Just copy and paste the transformation, then run it on the XML document you provided -- you must get the wanted result.
0

Try this:

  <xsl:template match="attributes/attribute">
    <xsl:if test=".//attributeName='salience'">
      <xsl:value-of select=".//value"/>
    </xsl:if>
  </xsl:template>

P.S. Please format your post, as the XSL is not showing.

2 Comments

The above solution is not working.I am getting nothing in the output file.
Take note of <xsl:template match="attributes/attribute">. It is different from what you have. I have tried this and it worked. Please double check how you implemented it.
0

The main problem is this xsl:if statement

<xsl:if test="//attributes//attribute[(attributeName = 'salience')]">

At this point, the context is still the root node, so all this does is checking the existing of an attibute element, you are not actually positioning yourself on the node. Thus, when you do the xsl:value-of you are simply getting the first value in the XML.

Instead of using an xsl:if, you should probably try matching the attribute element, like so

<xsl:apply-templates select="attributes/attribute[attributeName = 'salience']"/>

The whole XSLT would be as follows

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="/">
      <xsl:apply-templates select="attributes/attribute[attributeName = 'salience']"/>
   </xsl:template>
   <xsl:template match="attribute">
      <xsl:element name="{attributeName}">
         <xsl:value-of select="value"/>
      </xsl:element>
   </xsl:template>
</xsl:stylesheet>

When applied on your input XML, the output is as follows:

<salience>73</salience>

Note the use of xsl:element

<xsl:element name="{attributeName}">

This avoids having to hard-code salience in your matching template, making it more generic should you wish to match other elements in a similar fashion.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.