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 July 13th, 2009, 07:15 AM
Registered User
 
Join Date: Jul 2009
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default Copy entire document after transform

Hi,

I'm attempting a simple (!) xsl transformation, taking an input xml document, adding elements, and outputting an exact copy with the added element data.

For this I apply a template to insert the required element data and perform an identity transformaion to copy all remaining information from the input xml.

However, the identity transformation is dropping some text from first level elements and I just can't find the 'magic formula' in the identity transformation to prevent this. Please help!!


Input xml:
<?xml version="1.0" encoding="US-ASCII"?>
<!DOCTYPE exercise SYSTEM "exercise.dtd">
<exercise bg-graphic="1" version="1" id="MEP005076" template="21">
<metadata>
<rf>
<level name="ELE"/>
<age range="AUT"/>
<english dialect="BRI"/>
<exam>KET</exam>
<content type="MEP"></content>
</rf>
</metadata>
<title>Paper 1 Part 5</title>
<subhead>Reading and Writing: gapped text</subhead>
<desc-grp>
<desc lang="EN">This exercise is suitable for KET. This Reading and Writing Part 5 exercise focuses mainly on grammar. You choose the correct words to complete a text about Pete Sampras.</desc>
</desc-grp>
<task id="005076TAS01" feedback="y">
<rubric-grp zonenum="1" sequence="1">
<rubric lang="EN">
<para>Click on the correct word in each drop-down menu to complete the text.</para>
</rubric>
</rubric-grp>
<activity zonenum="2" sequence="1">
<gapfill type="dropdown">
<gappara>
<emph type="bold">Pete Sampras</emph>
</gappara>
<gappara>Pete Sampras is one of <gap>
<answer true="y">A the</answer>
<answer true="n">B some</answer>
<answer true="n">C these</answer>
</gap> best tennis players in history. He began <gap>
<answer true="n">A play</answer>
<answer true="n">B played</answer>
<answer true="y">C playing</answer>
</gap> tennis when he <gap>
<answer true="n">A got</answer>
<answer true="n">B had</answer>
<answer true="y">C was</answer>
</gap> seven years old. He found an old tennis racquet in his house. His parents didn't have enough money to pay <gap>
<answer true="n">A on</answer>
<answer true="y">B for</answer>
<answer true="n">C with</answer>
</gap> tennis lessons <gap>
<answer true="y">A so</answer>
<answer true="n">B why</answer>
<answer true="n">C that</answer>
</gap> he practised by himself.</gappara>
<gappara>
<br/>
</gappara>
<gappara>Then the family moved to California. Here, they joined a tennis club and Pete played tennis a lot. <gap>
<answer true="n">A He</answer>
<answer true="n">B There</answer>
<answer true="y">C It</answer>
</gap> became clear that Pete was very good indeed and <gap>
<answer true="y">A in</answer>
<answer true="n">B then</answer>
<answer true="n">C before</answer>
</gap> a few years he was winning championships. Pete retired in 2003.</gappara>
<gappara>
<br/>
</gappara>
<gappara>Although Pete was a very competitive player, <gap>
<answer true="n">A his</answer>
<answer true="y">B he</answer>
<answer true="n">C him</answer>
</gap> is a very easy-going and kind person.</gappara>
</gapfill>
</activity>
</task>
<help>
<hint-grp>
<hint lang="EN">
<para>Read the article and the questions very carefully. Decide which answer you think is correct by thinking about the complete sentence, not just the word before each gap.</para>
</hint>
</hint-grp>
</help>
<decoration zonenum="3" sequence="1">
<graphic id="ill-xxx-004434-xxx-v001"/>
</decoration>
</exercise>


XSL:
All I'm doing is adding elements to the metadata tag and copying everything else across, the xsl is currently:


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">

<!-- Standard matching to root element. -->
<xsl:template match="/">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>


<xsl:template match="metadata">
<metadata>
<xsl:apply-templates select="@* | *"/>
<flash_player version="1.0"/>
<example_text></example_text>
<time amount="5 mins"/>
</metadata>
</xsl:template>

<!-- This is the catch-all to process all other elements, i.e. outputting all other elements unchanged.-->
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>


Output XML:
The output is as follows. Please note the dropped 'exam', 'title' text, etc:

