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 February 16th, 2008, 03:04 AM
Authorized User
 
Join Date: Jul 2007
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Default Moving markup to different position

Hi all,

I've requirement wherein I need to move an element and its subsequent child element to other location after transformation (this is basically for print purposes). I am trying to achieve this with XSLT. Let me try again with Sample.

Input:
<article>
<meta>
<journalcode>CEDE</journalcode>
<issn type="print">XXXX-XXXX</issn>
<author primary="yes" corr="yes" seq="1"><name><givenname>Magdalena</givenname><surname>Norberg&hyphen;Schönfeldt</surname></name>
<contact corresponding="yes"/>
</author>
</meta>
<journaltitle>Education Economics</journaltitle>
<title><newpage pagenumber="1"/>Children’s school achievement and parental work: An analysis for Sweden</title>
</article>

Output Required:
<article>
<meta>
<journalcode>CEDE</journalcode>
<issn type="print">XXXX-XXXX</issn>
</meta>
<journaltitle>Education Economics</journaltitle>
<title><newpage pagenumber="1"/>Children’s school achievement and parental work: An analysis for Sweden</title>
[b]<author primary="yes" corr="yes" seq="1"><name><givenname>Magdalena</givenname><surname>Norberg&hyphen;Schönfeldt</surname></name>
<contact corresponding="yes"/>
</author>[b]
</article>

If you notice I need to place the <author> info after <title> after transforming. I am able to that with <xsl:value-of>, but unfortunately it takes only the string values contained by author and not its subsequent child elements, something like this:
<author primary="yes" corr="yes" seq="1">MagdalenaNorberg[#x02010]Sch[#x000F6]nfeldt</author>.

One way of achieving it with use of PERL etc to do search/replace before you actually transform the xml input, which is least desirable, as I would not prefer to play with input XML.

I've been using a Xpath function "content-markup" but altogether in different application and I don't think there is something like has been defined in Xpath/XSLT 1.0 (at least I have not found in Michael Kay Programmer's Reference book).

Any ideas how this can be done or where I should look into to get the solution.



















Pankaj
__________________
Pankaj
 
Old February 16th, 2008, 04:58 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Use xsl:copy-of rather than xsl:value-of to copy a whole element.

/- Sam Judson : Wrox Technical Editor -/
 
Old February 16th, 2008, 05:03 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Problems like this are always tackled using an identity template for the elements that don't need to change:

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

coupled with specific templates for the elements that do need to change:

<xsl:template match="meta">
  <xsl:apply-templates select="journalcode|issn"/>
</xsl:template>

<xsl:template match="article">
  <xsl:apply-templates select="*"/>
  <xsl:apply-templates select="meta/author"/>
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 18th, 2008, 05:39 AM
Authorized User
 
Join Date: Jul 2007
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Michael and Sam, its works. I've another issue <xsl:copy> copies all <author>s and their corresponding children as it is, which is quite fair which I asked earlier.

Problem: I need to put few punctuation in author <surname> and <givenname> + "," between two authors and "and" between second last and last author, something like below:


<author primary="yes" corr="yes" seq="1"><name><givenname>Magdalena</givenname> <surname>Norberg&hyphen;Schönfeldt</surname></name>
<contact corresponding="yes"/>
</author>, <author primary="yes" corr="yes" seq="1"><name><givenname>Magdalena</givenname> <surname>Norberg&hyphen;Schönfeldt</surname></name>
<contact corresponding="yes"/>
</author> and <author primary="yes" corr="yes" seq="1"><name><givenname>Magdalena</givenname> <surname>Norberg&hyphen;Schönfeldt</surname></name>
<contact corresponding="yes"/>
</author>

Getting punctuation ",. and "and" " would have easily done if movment of authors were not required. But in case I need to output <author>s with required punctuation.

Is there is any way of getting that before/after copying <author> (Just let me know the way around and rest will be done by me as i need to include other punctuation, not described here just to avoid the complexity).

Also, how to avoid <author> which are outputting as child to <title> (which is understood as they have copied match="article/title"). Below is the snippet, I've written for testing purposes as of now:


    <xsl:template match="article/title">
        <xsl:element name="title">
    <xsl:attribute name="aid:pstyle">art_title</xsl:attribute>
            <xsl:copy-of select="@*"/>
        <xsl:apply-templates/>
        </xsl:element>
        <xsl:copy>
    <xsl:copy-of select="preceding-sibling::meta/author"/>
        </xsl:copy>
    </xsl:template>

Sorry for asking so much.

Tanks





Pankaj
 
Old February 18th, 2008, 06:00 AM
Authorized User
 
Join Date: Jul 2007
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Also I need to add new attributes to <author> and <affiliation id="AF0001"> elements.

Thanks a lot.

Pankaj





Similar Threads
Thread Thread Starter Forum Replies Last Post
drag-and-drop from toolbox to Markup View Rex777 BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 5 September 22nd, 2008 01:54 PM
P2PML - Peer-to-Peer Markup Language mlauzon XML 0 November 2nd, 2007 11:16 AM
Markup Text to XML ROCXY XSLT 4 October 18th, 2007 05:04 AM
any markup language to XML bcogney XSLT 10 April 16th, 2007 08:39 AM
Unwanted z-index in html markup using VWD VictorVictor ASP.NET 2.0 Basics 3 July 25th, 2006 07:51 AM





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