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 December 18th, 2012, 12:11 PM
Authorized User
 
Join Date: Sep 2012
Posts: 35
Thanks: 18
Thanked 0 Times in 0 Posts
Exclamation XPath to exclude child elements.

Hello,

Given an XML file of :
Code:
Level_1
	Level_2
		Level_3
Is there any XPath can I use to return ONLY the Level_2 element (and attributes) with NO Level_3 elements? I'm thinking not, but thought I would ask just in case.

As using
Code:
/Level_1/Level_2 
/Level_1/Level_2[@*]
/Level_1/child::Level_2
all return the child elements.

In case you are wondering why I want this there can be many 1000s of Level_3 elements and for the purpose of this transformation are redundant, so didn't really want them returned.

--
William

Last edited by WilliamYou; December 18th, 2012 at 12:50 PM..
 
Old December 18th, 2012, 12:53 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Actually the query /level1/level2 returns a node-set containing a single node, the level2 element. Your problem is that the application processing the result of the XPath expression will typically process the subtree under the returned node, and this subtree of course includes the level3 element. So you need to return a modified copy of the level2 node in which the level3 element has been removed. XPath cannot transform the XML, so this is beyond the capabilities of XPath: you need XSLT or XQuery.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
WilliamYou (December 18th, 2012)
 
Old December 18th, 2012, 12:58 PM
Authorized User
 
Join Date: Sep 2012
Posts: 35
Thanks: 18
Thanked 0 Times in 0 Posts
Default

As always; Thank you, I was beginning to think it was me!

--
William





Similar Threads
Thread Thread Starter Forum Replies Last Post
Change ALL DateTime Child Elements BobSSC XSLT 2 February 18th, 2010 01:28 PM
Excluding elements in XPath baseliner BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition ISBN: 978-0-470-19274-0 8 July 29th, 2009 11:34 AM
Exclude Elements in Apply Templates mail4kaja XSLT 18 November 29th, 2008 12:09 PM
How to use Xpath to extract child nodes smilysuresh XSLT 3 February 23rd, 2007 06:32 AM
how to exclude elements in the result tree output ntmt XSLT 0 May 25th, 2006 10:33 AM





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