OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

 


 

   Re: [xml-dev] XSLT way

[ Lists Home | Date Index | Thread Index ]

At 2003-07-11 09:24 +0400, Andrey Gorjunov wrote:
>I have some problem during XSLT transformation with variable gain -

XSLT and XPath questions would be better posted to the following list:

   http://www.mulberrytech.com/xsl/xsl-list

There are a number of subscribers who would enthusiastically respond to 
such questions.

There is also an *excellent* FAQ at:

   http://www.dpawson.co.uk

>For instance I have some XML file
>...
>empty cells are not in the structure, also the full number of cells 
>(filled and empty) is known
>(for that example the full number of cells is 9)
>Is it possible to apply XSLT template to change XML file to the structure 
>like ...

Yes, but it isn't pretty because of the absent cells.  It would be very 
straightforward if the cells that were present each had the index attribute.

To get your desired results, you have to "walk" the document tree keeping 
track of where you are and checking if that is where you are supposed to 
be, and then special casing when you run out of nodes to walk.

A working example is below using XSLT 1.0, and I've whittled it down to 
ensure that in only one place do you handle a cell that is present and only 
one place do you handle a cell that is absent.

I hope this helps.

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

t:\ftemp>type gorjunov.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<Table xmlns:ss="other">
    <Row>
     <Cell>1</Cell>
     <Cell ss:Index="3">3</Cell>
     <Cell>4</Cell>
     <Cell ss:Index="6">5</Cell>
     <Cell ss:Index="8">6</Cell>
    </Row>
    <Row>
     <Cell ss:Index="2">2</Cell>
     <Cell ss:Index="4">7</Cell>
     <Cell ss:Index="8">8</Cell>
     <Cell>9</Cell>
    </Row>
</Table>
t:\ftemp>type gorjunov.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                 xmlns:ss="other" exclude-result-prefixes="ss"
                 version="1.0">

<xsl:output indent="yes"/>

<xsl:template match="Row">
   <Row>
     <xsl:apply-templates select="Cell[1]"/>
   </Row>
</xsl:template>

<xsl:template match="Cell" name="do-cell">
   <xsl:param name="cell" select="1"/>
   <xsl:param name="finishing" select="false()"/>
   <xsl:if test="$cell &lt;= 9">
     <xsl:choose>
       <xsl:when test="not( $finishing ) and
                       ( not( @ss:Index ) or @ss:Index=$cell )">
         <Cell><xsl:apply-templates/></Cell>
         <xsl:choose>
           <xsl:when test="following-sibling::Cell">
             <xsl:apply-templates select="following-sibling::Cell[1]">
               <xsl:with-param name="cell" select="$cell + 1"/>
             </xsl:apply-templates>
           </xsl:when>
           <xsl:otherwise>
             <xsl:call-template name="do-cell">
               <xsl:with-param name="cell" select="$cell + 1"/>
               <xsl:with-param name="finishing" select="true()"/>
             </xsl:call-template>
           </xsl:otherwise>
         </xsl:choose>
       </xsl:when>
       <xsl:otherwise>
         <Cell/>
         <xsl:call-template name="do-cell">
           <xsl:with-param name="cell" select="$cell + 1"/>
         </xsl:call-template>
       </xsl:otherwise>
     </xsl:choose>
   </xsl:if>
</xsl:template>

</xsl:stylesheet>

t:\ftemp>saxon -o gorjunov.out gorjunov.xml gorjunov.xsl

t:\ftemp>type gorjunov.out
<?xml version="1.0" encoding="utf-8"?>

<Row>
    <Cell>1</Cell>
    <Cell/>
    <Cell>3</Cell>
    <Cell>4</Cell>
    <Cell/>
    <Cell>5</Cell>
    <Cell/>
    <Cell>6</Cell>
    <Cell/>
</Row>

<Row>
    <Cell/>
    <Cell>2</Cell>
    <Cell/>
    <Cell>7</Cell>
    <Cell/>
    <Cell/>
    <Cell/>
    <Cell>8</Cell>
    <Cell>9</Cell>
</Row>


--
Upcoming hands-on courses: in-house corporate training available;
North America public:  XSL-FO Aug 4,2003; XSLT/XPath Aug 12, 2003

G. Ken Holman                mailto:gkholman@CraneSoftwrights.com
Crane Softwrights Ltd.         http://www.CraneSoftwrights.com/x/
Box 266, Kars, Ontario CANADA K0A-2E0   +1(613)489-0999 (F:-0995)
ISBN 0-13-065196-6                      Definitive XSLT and XPath
ISBN 0-13-140374-5                              Definitive XSL-FO
ISBN 1-894049-08-X  Practical Transformation Using XSLT and XPath
ISBN 1-894049-11-X              Practical Formatting Using XSL-FO
Member of the XML Guild of Practitioners:    http://XMLGuild.info
Male Breast Cancer Awareness http://www.CraneSoftwrights.com/x/bc


  • References:
    • XSLT way
      • From: "Andrey Gorjunov" <Gor100GG@sual-holding.ru>



 

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS