Re: [xsl] XML/XSLT Problem

Subject: Re: [xsl] XML/XSLT Problem
From: "Alex Genis" <agenis@xxxxxxxx>
Date: Tue, 30 Jul 2002 10:48:59 -0400
Hi, G.Ken.
Thanks a lot for your help. Your XSLT works perfectly.
But this is not exactly what I was looking for (maybe because I've
described my task not clear enough).

1. What if an input XML Document contains a few groups (each of which
contains  "PP", "IP" and "PR" elements
    connected between each other by the same condition: (PP/IPI/@id =
IP/@id) and (IP/@id-1 = PR/@id))?
    When I tested your Stylesheet I've created such input XML Document and
result XML looked like :
    <abc>
       <PP id="..." id-1="...">
           <IPI id="..." id-2="...">
                <p1>....</p1>
           </IPI>
           <IPI id="..." id-2="...">
                <p1>....</p1>
           </IPI>
       </PP>
       <PP id="..." id-1="...">
           <IPI id="..." id-2="...">
                <p1>....</p1>
           </IPI>
           <IPI id="..." id-2="...">
                <p1>....</p1>
           </IPI>
       </PP>
       <IP id="..." id-1="...">
          <i1>...</i1>
       </IP>
       <IP id="..." id-1="...">
          <i1>...</i1>
       </IP>
       <PR id="...">
          <r1>...</r1>
       </PR>
       <PR id="...">
          <r1>...</r1>
       </PR>
   </abc>

My question is: how can I combine them like:

<abc>
    <Group>
        <PP id="..." id-1="...">
           <IPI id="..." id-2="...">
                <p1>....</p1>
           </IPI>
           <IPI id="..." id-2="...">
                <p1>....</p1>
           </IPI>
        </PP>
        <IP id="..." id-1="...">
              <i1>...</i1>
        </IP>
        <PR id="...">
             <r1>...</r1>
       </PR>
    </Group>
    <Group>
        <PP id="..." id-1="...">
           <IPI id="..." id-2="...">
                <p1>....</p1>
           </IPI>
           <IPI id="..." id-2="...">
                <p1>....</p1>
           </IPI>
        </PP>
        <IP id="..." id-1="...">
              <i1>...</i1>
        </IP>
        <PR id="...">
             <r1>...</r1>
       </PR>
     </Group>
  </abc>

  where each group contains only elements connected between each other by
condition (above).

Because my task is :
    To create for each such group set of output elements.
    Each of this output element must contain children value of which should
be generated
         from an some attributes and elements of the appropriate group.

Therefore I want those sets of "PP", "IP", and "PR" to be grouped -
just to have a possibility reading this XML to use <xsl:for-each select
="Group">.....</xsl:for-each>.

2. And by the way does it mean that the task (above) must contain two
steps:
    a) XSLT-1  which creates  XML containing those groups;
    b) XSLT-2  which reads this XML and create output elements for each
group.
   Or it's possible to perform this in one shot ?

Thank you very much for your help again.
---------------------- Forwarded by Alex Genis/DTCC on 07/30/2002 10:14 AM
---------------------------


"G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>@lists.mulberrytech.com on
07/29/2002 04:24:57 PM

Please respond to xsl-list@xxxxxxxxxxxxxxxxxxxxxx

Sent by:    owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx


To:    xsl-list@xxxxxxxxxxxxxxxxxxxxxx
cc:
Subject:    Re: [xsl] XML/XSLT Problem


At 2002-07-29 15:30 -0400, Alex Genis wrote:
>I have input XML Document like this:
>...
>I need to create XSLT Stylesheet which retrieves from this XML only those
>elements "PP", "IP" and "PR" where:
>(PP/IPI/@id = IP/@id) and (IP/@id-1 = PR/@id).
>...
>If beeing already within <xsl:template match="abc">
>I use <xsl:apply-templates select="PP|IP|PR"> - I can not refer to the
>attributes of this elements ("id" and "id-1") because this attribute names
>are not unique.

I'm not sure what you are saying in the last sentence I quoted above.

