[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Insert an new Element on a attribute value change / te

Subject: Insert an new Element on a attribute value change / test for nonexistence of an attribute
From: "Stephan H. Wissel" <stephan@xxxxxxxxxx>
Date: Wed, 14 Jul 2004 23:01:44 +0800
xsl change attribute value
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

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.