<?xml version="1.0" encoding="UTF-8"?>
<exercise bg-graphic="1" version="1" id="MEP005076" template="21">
<metadata xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<rf>
<level name="ELE"/>
<age range="AUT"/>
<english dialect="BRI"/>
<exam/>
<content type="MEP"/>
</rf>
<flash_player version="1.0"/>
<example_text/>
<time amount="5 mins"/>
</metadata>
<title/>
<subhead/>
<desc-grp>
<desc lang="EN">This exercise is suitable for KET. This Reading and Writing Part 5 exercise focuses mainly on grammar. You choose the correct words to complete a text about Pete Sampras.</desc>
</desc-grp>
<task id="005076TAS01" feedback="y">
<rubric-grp zonenum="1" sequence="1">
<rubric lang="EN">
<para/>
</rubric>
</rubric-grp>
<activity zonenum="2" sequence="1">
<gapfill type="dropdown">
<gappara>
<emph type="bold">Pete Sampras</emph>
</gappara>
<gappara>Pete Sampras is one of <gap>
<answer true="y">A the</answer>
<answer true="n">B some</answer>
<answer true="n">C these</answer>
</gap> best tennis players in history. He began <gap>
<answer true="n">A play</answer>
<answer true="n">B played</answer>
<answer true="y">C playing</answer>
</gap> tennis when he <gap>
<answer true="n">A got</answer>
<answer true="n">B had</answer>
<answer true="y">C was</answer>
</gap> seven years old. He found an old tennis racquet in his house. His parents didn't have enough money to pay <gap>
<answer true="n">A on</answer>
<answer true="y">B for</answer>
<answer true="n">C with</answer>
</gap> tennis lessons <gap>
<answer true="y">A so</answer>
<answer true="n">B why</answer>
<answer true="n">C that</answer>
</gap> he practised by himself.</gappara>
<gappara>
<br/>
</gappara>
<gappara>Then the family moved to California. Here, they joined a tennis club and Pete played tennis a lot. <gap>
<answer true="n">A He</answer>
<answer true="n">B There</answer>
<answer true="y">C It</answer>
</gap> became clear that Pete was very good indeed and <gap>
<answer true="y">A in</answer>
<answer true="n">B then</answer>
<answer true="n">C before</answer>
</gap> a few years he was winning championships. Pete retired in 2003.</gappara>
<gappara>
<br/>
</gappara>
<gappara>Although Pete was a very competitive player, <gap>
<answer true="n">A his</answer>
<answer true="y">B he</answer>
<answer true="n">C him</answer>
</gap> is a very easy-going and kind person.</gappara>
</gapfill>
</activity>
</task>
<help>
<hint-grp>
<hint lang="EN">
<para/>
</hint>
</hint-grp>
</help>
<decoration zonenum="3" sequence="1">
<graphic id="ill-xxx-004434-xxx-v001"/>
</decoration>
</exercise>


HELP!!:
Any help would be much appreciated, I've tried lots of various identity transformations all with the same result, so please anyone who really knows there stuff enlighten me!!

All of the following transformation permutations give the same result:
<!--
<xsl:template match="/*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>

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

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

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

<!-- IdentityTransform -->
<!--<xsl:template match="/ | @* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>-->

<!--
<xsl:template match="/">

<xsl:copy-of select="."/>

</xsl:template>-->

<!-- XSLT Template to copy anything, priority="-1" -->
<!--<xsl:template match="/|*|@*|node()|@*/node()" priority="-1">
<xsl:copy>
<xsl:apply-templates select="*|@*|node()|@*/node()"/>
</xsl:copy>
</xsl:template>-->

<!--
<xsl:template
match="*|@*|comment()|processing-instruction()|text()">
<xsl:copy>
<xsl:apply-templates
select="*|@*|comment()|processing-instruction()|text()"/>
</xsl:copy>
</xsl:template>-->

<!--
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

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

<!--
<xsl:template match="metadata/rf/exam">
<exam>some text</exam>
</xsl:template>-->

<!--
<xsl:template match="/">
<xsl:copy-of select="."/>
</xsl:template>
-->
<!--
<xsl:template match="*/text()">
<xsl:copy-of select="."/>
</xsl:template>
-->
<!--
<xsl:template
match="*|@*|comment()|processing-instruction()|text()">
<xsl:copy>
<xsl:apply-templates
select="*|@*|comment()|processing-instruction()|text()"/>
</xsl:copy>
</xsl:template>
-->


Thanks. Adam
 
Old July 13th, 2009, 07:22 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

It appears to all work fine for me if I remove the DOCTYPE declaration, so maybe that is your problem.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?

Last edited by samjudson; July 13th, 2009 at 07:22 AM.. Reason: spotted error.
The Following User Says Thank You to samjudson For This Useful Post:
macmillan01 (July 13th, 2009)
 
Old July 13th, 2009, 07:28 AM
Registered User
 
Join Date: Jul 2009
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Great, that seems to have sorted the problem, that was driving me nuts.
We do need the dtd declaration when we process the document through Xalan, but it seems to be messing up XMLSpy for a simple, single document transformation.
I'll push it through Xalan and see what happens.

Thanks very much. Adam.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy a document to another justabeginner Word VBA 1 March 31st, 2007 06:44 AM
Open document, Open second doc and copy to first justabeginner Word VBA 1 March 7th, 2007 02:47 AM
copy entire row stealthdevil Access VBA 3 December 1st, 2006 11:39 AM
deleting entire database x_ray Classic ASP Basics 3 April 4th, 2006 03:02 PM
entire book list stefankoegl Forum and Wrox.com Feedback 1 July 4th, 2003 10:32 AM





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