Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
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 …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

746 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

12 Experts available now in Live!

Get 1:1 Help Now