Subject: doubt in using <xsl:template>
Posted By: rajatake Post Date: 12/15/2006 5:41:37 AM
Hi,

I am having one input xml file which has to be transformed to another xml file.

part of the input file
--------------------
<file>
  <type>text</type>
  <size>1024<type>
</file>


this has to be converted as
<fileInfo type="text" size=1024">

This <file > element may present in any of the level(file contain many nested elements)and may present in any number of times in the input XML file. Whereever i find this <file> element, i have to convert it.

I have written the following code

<xsl:template match="/">
 <xsl:apply-templates select="file"/>
</xsl:template>

<xsl:template match="file">
<fileInfo>
     <xsl:attribute name="type">
         <xsl:value-of select="@type"></xsl:value-of>
<xsl:attribute name="size">
         <xsl:value-of select="@size"></xsl:value-of>
</fileInfo>
</xsl:template>

This is not working for me. can any one help me.

Regards,
Raja







Reply By: joefawcett Reply Date: 12/15/2006 5:55:24 AM
You probably just need to change:
<xsl:apply-templates select="file"/>
to
<xsl:apply-templates select="//file"/>
to select all the descendant or self file elements. Alternatively you can use the identity pattern, and simplify your matching template:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

 <xsl:template match="file">
   <fileinfo type="{type}" size="{size}"/>
 </xsl:template>
</xsl:stylesheet>


--

Joe (Microsoft MVP - XML)
Reply By: mhkay Reply Date: 12/15/2006 6:12:08 AM
You say your file may contain nested elements, presumably nested file elements, but your template rule isn't doing anything to process them. It needs to call <xsl:apply-templates select="file"/> to recurse down the tree.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply By: joefawcett Reply Date: 12/15/2006 6:23:59 AM
Sorry, Michael's right, using //file would output all the file elements but not nested correctly. My stylesheet should work however.

--

Joe (Microsoft MVP - XML)
Reply By: rajatake Reply Date: 12/15/2006 6:27:46 AM
Joe,

  <xsl:template match="node()|@*"> is working fine for me. As I am newbie to XSL, i m trying to understand it. Can u suggest me a good book to understand XSL and XPATH.

Regards,
Raja

Reply By: joefawcett Reply Date: 12/15/2006 7:09:48 AM
Both these are excellent, I learned 90% of my stuff from the first edition, now they are two separate volumes.
XSLT is one of those subjects that you really need to study whilst not under time pressure. Eventually the penny will drop.

XPath 2.0 Programmer's Reference

XSLT 2.0 Programmer's Reference, 3rd Edition


--

Joe (Microsoft MVP - XML)

Go to topic 53677

Return to index page 92
Return to index page 91
Return to index page 90
Return to index page 89
Return to index page 88
Return to index page 87
Return to index page 86
Return to index page 85
Return to index page 84
Return to index page 83