473,395 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

XSLT for filtering XML based on attribute value?

I'm trying to write XSLT that will filter out an XML based on an
attribute's value. XML looks like:

<postings>
<channel name="A">
<posting Connected="True" name="Posting1" />
<posting Connected="False" name="Posting2" />
</channel>
<channel name="B">
<channel name="C">
<posting Connected="True" name="Posting3" />
</channel>
</channel>
</postings>

Output should look like:
<postings>
<posting Connected="True" name="Posting1" />
<posting Connected="True" name="Posting3" />
</postings>

So, I'm trying to extract all nodes that have the attribute value
Connected="True." Any node that doesn't have the Connected attribute,
or whose value isn't True should be ignored. I can filter out those
nodes whose value isn't True, but that still leaves the Channel nodes
that don't have the attribute. And when I try to explicitly filter out
the Channel nodes, nothing gets processed since the posting nodes are
children of Channel nodes. If it helps, Channel nodes will never have
the Connected attribute and Posting nodes always will.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2004/10/xpath-functions"
xmlns:xdt="http://www.w3.org/2004/10/xpath-datatypes">

<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="//*[@Connected='False']">
</xsl:template>
</xsl:stylesheet>

Can anyone help?
Thanks,
Carole

Jul 20 '05 #1
4 2007
Try adding

<xsl:template match="channel">
<xsl:apply-templates select="node()"/>
</xsl:template>

to your XSL. This will process the children of the channel elements
without coping them.

Normand
Jul 20 '05 #2
el*****@yahoo.com wrote:
[...]
So, I'm trying to extract all nodes that have the attribute value
Connected="True." Any node that doesn't have the Connected attribute, [...] <xsl:template match="//*[@Connected='False']">
</xsl:template>
</xsl:stylesheet>

Can anyone help?
Thanks,
Carole


So, what if you tried:

<xsl:template match="//*[not(@Connected='True')]">
</xsl:template>
Jul 20 '05 #3
That did it, thanks!

Jul 20 '05 #4
The problem with that is that it will ignore the channel nodes and not
process their children. Using the suggestion from the previous post
did it:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2004/10/xpath-functions"
xmlns:xdt="http://www.w3.org/2004/10/xpath-datatypes">

<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="channel">
<xsl:apply-templates select="node()"/>
</xsl:template>

<xsl:template match="//*[@Connected='False']">
</xsl:template>

</xsl:stylesheet>

Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sergio del Amo | last post by:
Hi, I use the xslt functions provided by php. I am running in my computer the package xampp(www.apachefriends.org) which includes php/apache/mysql .. In this package the php includes the sablotron...
9
by: Iain | last post by:
I want to create an XML configuration file which might look like <REGION Name="Europe" WingDing="Blue"> <COUNTRY Name="UK" WingDing="white"> <TOWN Name="London" WingDing="Orange" /> </COUNTRY>...
2
by: nanookfan | last post by:
Hi all, I'm having a bizarre problem converting XML files to HTML using an XSLT. The problem is only occuring in my Netscape 7.0 browser. What makes it more bizarre is that it is only...
7
by: Harolds | last post by:
The code below worked in VS 2003 & dotnet framework 1.1 but now in VS 2005 the pmID is evaluated to "" instead of what the value is set to: .... xmlItems.Document = pmXML // Add the pmID...
2
by: sp | last post by:
hai i am new to this XSTL and i am trying to write xslt that will filter out the xml value based on Attribute Value. my xml looks like this <catalog> <cd> <title year="1990">Empire...
3
by: thomas.porschberg | last post by:
Hi, I want to read records from a database and export it in an arbitrary format. My idea was to feed a class with a String array fetched from the database and let this class fire SAX events as...
6
by: Scott Zabolotzky | last post by:
I have the following XML sample: <Component Name="A"> <Param Name="OptionEnabled" DataType="boolean"> <Default>true</Default> </Param> <Param Name="Size" DataType="unsignedInt"/> <Param...
2
jkmyoung
by: jkmyoung | last post by:
Here's a short list of useful xslt general tricks that aren't taught at w3schools. Attribute Value Template Official W3C explanation and example This is when you want to put dynamic values...
6
by: John Larson | last post by:
Hi All, I am some information from INSPEC database records in XML to build a relational database of my own. I am currently trying to extract information by doing an XSLT transform of the XML...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.