Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 4th, 2003, 06:30 AM
Registered User
 
Join Date: Aug 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSL numbering

I would like to realise a 'classical' numbering of a XML document except that node at the same level and with the same name must have the same number as illustrated in the following example:

<A> 1
   <B>xxx</B> 1.1
   <C>xxx</C> 1.2
   <C>xxx</C> 1.2
   <D>xxx</D> 1.3
<A>
<E> 2
   <F> 2.1
     <G>xxx</G> 2.1.1
     <G>xxx</G> 2.1.1
     <H>xxx</H> 2.1.2
   </F>
<E>

Any idea of how to do that using <xsl:number> or ... ?
Thank you for reply ?


 
Old August 5th, 2003, 05:08 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

It's impossible to achieve the goal using xsl:number instruction. Here is the stylesheet:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" method="xml"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
        <xsl:apply-templates>
            <xsl:with-param name="level" select="''"/>
        </xsl:apply-templates>
    </xsl:template>

    <xsl:template match="*">
        <xsl:param name="level"/>
        <xsl:variable name="curr-name" select="name()"/>
        <xsl:variable name="siblings-and-self" select="../*"/>
        <xsl:variable name="unique-element-names">
            <xsl:call-template name="unique-element-names">
                    <xsl:with-param name="node-set" select="$siblings-and-self"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:variable name="current-unique-element-pos" select="count($unique-element-names/*[name() = $curr-name]/preceding-sibling::*) + 1"/>
            <xsl:variable name="lvl">
                <xsl:choose>
                    <xsl:when test="not($level)">1</xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="concat($level, '.', $current-unique-element-pos)"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:variable>
            <xsl:attribute name="element-level">
                <xsl:value-of select="$lvl"/>
            </xsl:attribute>            
            <xsl:apply-templates>
                <xsl:with-param name="level" select="$lvl"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="node()[self::text() or self::comment() or self::processing-instruction()]">
        <xsl:copy-of select="."/>
    </xsl:template>

    <xsl:template name="unique-element-names">
        <xsl:param name="node-set"/>
        <xsl:choose>
            <xsl:when test="$node-set">
                <xsl:variable name="first-item" select="$node-set[1]"/>
                <xsl:element name="{name($first-item)}">
                    <xsl:value-of select="name($first-item)"/>
                </xsl:element>
                <xsl:call-template name="unique-element-names">
                    <xsl:with-param name="node-set" select="$node-set[name() != name($first-item)]"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise/>
        </xsl:choose>

    </xsl:template>
</xsl:stylesheet>
And the XML source, for example, is(it's now well-formed):
Code:
<?xml version="1.0" encoding="utf-8"?>
<Z>
   <A>
      <B>xxx</B>
      <C>
         <C1>xxx</C1>
           xxx
       </C>
      <C>
         <C2>xxx</C2> 
           xxx
       </C>
      <D>xxx</D>
   </A>
   <E>
      <F>
         <G>xxx</G>
         <G>xxx</G>
         <H>xxx</H>
      </F>
   </E>
</Z>
The stylesheet performs an identity transformation, except that it
appends "element-level" attribute (containing the needed numbering) to each element.

Regards,
Armen
 
Old August 5th, 2003, 05:14 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

The stylesheet is working for Saxon 6.5.2, since it implicitly converts
an RTF to nodeset(the "version" attribute has the value "1.1").
You have to use vendor's node-set(RTF) function in the places when such a conversion is needed.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Numbering - For each Navy1991_1 XSLT 3 July 2nd, 2008 07:56 AM
Numbering in XSLT Angel1221 XSLT 1 June 10th, 2008 05:07 PM
Page numbering athanatos XSLT 4 May 13th, 2005 06:12 PM
Recordset numbering drumph Classic ASP Components 3 May 20th, 2004 06:54 PM
numbering harag XSLT 2 November 6th, 2003 06:04 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.