Thursday, 31 December 2015

XML : XSLT transformation tips

This should be fairly easy but I am new to XSLT and I have a hard time finding the solution. I got the following XML:

  <catalog>      <book id="1">          <author>Mike</author>          <title>Tomas</title>      </book>  </catalog>    

and I am trying to add another book entry on top of it and change its id from 1 to 2. I have tried the following which fails to change the attribute value though.

  <?xml version='1.0'?>  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >     <xsl:template match="@id">      <xsl:attribute name="id">2</xsl:attribute>      <xsl:apply-templates/>  </xsl:template>    <xsl:template match="book">      <xsl:element name="book">          <xsl:attribute name="id">1</xsl:attribute>          <xsl:element name="author">author1</xsl:element>          <xsl:element name="title">title1</xsl:element>      </xsl:element>      <xsl:copy-of select="."/>  </xsl:template>    </xsl:stylesheet>    

Any suggestions?

No comments:

Post a Comment