RE: [xsl] generating id by calling template but how to use it at other places

Subject: RE: [xsl] generating id by calling template but how to use it at other places
From: "Roger Glover" <glover_roger@xxxxxxxxx>
Date: Thu, 6 Feb 2003 12:54:34 -0600
Roger Glover [mailto:glover_roger@xxxxxxxxx] wrote:
>
> Jinesh Varia wrote:
----- snip -----
> > Please comment on ###1,
>
> ###1 was a typo, see above.
>
>
> > ###3
> > I want to know what select statement should I use?
> > I thought using a
> >
> > <xsl:variable name="tempperid">
> > <xsl:call-template name="generate-author-id" />
> > </xsl:variable>
> >
> > and use $tempperid everywhere. But this does not work
> > since the XSL is a decraraltive language and the value
> > of the variable remains the same.
>
> So what *I* would do instead is generate *ALL* the "perid"s (and
> the "person" elements around them) in a global variable (see
> above) and extract the "perid"s you need when you need them.
>
> I would be glad to work up a some demo code for you (being out of
> work does give me a fair amount of free time for this sort of
> thing) :-)-:.  But it is nearly midnight on a cold night in
> Minnesota, so I am off to bed.  If you don't have any better
> answers by (my) morning, I will be glad to see what I can do for you.

See demo code below.  I ran this with the version of Xalan-J included in
Sun's Java J2SE 1.4.1 release.

========================
pubs.xml
================================================
<pubs>
<publication pubid="0002">
<author>steve lawer</author>
<isbn>010101010101</isbn>
<title>How I Did That</title>
<publisher>Been There Press</publisher>
<copyright>2001</copyright>
</publication>
<publication pubid="0002">
<author>rick maker</author>
<isbn>010101010202</isbn>
<title>Why I Went There</title>
<publisher>Done That Books</publisher>
<copyright>1998</copyright>
</publication>
<publication pubid="0003">
<author>rick maker</author>
<isbn>010101020202</isbn>
<title>There and Back Again</title>
<publisher>Done That Books</publisher>
<copyright>2000</copyright>
</publication>
<publication pubid="0004">
<author>rick maker</author>
<author>steve lawer</author>
<isbn>010102020202</isbn>
<title>Been There, Done That: The Kareem Abdul Jabbar Story</title>
<publisher>Stringbean Publications</publisher>
<copyright>2002</copyright>
</publication>
</pubs>
================================================

========================
pubRegroup.xsl
================================================
<xsl:transform version="1.1"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:output method="xml" indent="yes"
xmlns:xalan="http://xml.apache.org/xalan"; xalan:indent-amount="4"/>

<xsl:variable name="persons">
    <xsl:apply-templates
select="//publication/author[not(.=preceding::author)]"
mode="generate-person"/>
</xsl:variable>

<!-- Similar to original "generate-author-id" template, but generates entire
person element-->
<xsl:template match="author" mode="generate-person">
    <xsl:variable name="temp" select="concat('800000000',position())" />
    <xsl:variable name="perid"
select="substring($temp,string-length($temp)-9)"/>
    <person perid="{$perid}">
        <personname>
            <xsl:value-of select="."/>
        </personname>
    </person>
</xsl:template>

<!-- I had to make up "pubs" since there was not a top level element in the
original XML source example -->
<xsl:template match="pubs">

	<!-- I had to make up "pubs2" since there was not a top level element in
the original XML result example -->
    <pubs2>

        <!-- copies the "person" elements result tree fragment into the
result tree -->
        <xsl:copy-of select="$persons"/>
        <xsl:apply-templates select="publication"/>
    </pubs2>
</xsl:template>

<xsl:template match="publication">

    <!-- Same as in the original code -->
    <publication>
        <xsl:copy-of select="@*|*[not(self::author or self::editor)]"/>
    </publication>

    <!-- calls template to create "pubper" elements, one per publication per
pub author -->
    <xsl:apply-templates select="author"/>
</xsl:template>

<!-- creates "pubper" elements -->
<xsl:template match="author">
    <pubper>

        <!-- gets "pubid" from parent  -->
        <pubid>
            <xsl:value-of select="../@pubid"/>
        </pubid>

        <!-- gets "perid" from "$persons" variable -->
        <perid>

            <!-- Note that in XSLT 1.0 a result tree fragment like
"$persons" does
                 not automatically convert to a node set.  Therefore most
processors
                 provide an extension function for that purpose
                 (like "xalan:nodeset()" below) -->
            <xsl:value-of
                xmlns:xalan="http://xml.apache.org/xalan";

select="xalan:nodeset($persons)/person[current()=personname]/@perid"
            />
        </perid>
        <persontype>1</persontype>
    </pubper>
</xsl:template>


</xsl:transform>
================================================

========================
pubs2.xml
================================================
<?xml version="1.0" encoding="UTF-8"?>
<pubs2>
<person perid="8000000001">
<personname>steve lawer</personname>
</person>
<person perid="8000000002">
<personname>rick maker</personname>
</person>
<publication pubid="0002">
<isbn>010101010101</isbn>
<title>How I Did That</title>
<publisher>Been There Press</publisher>
<copyright>2001</copyright>
</publication>
<pubper>
<pubid>0002</pubid>
<perid>8000000001</perid>
<persontype>1</persontype>
</pubper>
<publication pubid="0002">
<isbn>010101010202</isbn>
<title>Why I Went There</title>
<publisher>Done That Books</publisher>
<copyright>1998</copyright>
</publication>
<pubper>
<pubid>0002</pubid>
<perid>8000000002</perid>
<persontype>1</persontype>
</pubper>
<publication pubid="0003">
<isbn>010101020202</isbn>
<title>There and Back Again</title>
<publisher>Done That Books</publisher>
<copyright>2000</copyright>
</publication>
<pubper>
<pubid>0003</pubid>
<perid>8000000002</perid>
<persontype>1</persontype>
</pubper>
<publication pubid="0004">
<isbn>010102020202</isbn>
<title>Been There, Done That: The Kareem Abdul Jabbar Story</title>
<publisher>Stringbean Publications</publisher>
<copyright>2002</copyright>
</publication>
<pubper>
<pubid>0004</pubid>
<perid>8000000002</perid>
<persontype>1</persontype>
</pubper>
<pubper>
<pubid>0004</pubid>
<perid>8000000001</perid>
<persontype>1</persontype>
</pubper>
</pubs2>
================================================



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread