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 23rd, 2009, 04:14 AM
Authorized User
 
Join Date: Apr 2007
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default Filtering XML Elements using XSLT.

Hi All,

i have a requirement to filter xml elements .
For eg:-
If my input is :-
<root>
<a/>
<b/>
<c/>
</root>
In this after applying xslt i need to filter some elements .

For example dynamically i will know "b & c" elements needs to be filtered and my final xml should be something like this .

<root>
<a/>
</root>

Is it possible to do so using xslt .

Please help me in this .

Regards,
Sasi.A
__________________
A. Sasi
 
Old March 23rd, 2009, 06:39 AM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 119
Thanks: 25
Thanked 3 Times in 3 Posts
Default

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      exclude-result-prefixes="xs"
      version="2.0">
    <xsl:template match="root">
        <xsl:copy>
            <xsl:apply-templates select="a"></xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="a">
        <xsl:copy-of select="."></xsl:copy-of>
    </xsl:template>
</xsl:stylesheet>
Cheers, John Bampton.
 
Old March 23rd, 2009, 11:22 AM
Authorized User
 
Join Date: Apr 2007
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi John,
Thanks for your help .
I have one more small doubt, If "a &b" elements are dynamic for me i.e suppose a & b are not static for me , I will get some element names dependind upon some selection and those elements should be there in the final xml .

In this case user will select a & b elements and i will get that info and the original xml file and then i need to apply for filtering of all the other elements except the elements i got from the user.

I hope i am clear .

Once again thanks for your valuable reply.

Regards,
Sasi.A
__________________
A. Sasi
 
Old March 23rd, 2009, 11:30 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Which XSLT processor do you use? Do you use XSLT 1.0 or 2.0?
If you get element names 'a' and 'b', do you want to remove elements with those names? Or do you want to remove elements with names different from 'a' and 'b'?
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old March 23rd, 2009, 11:52 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Here is an example using XSLT 2.0 which assumes you pass in a parameter named to-delete which is a sequence of local names to be deleted:

Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs"
  version="2.0">
  
  <xsl:output method="xml" indent="yes"/>
  
  <xsl:strip-space elements="*"/>
  
  <xsl:param name="to-delete" as="xs:string*"/>
  
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="*[local-name() = $to-delete]"/>

</xsl:stylesheet>
For instance, using AltovaXML tools, you could run that as follows to delete elements named 'a' and 'c':
AltovaXML.exe /xslt2 sheet.xsl /in file.xml /param to-delete="('a', 'c')"
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old March 23rd, 2009, 11:56 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

As an alternative, if you want to pass in element names you want to copy, here is an example that takes element names to be copied as the parameter to-copy as a sequence of strings:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs"
  version="2.0">
  
  <xsl:output method="xml" indent="yes"/>
  
  <xsl:strip-space elements="*"/>
  
  <xsl:param name="to-copy" as="xs:string*"/>
  
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="/*/*[not(local-name() = $to-copy)]"/>

</xsl:stylesheet>
It assumes you pass in names of child element names of the root element which you want to copy.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
filtering xml in using XSLT venjamin XSLT 1 October 20th, 2006 04:21 AM
Filtering an xml with xslt Tomi XSLT 4 September 5th, 2006 06:29 AM
Filtering XML data based on differnt XML ahmed123 XSLT 5 August 11th, 2006 09:15 AM
renumbering xml elements with XSLT csbdeady XSLT 5 July 27th, 2005 09:17 AM
adding elements in XSLT 1.0 spencer.clark XSLT 3 July 25th, 2005 09:41 AM





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