Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 20th, 2005, 09:39 AM
elora_c@yahoo.com
Guest
 
Posts: n/a
Default 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

  #2  
Old July 20th, 2005, 09:39 AM
nstonge@csc.com
Guest
 
Posts: n/a
Default Re: XSLT for filtering XML based on attribute value?

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
  #3  
Old July 20th, 2005, 09:39 AM
Volkm@r
Guest
 
Posts: n/a
Default Re: XSLT for filtering XML based on attribute value?

elora_c@yahoo.com wrote:
[...][color=blue]
> So, I'm trying to extract all nodes that have the attribute value
> Connected="True." Any node that doesn't have the Connected attribute,[/color]
[...][color=blue]
> <xsl:template match="//*[@Connected='False']">
> </xsl:template>
> </xsl:stylesheet>
>
> Can anyone help?
> Thanks,
> Carole
>[/color]

So, what if you tried:

<xsl:template match="//*[not(@Connected='True')]">
</xsl:template>
  #4  
Old July 20th, 2005, 09:39 AM
elora_c@yahoo.com
Guest
 
Posts: n/a
Default Re: XSLT for filtering XML based on attribute value?

That did it, thanks!

  #5  
Old July 20th, 2005, 09:39 AM
elora_c@yahoo.com
Guest
 
Posts: n/a
Default Re: XSLT for filtering XML based on attribute value?

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>

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles