Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
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 Search this Thread Display Modes
  #1 (permalink)  
Old November 15th, 2005, 07:17 AM
Authorized User
 
Join Date: Mar 2005
Location: , , .
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default Slightly modified xsl:copy-of

Hi!

I have a poor formatted html-file, which includes xml-nodes:

<DATADOCUMENT>
<body>
Hi there!<br>
My name is <b>Brad</b><br>
Here is a photo of me:<br>

<IMGOBJECT>
<FILENAME>photo.gif</FILENAME>
<STYLE>
<PARAM attribute="border">1px solid black</PARAM>
</STYLE>
</IMGOBJECT>

I am interested in XSLT programming!
</body>
</DATADOCUMENT>

Today, I'm using <xsl:copy-of select="DATADOCUMENT" /> to generate the HTML and manually rewrite the IMGOBJECT node to:

<img src="photo.gif" style="border: 1px solid black" />

So that the output becomes:

<body>
Hi there!<br>
My name is <b>Brad</b><br>
Here is a photo of me:<br>

<img src="photo.gif" style="border: 1px solid black" />

I am interested in XSLT programming!
</body>

I have no permission to rewrite the data provider, so I'm wodering how I can rewrite the XSLT to produce such output.

Please help me with this one.

Thanx in advance!

Reply With Quote
  #2 (permalink)  
Old November 15th, 2005, 08:03 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I'm slightly surprised that you can process this input using XSLT at all, because it seems not to be well-formed XML - the <br> tags are not closed. Let's assume that's a red herring and it is well-formed.

Then the answer to your question, how to do a "slightly modified copy", is a classic XSLT design pattern. First you write an identity template that copies everything unchanged:

<xsl:template match="*">
  <xsl:copy><xsl:copy-of select="@*"/><xsl:apply-templates/></xsl:copy>
</xsl:template>

then you override this with a template that modifies the elements you want to modify:

<xsl:template match="IMGOBJECT">
  <img src="{FILENAME}">
    <xsl:attribute name="style">
    <xsl:for-each select="STYLE/PARAM">
      <xsl:value-of select="concat(@attribute, ': ', .)"/>
    </xsl:for-each>
  </img>
</xsl:template>

(You may have to refine that but it illustrates the idea)

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old November 15th, 2005, 08:38 AM
Authorized User
 
Join Date: Mar 2005
Location: , , .
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for your response.

I've tried to implement the code, but the data provided is much larger that I wrote in the example. And the transform is larger too.

However, this is a small piece of it:

<xsl:template match="ARTICLE">
    <h1><xsl:apply-templates select="SUMMARY/TITLE/text()" mode="TAG_TEXT"/></h1>
        <xsl:copy-of select="ELEMENTS/EDITORELEMENT/CONTENT/body"/>

</xsl:template>

If I use your <xsl:template match="ELEMENTS/EDITORELEMENT/CONTENT/*"> instead of the copy-of, I get an error (don't know which error though)

As you can see, they've used apply-templates for the title. Should I use that element?

Reply With Quote
  #4 (permalink)  
Old November 29th, 2005, 11:05 AM
Authorized User
 
Join Date: Mar 2005
Location: , , .
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I still haven't solved this problem, so I would really appreciate some help.

Reply With Quote
  #5 (permalink)  
Old April 10th, 2006, 04:21 AM
Registered User
 
Join Date: Apr 2006
Location: chennai, tamil nadu, India.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to samuelhotson Send a message via Yahoo to samuelhotson
Default

Sir,
I want to copy of one paragraph in another xml document.
how to get it.

i don't know whether using copy-of element or another element.

please help me.

thank you

samuel
Reply With Quote
  #6 (permalink)  
Old April 10th, 2006, 04:50 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If you want an exact copy, use xsl:copy-of

If you want a slightly modified copy, use the "modified identity transform" coding pattern. Write an identity template rule, and then override it with a template rule for the node you want to modify: see http://www.dpawson.co.uk/xsl/sect2/identity.html



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #7 (permalink)  
Old April 10th, 2006, 04:53 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

P.S. You should have started a new thread. It's not a good idea to piggyback your question on an existing thread. It confuses readers, and it probably confused me, because I thought the subject line was relevant to your post and it seems it might not have been.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
xsl:copy-of Issue DKatSSA BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition ISBN: 978-0-470-19274-0 8 November 19th, 2008 05:18 PM
Slightly off topic but how do I... Ceromus BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 0 October 21st, 2008 12:16 PM
How can i use xsl:copy-of in xsl sampath.bandaru XSLT 11 October 1st, 2008 03:37 AM
Copy one node to another in XSL hugoscp XML 5 September 14th, 2006 09:40 AM
xsl:copy copies everywhere ravik@olemiss.edu XSLT 1 November 11th, 2005 07:44 AM



All times are GMT -4. The time now is 11:54 PM.


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