Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old December 15th, 2006, 06:41 AM
Authorized User
Points: 179, Level: 3
Points: 179, Level: 3 Points: 179, Level: 3 Points: 179, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2005
Location: chennai, Tamil Nadu, India.
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







Reply With Quote
  #2 (permalink)  
Old December 15th, 2006, 06:55 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
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)
Reply With Quote
  #3 (permalink)  
Old December 15th, 2006, 07:12 AM
mhkay's Avatar
Wrox Author
Points: 18,438, Level: 59
Points: 18,438, Level: 59 Points: 18,438, Level: 59 Points: 18,438, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,954
Thanks: 0
Thanked 290 Times in 285 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
Reply With Quote
  #4 (permalink)  
Old December 15th, 2006, 07:23 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
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)
Reply With Quote
  #5 (permalink)  
Old December 15th, 2006, 07:27 AM
Authorized User
Points: 179, Level: 3
Points: 179, Level: 3 Points: 179, Level: 3 Points: 179, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2005
Location: chennai, Tamil Nadu, India.
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

Reply With Quote
  #6 (permalink)  
Old December 15th, 2006, 08:09 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
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)
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
embedded <xsl:element> into <xsl:with-param> petergoodman XSLT 2 July 9th, 2008 07:36 AM
Performance for <xsl:import> and <xsl:include> vikkiefd XSLT 2 April 16th, 2008 09: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 07:30 AM
<xsl:choose> and <xsl:otherwise> problem djmarquette XSLT 4 January 21st, 2005 01:56 PM



All times are GMT -4. The time now is 12:14 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.