>Can somebody give me a hint  how can I solve this problem?

Attached below is a stylesheet that matches the limited data example you
have, and I think it expresses what you want from your English
description.  The IP element is what is common between the IPI element and
the PR element, so I used that as the focus of the stylesheet.  My use of
variables is to improve speed by reducing the accesses to the source node
tree.

The key to the solution is the node-set comparison in XSLT will check each
node in a node-set in a single test.

I hope this helps.

................. Ken


T:\ftemp>type alex.xml
    <ABC>
   <efg>fgh</efg>
   <hij>sdf</hij>
   <abc>
     <PP id="111" id-1="222" >
       <IPI id="333" id-2="444" >
        <p1>p1p1p1</p1>
       </IPI>
       <IPI id="sss" id-2="666" >
        <p1>p2p2p2</p1>
       </IPI>
     </PP>
     <PP id="777" id-1="888" >
       <IPI id="123" id-2="234" >
        <p1>p3p3p3</p1>
       </IPI>
       <IPI id="456" id-2="567" >
        <p1>p4p4p4</p1>
       </IPI>
     </PP>
     <IP id="rrr" id-1="aaa">
       <i1>i1i1i1</i1>
     </IP>
     <IP id="sss" id-1="bbb">
       <i1>i2i2i2</i1>
     </IP>
     <IP id="ttt" id-1="ccc">
       <i1>i3i3i3</i1>
     </IP>
     <IP id="uuu" id-1="ddd">
       <i1>i4i4i4</i1>
     </IP>
     <PR id="xxx">
       <r1>r1r1r1</r1>
     </PR>
     <PR id="bbb">
       <r1>r2r2r2</r1>
     </PR>
     <PR id="zzz">
       <r1>r3r3r3</r1>
     </PR>
   </abc>
</ABC>


T:\ftemp>type alex.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                 version="1.0">

<xsl:output indent="yes"/>

<xsl:template match="/ABC">
   <xsl:apply-templates select="abc"/>
</xsl:template>

<xsl:template match="abc">
   <abc>
     <xsl:variable name="IPI-ids" select="PP/IPI/@id"/>
     <xsl:variable name="PR-ids" select="PR/@id"/>
     <xsl:variable name="common-IPs" select="IP[@id=$IPI-ids and
                                                @id-1=$PR-ids]"/>

     <xsl:copy-of select="PP[IPI/@id=$common-IPs/@id]"/>
     <xsl:copy-of select="$common-IPs"/>
     <xsl:copy-of select="PR[@id=$common-IPs/@id-1]"/>

   </abc>
</xsl:template>

</xsl:stylesheet>

T:\ftemp>xt alex.xml alex.xsl alex.out

T:\ftemp>type alex.out
<?xml version="1.0" encoding="utf-8"?>
<abc>
<PP id="111" id-1="222">
       <IPI id="333" id-2="444">
        <p1>p1p1p1</p1>
       </IPI>
       <IPI id="sss" id-2="666">
        <p1>p2p2p2</p1>
       </IPI>
     </PP>
<IP id="sss" id-1="bbb">
       <i1>i2i2i2</i1>
     </IP>
<PR id="bbb">
       <r1>r2r2r2</r1>
     </PR>
</abc>

T:\ftemp>rem Done!


--
Upcoming hands-on in-depth 3-days XSLT/XPath and/or 2-days XSL-FO:
-                               North America:  Sep 30-Oct  4,2002
-                               Japan:          Oct  7-Oct 11,2002

G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0  +1(613)489-0999 (Fax:-0995)
ISBN 0-13-065196-6                       Definitive XSLT and XPath
ISBN 1-894049-08-X   Practical Transformation Using XSLT and XPath
ISBN 1-894049-07-1                Practical Formatting Using XSLFO
XSL/XML/DSSSL/SGML/OmniMark services, books (electronic, printed),
articles, training (instructor-live,Internet-live,web/CD,licensed)
Next public training:           2002-08-05,26,27,09-30,10-03,07,10


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





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


Current Thread