Solved

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

Posted on 2007-12-06
5
457 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

707 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