xsl-list
[Top] [All Lists]

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

2003-02-06 18:48:24
Roger,
Are you some kind of XML jini!
thank you very much. I am entangled in this XSL
programming since two weeks and you solved it like in
a blink.

But there are some serious issues here:

With your approach of generating perids before the
actual seperation of publication, person, pubper
elements, I feel it would not work when I have 500,000
author elements. I have an 130MB XML sheet which
contains almost 350,000 publication elements
I know you did not knew about this. Can you please
comment on this. 

Do you think I am right on this? Please correct me.

Now there are also editors along with authors. Authors
can be editors also for some publication. means
<author>Steve Lawyer</author> for pub1 can be
<editor>Steve Lawyer</editor> for pub2. but we want
to have single person element generated. While in
<pubper> we have <persontype> (1 for author, 2 for
editors) hence in our example for pub1, it shoud be
<persontype>1</persontype> and for pub2 it should be
<persontype>2</persontype> 
how can we store that information with your code then?
we have to get unique person names

ALso

You dont have a clue How much your code has helped
me!!! I have been working on this since two weeks...
thanks, roger. thank you

Jinesh

--- Roger Glover <glover_roger(_at_)yahoo(_dot_)com> wrote:
Roger Glover [mailto:glover_roger(_at_)yahoo(_dot_)com] 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>

=== message truncated ===


=====
-----------------------------------------------------------------
Jinesh Varia
Graduate Student, Information Systems
Pennsylvania State University
Email: jinesh(_at_)psu(_dot_)edu
-----------------------------------------------------------------
'Self is the author of its actions.'

__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com

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