Re: [xsl] apply one template to another

Subject: Re: [xsl] apply one template to another
From: Robert Koberg <rob@xxxxxxxxxx>
Date: Wed, 01 Nov 2006 08:17:39 -0500
Adam Retter wrote:
I am using a copy pattern that Michael Kay very kindly described
originally for me.
These are the last two templates in my document that do the copying-

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


Do you need the identity template to be in a mode. In other words, could you take out the mode and have it be the default. That way you could apply templates here:
...
<xsl:choose>
<xsl:when test="empty(/root/page)">
<xsl:apply-templates"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="/root/page"/>
</xsl:otherwise>
</xsl:choose>
...
and just match the page elem (or anything else) to override the default copy behavior.


best,
-Rob






Now I have a template that makes a conditional copy of the
/root/xform/xforms:model/xforms:instance node -

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


Now, what I would like to do is apply a template to that template, so that I can conditionally replace the /root/xform/xforms:model/xforms:instance/page/contact node with a custom node. I tried something like this but I couldnt get it to work, I also tried playing with priorities and modes but didnt make any progress.

<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>



If you could provide me with details of how to acheive that, I think I could extend that to other nodes that I want to conditinally replace with custom nodes -

/root/xform/xforms:model/xforms:instance/page/location
/root/xform/xforms:model/xforms:instance/page//start
/root/xform/xforms:model/xforms:instance/page//end

Thanks very much - Adam Retter


For reference my XML document structure looks something like this -


<root>
    <contact>
        <title>Miss</title>
        <firstname>Jo</firstname>
        <lastname>Smith</lastname>
        <telephone>
            <number type="Office">01404 812345</number>
        </telephone>
        </contact>
    <location>
        <address>The Old Institute</address>
        <address>Yonder Street</address>
        <town>Ottery St Mary</town>
        <county>Devon</county>
        <postcode>EX11 1XX</postcode>
    </location>
    <start>2006-11-01T09:00:00.00</start>
    <end>2006-11-01T18:00:00.00</end>
    <xform xmlns:xlink="http://www.w3.org/1999/xlink";
xmlns:xs="http://www.w3.org/2001/XMLSchema";
xmlns:ev="http://www.w3.org/2001/xml-events"; xmlns:xforms
        ="http://www.w3.org/2002/xforms";>
        <xforms:model ev:event="xforms-revalidate"
ev:defaultAction="cancel">
            <xforms:submission id="submit"
action="http://localhost:8080/exist/servlet/db/CommunityDirectory/index.
xql?action=savepage" method="post" replace="all"/>
            <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>
      </xforms:model>
   </xform>
</root>

Adam Retter

Current Thread