xsl-list
[Top] [All Lists]

RE: [xsl] apply one template to another

2006-11-01 06:13:20
Sorry, I think my request may have been confusing and needed some more
detail perhaps -

Basically I am trying to generate a document from the /root/xform, other
nodes in /root are extra information that I have passed in. If certain
nodes in this extra information exist I want to replace the
corresponding node in /root/xform/xforms:model/xforms:instance/page with
them. These nodes may not always be at the top level of
xforms:instance/page.

My problem was that my two templates were kinda colliding somehow...


So basically if there is a /root/contact node then the output of the
xforms:instance would look like this - 

<xforms:instance>
                <page design="event">
                    <title/>
                    <description/>
                    <when>
                        <start/>
                        <end/>
                        <occurs>Once</occurs>
                    </when>
                    <contact>
                                <title>Miss</title>
                                <firstname>Jo</firstname>
                                <lastname>Smith</lastname>
                                <telephone>
                                    <number type="Office">01404
812345</number>
                                </telephone>
                        </contact>
                    <location>
                        <address/>
                        <town/>
                        <county/>
                        <postcode/>
                        <directions/>
                    </location>
                    <cost/>
                </page>
            </xforms:instance>

Otherwise, if the /root/contact node is NOT present the output for
xforms:instance would look like this -

<xforms:instance>
                <page design="event">
                    <title/>
                    <description/>
                    <when>
                        <start/>
                        <end/>
                        <occurs>Once</occurs>
                    </when>
                    <contact>
                        <title/>
                        <firstname/>
                        <lastname/>
                        <telephone/>
                    </contact>
                    <location>
                        <address/>
                        <town/>
                        <county/>
                        <postcode/>
                        <directions/>
                    </location>
                    <cost/>
                </page>
            </xforms:instance>

Thanks Adam.

-----Original Message-----
From: David Carlisle [mailto:davidc(_at_)nag(_dot_)co(_dot_)uk] 
Sent: 01 November 2006 10:30
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] apply one template to another



 I tried something like this but I couldnt get it to work, 
do you mean it gave an error, or it gave the  wrong answer?
It looks OK, although without seeing an input documemt it's impossible
to say if it matches any element in the input.


<xsl:template
match="/root/xform/xforms:model/xforms:instance/page/contact"
mode="copy">
        <xsl:choose>
            <xsl:when test="empty(/root/contact)">
                <xsl:copy-of select="."/>
            </xsl:when>   
            <xsl:otherwise>
                <xsl:copy-of select="/root/contact"/>
            </xsl:otherwise>
        </xsl:choose>
</xsl:template>



I'd probably write that as

<xsl:template match="contact" mode="copy">
                <xsl:copy-of select="(/root/contact,.)[1]"/>
</xsl:template>

(which is probably equivalent, if some assumptions about your input are
correct, if you can have multiple contact children you'd need to change
it slightly.)

But as you have it should work, as long as you do have an element
matching root/xform/xforms:model/xforms:instance/page/contact

David




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--