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 August 11th, 2005, 09:30 AM
Authorized User
 
Join Date: Jun 2005
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default Adding nodes to grandparent of current

I have a question concerning adding elements to ancestor nodes of the current. In the past, i have found ways of getting around this problem, however, i am a situation that this is only method that i can think of to get the output that i need.
Here is the summary of my situation: i have a template that will search through the body of xml doc and create a paragraph everytime it finds a w:p (paragraph) element. Easy enough; the problem is shapes can appear in the middle of the paragraph and the xml output must have all of its objects (paragraphs, shapes, tables, etc.) completely seperated. They can not be flowed together. So i need to make the current node an ancestor of the current, create the shape element with it's attributes and child nodes, and then finish up the paragraph.

Here is a sample of input that i could receive:

<body>

<para>
<text> Here would be the beginning of a paragraph.</text>
<text> OH! looky here, some more text.</text>
<text> Last text before a shape.</text>
<shape type="rectangle" />
<text> Here would be the sum of rest of the text runs.</text>
</para>

<para>
<text> Here would be another paragraph.</text>
</para>

<para>
<text> And another paragraph, but no shapes. </text>
</para>
<body>

So here are the templates that i am trying to use:

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

<xsl:template match="para">
    <new-paragraph>
        <make attributes/>
        <xsl:for-each select="child::*">
            <xsl:if test="text">
                <text-run><xsl:value-of select="text"/></text-run>
            </xsl:if>
            <xsl:if test="shape">
                <xsl:element name="new-paragraph">
                <new-shape>
                    <xsl:value-of select="shape/@type"/>
                </new-shape>
                <xsl:element name="new-paragraph">
                    <give new new-paragraph same attributes/>
            </xsl:if>
        </xsl:for-each>
    </new-paragraph>
</xsl:template>

This would be the type of format that i need to be the output.
<new-body>
    <new-paragraph>
        <text-run/>
        <text-run/>
        <text-run/>
        <text-run/>
    </new-paragraph>
    <new-shape/>
    <new-paragraph>
        <text-run/>
        <text-run/>
        <text-run/>
        <text-run/>
    </new-paragraph>
    <new-paragraph>
        <text-run/>
        <text-run/>
        <text-run/>
        <text-run/>
    </new-paragraph>
</new-body>
As you can see the shape is outside of the paragraph. It probably is wiser to use templates instead of for-each and if statements, but i don't see anyway around this problem.

 
Old August 12th, 2005, 02:59 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The clue to this kind of problem is to structure your stylesheet according to the output you want to produce, not according to the input. Ask your self "What do I want to do next? I want to produce an X if the input is an ordinary paragraph, but a Y if the input is a paragraph with a <shape> element somewhere inside it". Your description of the problem suggests you are thinking the opposite way around, structuring your code according to the form of the input. Remember that XPath gives you random access to the source document, but the output document is always written sequentially.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old August 12th, 2005, 09:29 AM
Authorized User
 
Join Date: Jun 2005
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the advice, I see what i need to do. As an alternative, could i use an if statement when element nodes that will allow be to back out to the parent node, <body>, create the shape, then use element nodes again to create another paragraph that will have the text runs from the rest of the paragraph? I see that this deviates from my original goal, however, for the final output it will displayed the same.







Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding and Sorting nodes georgemeng XSLT 4 December 1st, 2008 12:13 PM
How to value in b/n nodes Swetha XSLT 1 May 20th, 2008 12:20 PM
Adding nodes to JTree sriharshareddyk Java Basics 0 May 4th, 2007 01:18 PM
adding prefix to nodes and subnodes to output xml raghurns XSLT 9 November 17th, 2006 04:41 PM
Nodes ....'For Each' problem. Neal XSLT 3 February 13th, 2006 08:57 AM





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