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 July 7th, 2004, 04:05 AM
Authorized User
 
Join Date: Jul 2004
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to NotesSensei Send a message via Yahoo to NotesSensei
Default Group XML by Attribute values?

Hi there,
can someone point me to a solution? It is about adding group level to a flat XML using XLST:
I have:
<MyQuotes>
<Quote newpage="true">If u think education is expensive, try ignorance</Quote>
<Quote>Just Do it</Quote>
<Quote>Vorsprung durch Technik</Quote>
<Quote newpage="true">Orandum est ut sit mens sana in corpore sano</Quote>
<Quote>It is all about choices and consequences</Quote>
<Quote newpage="true">What a day to die</Quote>
<Quote newpage="true">Quidquid agis prudenter agas et respice finem</Quote>
<Quote>Wir sind das Volk!</Quote>
<Quote>Dream the Butterfly</Quote>
<MyQuotes>

I want:
<MyQuotes>
<page>
<Quote>If u think education is expensive, try ignorance</Quote>
<Quote>Just Do it</Quote>
<Quote>Vorsprung durch Technik</Quote>
<Quote>Orandum est ut sit mens sana in corpore sano</Quote>
</page>
<page>
<Quote>It is all about choices and consequences</Quote>
</page>
<page>
<Quote>What a day to die</Quote>
</page>
<page>
<Quote>Quidquid agis prudenter agas et respice finem</Quote>
<Quote>Wir sind das Volk!</Quote>
<Quote>Dream the Butterfly</Quote>
</page>
<MyQuotes>

How do I get there?
;) stw
P.S.: Ordered the XLST book, so I can ask smarter questions in future!

If you think education is expensive - try ignorance!
__________________
If you think education is expensive - try ignorance!
 
Old July 7th, 2004, 04:29 AM
Authorized User
 
Join Date: Jul 2004
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Do you mean you want the following:

<MyQuotes>
<page>
<Quote>If u think education is expensive, try ignorance</Quote>
<Quote>Just Do it</Quote>
<Quote>Vorsprung durch Technik</Quote>
</page>
<page>
<Quote>Orandum est ut sit mens sana in corpore sano</Quote>
<Quote>It is all about choices and consequences</Quote>
</page>
<page>

..
..

as <page>Orandum .... has a newpage attribute ??

 
Old July 7th, 2004, 04:50 AM
Authorized User
 
Join Date: Jul 2004
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to NotesSensei Send a message via Yahoo to NotesSensei
Default

Exactly. What I need to to is to wrap all quotes into a page element when a newpage=true occurs. The verbal rule:
When you hit a "newpage=true" look for following siblings until the end or the next "newpage=true" (not included) and group them into a <page> element. I can expect that the first element does have a newpage=true... however if a solution wouldn't depend on it would be more robust.
;) stw

If you think education is expensive - try ignorance!
 
Old July 14th, 2004, 10:39 AM
Authorized User
 
Join Date: Jul 2004
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The following code works, but you would have to omit the first instance of newpage="true" to avoid starting with an empty 'page' element.

Obviously I've hardcoded the output tag names, but you could use <xsl:value-of select="local-name()"> to produce a more generic solution.

<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output
    method="xml"
    omit-xml-declaration="no"/>

<xsl:template match="/MyQuotes">
<MyQuotes>
    <page>
        <xsl:for-each select="Quote">
            <xsl:choose>
              <xsl:when test="@newpage='true'">
                <xsl:text disable-output-escaping="yes">&lt;/page>&lt;page></xsl:text>
                <Quote><xsl:value-of select="."/></Quote>
              </xsl:when>
              <xsl:otherwise>
                <xsl:copy-of select="." />
              </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
    </page>
</MyQuotes>
</xsl:template>

</xsl:stylesheet>

holdmykidney
 
Old July 14th, 2004, 11:53 AM
Authorized User
 
Join Date: Jul 2004
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to NotesSensei Send a message via Yahoo to NotesSensei
Default

Hi HoldMyKidney,
thx for taking the time to look into my problem. I haven't though using output escaping (feels a bit like cheating). However thx for the solution. I did some reseach just today and found the generic XML solution using generate key:
<?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="MyQuotes">
        <MyQuotes>
            <xsl:apply-templates select="Quote[@newpage='true']"/>
        </MyQuotes>
    </xsl:template>
    <xsl:template match="Quote[@newpage='true']">
        <page>
            <Quote>
                <xsl:value-of select="."/>
            </Quote>

            <xsl:variable name="nextPage" select="generate-id(following-sibling::*[@newpage='true'][1])"/>
            <xsl:apply-templates select="following-sibling::*[generate-id(following-sibling::*[@newpage='true'][1])=$nextPage]"/>
        </page>
    </xsl:template>
    <xsl:template match="Quote[not(@newpage)]">
        <Quote>
            <xsl:value-of select="."/>
        </Quote>
    </xsl:template>
    <xsl:template match="*"/>
</xsl:stylesheet>

David Carlisle and that pages helped: http://www.jenitennison.com/xslt/gro...muenchian.html
;) stw

If you think education is expensive - try ignorance!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Extracting a number range from tag attribute group maikm XSLT 6 August 25th, 2008 04:54 PM
HOW TO: xlate attribute values? Philibuster XSLT 4 August 24th, 2006 12:17 PM
Access to attribute values from class of attribute jacob C# 1 October 28th, 2005 01:11 PM
how to get values depending on the attribute vidhya XSLT 1 July 8th, 2005 03:55 AM
different values with the same attribute name srini XSLT 0 January 21st, 2004 05:21 AM





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