Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 15th, 2006, 06:41 AM
Authorized User
 
Join Date: Jun 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rajatake
Default doubt in using <xsl:template>

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







 
Old December 15th, 2006, 06:55 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You probably just need to change:
Code:
<xsl:apply-templates select="file"/>
to
Code:
<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:
Code:
<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)
 
Old December 15th, 2006, 07:12 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old December 15th, 2006, 07:23 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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)
 
Old December 15th, 2006, 07:27 AM
Authorized User
 
Join Date: Jun 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rajatake
Default

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

 
Old December 15th, 2006, 08:09 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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)





Similar Threads
Thread Thread Starter Forum Replies Last Post
embedded <xsl:element> into <xsl:with-param> petergoodman XSLT 2 July 9th, 2008 06:36 AM
Performance for <xsl:import> and <xsl:include> vikkiefd XSLT 2 April 16th, 2008 08:06 AM
<xsl:for-each> inside another <xsl:for-each> suersh79 XSLT 2 December 29th, 2006 01:24 AM
Doubt in XPATH synax in <xsl:apply-templates > rajatake XSLT 3 May 4th, 2006 06:30 AM
<xsl:choose> and <xsl:otherwise> problem djmarquette XSLT 4 January 21st, 2005 01:56 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.