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 4th, 2006, 02:22 PM
Authorized User
 
Join Date: Jul 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default Duplicate nodes get the value from first node

Hi,

 I need to get the value from the first node and ignore the values from the next node.
I need to get just book1 and ignore book2, book3

<root>
<list>
 <item>book1</item>
 <item>book2</item>
  <item>book3</item>
</list>
</root>

I don't have a sample code to show you b'coz I don't have any ideas how to proceed on this... I tried to use preceding sibling, precedingnodes but of no use.....



 
Old August 4th, 2006, 02:34 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You're trying to do grouping: read all about it at http://www.jenitennison.com/xslt/grouping

It's much easier in XSLT 2.0

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old August 4th, 2006, 03:57 PM
Authorized User
 
Join Date: Jul 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried reading about grouping in xslt 2.0 as I'm using xslt 2.0;
I tried experimenting with the examples provided & some don't work (I'm using xslt 2.0 processor).....

None of them talk about grouping of the scenario I have......

Presently stuck with this.... and need some help!!!!!!!!

This is the xml file I have .I need to do work with this file in 2 ways.

1. To get only the first value and ignore the rest of the values.
get only BOOK1 AND BOOK3 from respective records.

2. To display the values together with a space between them; concatenate BOOK1 AND BOOK2 and display BOOK1 BOOK2 together which belong to the same record.

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <record>
        <item id="100">
            <book code="a">BOOK1</book>
        </item>
        <item id="100">
            <book code="a">BOOK2</book>
        </item>
    </record>
    <record>
        <item id="100">
            <book code="a">BOOK3</book>
        </item>
        <item id="100">
            <book code="a">BOOK4</book>
        </item>
    </record>
</root>


 
Old August 4th, 2006, 04:24 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Sorry I misunderstood your requirement. You can get the first book in each record by writing (with a <record> as the context node):

select="item[1]/book"

You can display "BOOK1 BOOK2" (again with a <record> as the context node):

<xsl:value-of select="item/book"/>

For the latter make sure your stylesheet specifies version="2.0" otherwise you will get 1.0 mode which outputs only the first book.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old August 7th, 2006, 12:01 PM
Authorized User
 
Join Date: Jul 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The solution you gave works only if we have the same id for all items.What if I want to get the values form other items too...
Using the below xslt I get COMPUTERS:P4 AND BOOK3

whereas the desired output is :
COMPUTERS:P4 LAPTOPS FOR RECORD1
BOOK3 CHAIRS FOR RECORD2
 I have my xslt below ; I know why it is giving me the above output but don't know how to fix this so that it can get me the desired output...


This is my XML:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="book.xsl"?>
<f:root xmlns:f="http://www.w3schools.com/furniture">
    <f:record>
        <f:item id="600">
            <f:book code="a">COMPUTERS:P4</f:book>
        </f:item>
        <f:item id="600">
            <f:book code="a">COMPUTERS:P3</f:book>
        </f:item>
          <f:item id="700">
            <f:book code="a">LAPTOPS</f:book>
        </f:item>

    </f:record>
    <f:record>
        <f:item id="100">
            <f:book code="a">BOOK3</f:book>
        </f:item>
        <f:item id="100">
            <f:book code="a">BOOK4</f:book>
        </f:item>
          <f:item id="700">
            <f:book code="a">CHAIRS</f:book>
        </f:item>
    </f:record>
</f:root>

---------------------------------------------------------
[b] This is my xslt [b]
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:f="http://www.w3schools.com/furniture">
        <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
 <xsl:template match="/">
        <xsl:for-each select="f:root/f:record">
            <xsl:for-each select="f:item[1]">
                <xsl:if test="@id=100">
                    <xsl:value-of select="."/> <br/>
                </xsl:if>
            </xsl:for-each>
            <xsl:for-each select="f:item[1]">
                <xsl:if test="@id=600">
                    <xsl:value-of select="."/> <br/>
                </xsl:if>
            </xsl:for-each>
            <xsl:for-each select="f:item[1]">
                <xsl:if test="@id=700">
                    <xsl:value-of select="."/> <br/>
                </xsl:if>
            </xsl:for-each>

        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>


 
Old August 7th, 2006, 08:47 PM
Authorized User
 
Join Date: Jul 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

can someone please provide the solution to this...........

 
Old August 8th, 2006, 11:33 AM
Authorized User
 
Join Date: Jul 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

 can someone please provide the solution to this...........






Similar Threads
Thread Thread Starter Forum Replies Last Post
Removing duplicate nodes post-process QuickSilver002 XSLT 3 April 4th, 2007 03:47 PM
Copying Source Node attributes to output node pvsat XSLT 2 November 3rd, 2005 09:46 AM
Determining which of 2 nodes in this node chrscote XSLT 1 August 9th, 2005 11:45 AM
Help: Select every node except these nodes arcuza XSLT 8 May 25th, 2005 08:07 AM
Avoid duplicate node values nambati XSLT 1 September 8th, 2004 02:41 PM





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