Altova Mailing List Archives


[xsl] Insert an new Element on a attribute value change / test for nonexistence of an attribute

From: "Stephan H. Wissel" <stephan@---------->
To:
Date: 7/15/2004 7:02:00 AM
Hi there,

I'm stuck with a transformation problem that I can't seem to solve. I need 
to insert a hierarchy level based on an attribute value I have 
(simplified):
<?xml version="1.0" encoding="UTF-8"?>
<Book>
        <Pages>
                <page picture="pic1.jpg" newchapter="true" Title="One upon 
the time" StartPage="5">Once upon the time there was a king</page>
                <page picture="pic2.jpg">Bla Bla Bla</page>
                <page picture="pic2.jpg">Bla Bla Bla</page>
                <page picture="pic2.jpg">Bla Bla Bla</page>
                <page picture="pic1.jpg" newchapter="true" Title="The Test
" StartPage="11">Bla Bla Bla</page>
                <page picture="pic2.jpg">Bla Bla Bla</page>
                <page picture="pic1.jpg" newchapter="true" Title="The 
Quest" StartPage="36">Bla Bla Bla</page>
                <page picture="pic1.jpg" newchapter="true" Title="The 
Happy end" StartPage="37">Bla Bla Bla</page>
                <page picture="pic2.jpg">Bla Bla Bla</page>
                <page picture="pic2.jpg">Bla Bla Bla</page>
                <page picture="pic2.jpg">Bla Bla Bla</page>
                <page picture="pic2.jpg">Bla Bla Bla</page>
                <page picture="pic2.jpg">Bla Bla Bla</page>
        </Pages>
</Book>

I want:
<?xml version="1.0" encoding="UTF-8"?>
<Book>
        <Chapters>
                <Chapter Title="One upon the time" StartPage="5">
                        <page picture="pic1.jpg">Once upon the time there 
was a king</page>
                        <page picture="pic2.jpg">Bla Bla Bla</page>
                        <page picture="pic2.jpg">Bla Bla Bla</page>
                        <page picture="pic2.jpg">Bla Bla Bla</page>
                </Chapter>
                <Chapter Title="The Test" StartPage="11">
                        <page picture="pic1.jpg">Bla Bla Bla</page>
                        <page picture="pic2.jpg">Bla Bla Bla</page>
                </Chapter>
                <Chapter Title="The Quest" StartPage="36">
                        <page picture="pic1.jpg">Bla Bla Bla</page>
                </Chapter>
                <Chapter Title="The Happy end" StartPage="37">
                        <page picture="pic1.jpg">Bla Bla Bla</page>
                        <page picture="pic2.jpg">Bla Bla Bla</page>
                        <page picture="pic2.jpg">Bla Bla Bla</page>
                        <page picture="pic2.jpg">Bla Bla Bla</page>
                        <page picture="pic2.jpg">Bla Bla Bla</page>
                        <page picture="pic2.jpg">Bla Bla Bla</page>
                </Chapter>
        </Chapters>
</Book>


I tried:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="
http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="
yes"/>
        <xsl:template match="/">
                <xsl:apply-templates/>
        </xsl:template>
        <xsl:template match="Book">
                <Book>
                        <xsl:apply-templates/>
                </Book>
        </xsl:template>
        <xsl:template match="Pages">
                <Chapters>
                        <xsl:apply-templates select="
page[@newchapter='true']"/>
                </Chapters>
        </xsl:template>
        <xsl:template match="page[@newchapter='true']">
                <xsl:element name="Chapter">
                        <xsl:attribute name="Title"><xsl:value-of select="
@Title"/></xsl:attribute>
                        <xsl:attribute name="StartPage"><xsl:value-of 
select="@StartPage"/></xsl:attribute>
                        <xsl:element name="page">
                                <xsl:attribute name="picture"><
xsl:value-of select="@picture"/></xsl:attribute>
                                <xsl:value-of select="."/>
                        </xsl:element>
                        <!-- Here I got stuck: how to get all following 
siblings until there is one with a startpage how to test for non existance 
of an attribute? and how to get until the next similar-->
                        <xsl:apply-templates select="
following-sibling::page[@newchapter='']"/>
                </xsl:element>
        </xsl:template>
        <xsl:template match="page">
                <xsl:element name="page">
                        <xsl:attribute name="picture"><xsl:value-of select
="@picture"/></xsl:attribute>
                        <xsl:value-of select="."/>
                </xsl:element>
        </xsl:template>
        <xsl:template match="*"/>
</xsl:stylesheet>


There are 2 problems that I face: How to test for the non-existance of an 
attribute. And how to select a subset of the following siblings. 
The rule: all following siblings until you find one with an existing 
attribute newchapter='true'.

Any hint what I can do?
Thx
;-) stw

Disclaimer

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.