Solved

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

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Best way to find duplicate message in database 7 32
Removing Flash from Website 6 65
CSS (line height?) issue 2 65
Select distinct 25 103
When you work with shopping cart / ecommerce relates web sites, you need to pass the certain form post details to the payment gateway process page with required details for the products items you give to order. Also you may need to track the ordered…
Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

832 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