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 February 22nd, 2005, 02:35 AM
Registered User
 
Join Date: Feb 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default match only first level children

Hello,

I would like to get only the "direct" children value, not level values of a xml tag.
Here is a sample
XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<test>
    <a>
        <b>b1</b>
        <b>b2</b>
        <b>
            <c>
                <b>b3.1</b>
            </c>
        </b>
        <b>b3</b>
    </a>
</test>

and XSL

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:strip-space elements="*"/>
    <xsl:template match="/test/a">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="b">
        <new><xsl:value-of select="."/></new>
    </xsl:template>
</xsl:stylesheet>


Result:

<?xml version="1.0" encoding="utf-8"?>
<new>b1</new>
<new>b2</new>
<new>b3.1</new>
<new>b3</new>

There result contains the value 3.1 but I dont want it.

I guess it something in the match value.
Thank you
Seb


 
Old February 22nd, 2005, 05:12 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Your a element has 4 direct b children. The third one is

 <b>
      <c>
           <b>b3.1</b>
      </c>
 </b>

You are processing the direct children by outputting their string-value (using xsl:value-of). The string value of the above element is "b3.1" (with some surrounding whitespace, unless you are using MSXML which incorrectly removes it).

I don't know what rules you want to apply. If you want to completely exclude b elements that have element children of their own, write an extra template rule

<xsl:template match="b[*]"/>



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
help with nodes/children bmmayer XML 0 July 13th, 2007 05:04 PM
MDI Children angelboy C# 2005 5 June 28th, 2007 06:08 AM
template match doesnt match the required node Tomi XSLT 2 March 12th, 2007 06:24 AM
Geting Index of a children tutul128 XML 2 May 27th, 2004 10:34 AM
get all children nodes maratg XSLT 4 November 7th, 2003 02:07 PM





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