Re: [xsl] XML/XSLT Problem

Subject: Re: [xsl] XML/XSLT Problem
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Mon, 29 Jul 2002 16:24:57 -0400
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



Current Thread