Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 5th, 2006, 08:28 PM
Registered User
 
Join Date: May 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Removing an element

Hi,

Firstly - sincere apologies for not posting this separately in the first place.

I'd like to transform from Reporting Services XML to eliminate a tag.

The original XML is:

<?xml version="1.0"?>
<Report xmlns="UC203_x0020_Export_x0020_Returning_x0020_Of ficer_x0020_Details_x0020_for_x0020_Advertising" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="UC203_x0020_Export_x0020_Retur ning_x0020_Officer_x0020_Details_x0020_for_x0020_A dvertising http://localhost/reportserver/folder?%2fUC203+Export+Returning+Officer+Details+f or+Advertising&amp;ElectionEventID=dee73101-8b80-4cae-8c15-e6fed571dfa2&amp;ElectorateTypeID=00000001-0001-0001-0001-000000000006&amp;rs%3aFormat=XML&amp;rs%3aShowHide Toggle%3aisnull=True&amp;rc%3aSchema=True" Name="UC203 Export Returning Officer Details for Advertising">
    <ReturningOfficers>
        <ReturningOfficerDetails_Collection>
            <ReturningOfficerDetails>
                <Electorate>Ballajura</Electorate>
                <RO>Tom Jones</RO>
                <Tel>92913336</Tel>
                <Mob> </Mob>
                <Venue>Ballajura Community College</Venue>
                <Address1>Illawarra Crescent South</Address1>
                <Address2> </Address2>
                <Suburb>BALLAJURA</Suburb>
            </ReturningOfficerDetails>
        </ReturningOfficerDetails_Collection>
    </ReturningOfficers>
</Report>

The ReturningOfficerDetails_Collection and Report tags are to be removed so that the output then becomes:

<?xml version="1.0"?>
<ReturningOfficers>
   <ReturningOfficerDetails>
    <Electorate>Ballajura</Electorate>
    <RO>Tom Jones</RO>
    <Tel>92913336</Tel>
    <Mob> </Mob>
    <Venue>Ballajura Community College</Venue>
    <Address1>Illawarra Crescent South</Address1>
    <Address2> </Address2>
    <Suburb>BALLAJURA</Suburb>
   </ReturningOfficerDetails>
</ReturningOfficers>

Thanks

 
Old May 6th, 2006, 02:34 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You didn't specify whether you wanted to keep the namespaces or not, this stylesheet doesn't.
Code:
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                exclude-result-prefixes="report"
                xmlns:report="UC203_x0020_Export_x0020_Returning_x0020_Officer_x0020_Details_x0020_for_x0020_Advertising">

  <xsl:template match="/">
    <ReturningOfficers>
      <xsl:apply-templates select="report:Report/report:ReturningOfficers/report:ReturningOfficerDetails_Collection/report:ReturningOfficerDetails"/>
    </ReturningOfficers>
  </xsl:template>

  <xsl:template match="report:ReturningOfficerDetails_Collection//*">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates/>
    </xsl:element>

  </xsl:template>
</xsl:stylesheet>
Not the most elegant solution but I believe it works.


--

Joe (Microsoft MVP - XML)
 
Old May 6th, 2006, 03:52 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Write a default template rule that copies elements unchanged:

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

Then supplement it with a rule for selected elements that copies the contents but not the element itself:

<xsl:template match="Report|ReturningOfficeDetails_Collection">
<xsl:apply-templates/>
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 8th, 2006, 11:00 PM
Registered User
 
Join Date: May 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for that - works very nicely






Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem adding element to the previous element dani1 XSLT 5 September 10th, 2008 01:38 AM
translate element name to element name lexzeus XSLT 3 September 4th, 2006 09:04 AM
adding of element and assigning to one element sushovandatta XSLT 2 November 16th, 2004 07:04 PM
Removing an Element from an Associative Array nick8245 Javascript 2 September 26th, 2003 12:15 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.