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 August 21st, 2012, 12:39 PM
Registered User
 
Join Date: Jan 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default xpath preceding-sibling problem

Hello,

i'm trying to produce this "get the fisrt preceding sibling (looking backwards) that meets the condition"

e.g.

XML

Code:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type='text/xsl' href='test.xsl'?>
<chars>
	<char type="letter">A</char>
	<char type="letter">B</char>
	<char type="letter">C</char>
	<char type="letter">D</char>
	<char type="digit">1</char>
	<char type="digit">2</char>
	<char type="digit">3</char>
	<char type="digit">4</char>
	<char type="">5</char>
</chars>
XSL

if an element has @type='', then output ( + @type of the first backwards preceding sibling that has @type!='' + )


desired output: CHARACTERS: letter A B C D digit 1 2 3 4 (digit) 5

result: CHARACTERS: letter A B C D digit 1 2 3 4 (letter) 5

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="chars" name="chars">
        <xsl:text>CHARACTERS: </xsl:text>
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="char" name="char">
        <xsl:variable name="previous_charType">
            <xsl:value-of select="./preceding-sibling::*[1]/@type"/>
        </xsl:variable>
        <xsl:variable name="x">
            <xsl:value-of select="./@type"/>
        </xsl:variable>
        <xsl:if test="$previous_charType != $x ">
            <xsl:text> </xsl:text>
            <b>
                <xsl:value-of select="$x"/>
            </b>
        </xsl:if>
        <xsl:if test="./@type=''">
            <xsl:text>(</xsl:text>
            <xsl:value-of select="preceding-sibling::*[@type!='']/@type"/>
            <xsl:text>) </xsl:text>
        </xsl:if>
        <xsl:text> </xsl:text>
        <xsl:value-of select="."/>
        <xsl:text> </xsl:text>
    </xsl:template>
</xsl:stylesheet>
Any ideas? the <xsl:value-of select="preceding-sibling::*[@type!='']/@type"/> doen't work...

Thank You
 
Old August 21st, 2012, 01:00 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

I think the main issue is to use a positional predicate [1] e.g.
Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html"/>
    <xsl:template match="chars">
        <xsl:text>CHARACTERS: </xsl:text>
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="char">
        <xsl:variable name="previous_charType" select="preceding-sibling::*[1]/@type"/>
        <xsl:variable name="x" select="./@type"/>
        <xsl:if test="$previous_charType != $x ">
            <xsl:text> </xsl:text>
            <b>
                <xsl:value-of select="$x"/>
            </b>
        </xsl:if>
        <xsl:if test="./@type=''">
            <xsl:text>(</xsl:text>
            <xsl:value-of select="preceding-sibling::*[@type!=''][1]/@type"/>
            <xsl:text>) </xsl:text>
        </xsl:if>
        <xsl:text> </xsl:text>
        <xsl:value-of select="."/>
        <xsl:text> </xsl:text>
    </xsl:template>
</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old August 21st, 2012, 01:16 PM
Registered User
 
Join Date: Jan 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Martin Honnen View Post
I think the main issue is to use a positional predicate [1] e.g.
Thanks for your reply but that won't work either. The above example could actually be

Code:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type='text/xsl' href='test.xsl'?>
<chars>
	<char type="letter">A</char>
	<char type="letter">B</char>
	<char type="letter">C</char>
	<char type="letter">D</char>
	<char type="digit">1</char>
	<char type="digit">2</char>
	<char type="digit">3</char>
	<char type="">4</char>
	<char type="">5</char>
</chars>
desired output: CHARACTERS: letter A B C D digit 1 2 3 (digit) 4 (digit) 5

result: CHARACTERS: letter A B C D digit 1 2 3 (digit) 4 () 5
or

Code:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type='text/xsl' href='test.xsl'?>
<chars>
	<char type="letter">A</char>
	<char type="letter">B</char>
	<char type="letter">C</char>
	<char type="letter">D</char>
	<char type="digit">1</char>
	<char type="">2</char>
	<char type="">3</char>
	<char type="digit">4</char>
	<char type="digit">5</char>
</chars>
desired output: CHARACTERS: letter A B C D digit 1 (digit) 2 (digit) 3 4 5

result: CHARACTERS: letter A B C D digit 1 (digit) 2 () 3 4 5

predicate [1] works only for the first case and it's not flexible...





Similar Threads
Thread Thread Starter Forum Replies Last Post
Want to Pull multiple following-sibling data to preceding-sibling element sameer_kadam XSLT 4 May 9th, 2009 08:07 AM
Preceding Sibling mphare XSLT 3 March 4th, 2009 11:04 AM
Using preceding-sibling mcanne98 Infopath 0 September 11th, 2008 11:09 PM
preceding-sibling jonesyp XSLT 2 November 22nd, 2005 12:29 PM
preceding-sibling behaviour andrewcday XSLT 6 December 3rd, 2004 03:24 PM





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