This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Build unique nodeset when Key can't be used?


At 01/10/17 13:45 +0100, J S Publications wrote:
>I have a <paragraph> element that can contain one or more <link> elements. 
>Each <link> has a @type and a @id attributes. When @type='Figure' the link 
>is to an image. I want to output each <paragraph>, followed by a list of 
>pictures referenced in the <paragraph>.
>...
>How can I get a list with just one entry for each image? I can't see how 
>to you key, because key is a root-level element and so can't work with 
>just the current <paragraph>. I have tried for-each but can't come up with 
>a select that returns a 'unique' node-set.

My earlier note today shows how this can be done without keys but with 
local variables.

I've used XT (which doesn't support key()) and I've included an example of 
how the members could be counted.

I hope you find the solution below useful.

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


T:\chris>type chris.xml
<?xml version="1.0" encoding="utf-8"?>
<book>
   <para>
     <link type="Figure" id="fig1"/>
     <link type="Figure" id="fig2"/>
     <link type="Figure" id="fig1"/>
   </para>
   <para>
     <link type="Figure" id="fig1"/>
     <link type="Figure" id="fig3"/>
     <link type="Figure" id="fig5"/>
   </para>
   <para>
     <link type="Figure" id="fig4"/>
     <link type="Figure" id="fig2"/>
     <link type="Figure" id="fig2"/>
   </para>
</book>
T:\chris>type chris.xsl
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                 xmlns="http://www.w3.org/1999/XSL/Format";
                 version="1.0">

<xsl:output method="text"/>

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

<xsl:template match="para">
   <xsl:text/>Paragraph summary:
<xsl:text/>
   <xsl:variable name="figs" select="link[@type='Figure']"/>
   <xsl:for-each select="$figs">
     <xsl:sort select="@id"/>
     <xsl:variable name="figure-id" select="@id"/>
     <xsl:if test="generate-id(.)=
                   generate-id($figs[@id=$figure-id])">
       <xsl:text>Figure id: </xsl:text>
       <xsl:value-of select="$figure-id"/>
       <xsl:text> - </xsl:text>
       <xsl:value-of select="count($figs[@id=$figure-id])"/>
       <xsl:text> time</xsl:text>
       <xsl:if test="count($figs[@id=$figure-id])>1">
         <xsl:text>s</xsl:text>
       </xsl:if>
       <xsl:text>
</xsl:text>
     </xsl:if>
   </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

T:\chris>xt chris.xml chris.xsl
Paragraph summary:
Figure id: fig1 - 2 times
Figure id: fig2 - 1 time
Paragraph summary:
Figure id: fig1 - 1 time
Figure id: fig3 - 1 time
Figure id: fig5 - 1 time
Paragraph summary:
Figure id: fig2 - 2 times
Figure id: fig4 - 1 time



--
G. Ken Holman                      mailto:gkholman@CraneSoftwrights.com
Crane Softwrights Ltd.               http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0     +1(613)489-0999   (Fax:-0995)
Web site:     XSL/XML/DSSSL/SGML/OmniMark services, training, products.
Book:  Practical Transformation Using XSLT and XPath ISBN 1-894049-06-3
Article: What is XSLT? http://www.xml.com/pub/2000/08/holman/index.html
Next public training (instructor-live, Internet-live, and web-based):
-2001-10-22,11-01,11-02,11-05,11-19,11-21,12-03,12-05,12-09,12-10,12-19


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]