Subject: [xsl] 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 |
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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Newbie learning question, Nicolas Mailhot | Thread | Re: [xsl] Insert an new Element on , David Carlisle |
Re: [xsl] Newbie learning question, Stephan H. Wissel | Date | Re: [xsl] Insert an new Element on , David Carlisle |
Month |