Solved

How to drop parent element and all of it's children using template match with condition?

Posted on 2007-12-06
5
402 Views
Last Modified: 2013-11-18
Greetings,

Given the following element of which there are n number of them in my XML document:

<Record Identifier="V1A2" Type="Note" Readonly="0">
   <Property Name="Name">XMMSG-AFile</Property>
   <Property Name="Description"/>
   <Property Name="NextID"/>
   <Property Name="InputPs"/>
   <Property Name="OutputPs"/>
   <Property Name="NoteType">0</Property>
   <Property Name="NoteText" PreFormatted="1">XMMSG:Failed.</Property>
   <Property Name="TextFont">Arial\10\0\0\0\700\0</Property>
   <Property Name="TextHorizontalJustification">0</Property>
   <Property Name="TextVerticalJustification">0</Property>
   <Property Name="TextColor">0</Property>
   <Property Name="BackgroundColor">4103935</Property>
   <Property Name="BackgroundTransparent">0</Property>
   <Property Name="BorderVisible">1</Property>
</Record>

In XSLT I want to be able to delete all <Record> elements in the XML document when the <Property Name="NoteText" content contains the value "XMMSG".

Currently I do the following in XSLT to re-generate the XML document and delete the <Record> elements that meet this condition:

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml"/>

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

<xsl:template match="DSExport/*/Record[contains(@Type,'Note')]/Property[@Name='NoteText' and contains(text(),'XMMSG:')]">
</xsl:template>

</xsl:stylesheet>

And get the following output:

<Record Identifier="V1A2" Type="Note" Readonly="0">
   <Property Name="Name">XMMMSG-Afile</Property>
   <Property Name="Description"/>
   <Property Name="NextID"/>
   <Property Name="InputPins"/>
   <Property Name="OutputPins"/>
   <Property Name="NoteType">0</Property>
         
   <Property Name="TextFont">Arial\10\0\0\0\700\0</Property>
   <Property Name="TextHorizontalJustification">0</Property>
   <Property Name="TextVerticalJustification">0</Property>
   <Property Name="TextColor">0</Property>
   <Property Name="BackgroundColor">4103935</Property>
   <Property Name="BackgroundTransparent">0</Property>
   <Property Name="BorderVisible">1</Property>
</Record>

And so it only deletes the NoteText Property. What I was expecting was the the entire <Record> would be deleted, how do I delete all <Record> elements based on what "NoteText" contains given a value to match on?

TIA,

David
0
Comment
Question by:adgarcia13
  • 3
  • 2
5 Comments
 

Author Comment

by:adgarcia13
ID: 20420558
Btw,

I understand the current context in the 2nd template match is such that it is on the NoteText and that is why I am only getting rid of the NoteText property.. how do I reach back up once I have the correct <record> element and reset my context the the parent (<Record>) so I can remove the entire <record>? maybe I should have coded this some other way?
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 20421293
you have a template that matches at the Property level
make it like this
<xsl:template match="DSExport/*/Record[contains(@Type,'Note')][Property[@Name='NoteText' and contains(text(),'XMMSG:')]]">
</xsl:template>
(put the property information in an extra predicate)
and it will drop the entire record
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20421310
if you use '/' you go down one level
if you put it in a predicate []
you only test deeper down the tree,
but you don't walk there
0
 

Author Comment

by:adgarcia13
ID: 20425041
Works as published, thanks!
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20425880
welcome
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now