Solved

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

Posted on 2007-12-06
5
441 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Matthew
I am a very big proponent of technology compliance standards and strive to meet such criteria in all of my work. That includes my site, which is 100% XHTML 1.0 compliant as determined by the World Wide Web Consortium. https://www.matthewstevenkel…
Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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…

730 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