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 November 15th, 2005, 07:17 AM
Authorized User
 
Join Date: Mar 2005
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!

 
Old November 15th, 2005, 08:03 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
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
 
Old November 15th, 2005, 08:38 AM
Authorized User
 
Join Date: Mar 2005
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?

 
Old November 29th, 2005, 11:05 AM
Authorized User
 
Join Date: Mar 2005
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.

 
Old April 10th, 2006, 04:21 AM
Registered User
 
Join Date: Apr 2006
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
 
Old April 10th, 2006, 04:50 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
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
 
Old April 10th, 2006, 04:53 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
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





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 [email protected] XSLT 1 November 11th, 2005 07:44 AM





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