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 March 9th, 2005, 04:29 AM
Registered User
 
Join Date: Mar 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Combining adjacent elements with the same name

My XML-data has the follwing structure:

<Rubric>Some text
     <Reference>A1</Reference>
     . More text
     <Reference>B1</Reference>
     <Reference>B2</Reference>
     . This is the last text.
</Rubric>

From this I want to generate text, where the two adjacent references are
combined in brackets and separated by a coma.

This is the output I would like to get:

      Some text (A1). More text (B1, B2). This is the last text.

I tried to make use of the name of the preceding-sibling and the following-sibling, but it does not work. This is one of the variants I tried:


<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" version="1.0"/>
    <xsl:template match="/">
        <xsl:apply-templates select="*"/>
    </xsl:template>
    <xsl:template match="Reference">
        <xsl:choose>
            <xsl:when test="preceding-sibling[name()=Reference]">
                <xsl:text> ,</xsl:text>
                <xsl:value-of select="."/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:text> (</xsl:text>
                <xsl:value-of select="."/>
            </xsl:otherwise>
        </xsl:choose>
        <xsl:if test="following-sibling[name()!=Reference]">
            <xsl:text>)</xsl:text>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>


This is the generated output:
 Some text
      (A1
     . More text
      (B1 (B2
     . This is the last text.
 
Old March 9th, 2005, 05:17 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

This is one of the variants I tried:

following-sibling[name()!=Reference]

It looks to me as if you're trying to learn the language by trial and error. Guesswork will never get you there.

An axis-name in XPath is always followed by "::" and a NodeTest. "following-sibling" without "::" is interpreted as an element name, so this test is looking for all child elements named following-sibling whose name is the same as the content of the grandchild element named Reference. In other words, your guess is a pretty wild one.

You want test="following-sibling::*[1][self::Reference]"

That is:

- select all the following sibling elements (following-sibling::*)
- choose the first one ([1])
- select this element if its name is Reference ([self::Reference])
- return true if the resulting node-set is non-empty



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 10th, 2005, 03:49 AM
Registered User
 
Join Date: Mar 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for your help Michael.

Your hint soved my problem. I definitely have to investigate more time on getting accustomed to XPath.

Thanks again Susanne :)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Xsl: strip-space elements; and 'group-adjacent' ROCXY XSLT 6 July 15th, 2010 08:59 AM
Combining string paths elements into another XML eXhumed XSLT 2 March 19th, 2008 11:06 AM
Finding Adjacent Empty Cell keithd Excel VBA 7 July 19th, 2006 06:20 AM
Using - "group-adjacent" ROCXY XSLT 4 January 4th, 2006 11:09 AM
Using Two "group-adjacent" ROCXY XSLT 0 January 4th, 2006 08:29 AM





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