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 October 6th, 2008, 04:43 PM
Registered User
 
Join Date: Oct 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Excluding attribute from xsl:coy-of

Given the following XML:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="results.xsl"?>
<results group="A">
  <match>
    <date>1998-06-10</date>
    <team score="2">Brazil<remark>1970 World Cup champion</remark></team>
    <team score="1">Scotland</team>
  </match>
  <match>
    <date>1998-06-10</date>
    <team score="2">Morocco</team>
    <team score="2">Norway</team>
  </match>
  <match>
    <date>1998-06-23</date>
    <team score="0">Scotland</team>
    <team score="3">Morocco</team>
  </match>
  <match>
    <date>1990-06-23</date>
    <team score="0">Chili<remark>Disqualified for fake injury</remark></team>
    <team score="3">Brazil</team>
  </match>
</results>
And the given XSL:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes">
  <xsl:output method="xml" indent="yes" media-type=""/>
  <xsl:template match="/">
  <xsl:element name="results_new">
    <xsl:for-each select="results/match">
      <xsl:element name="match">
        <xsl:copy-of select="team"/>
      </xsl:element>
    </xsl:for-each>
  </xsl:element>
  </xsl:template>
</xsl:stylesheet>
How can I use xsl:copy-of function to exclude the attribute 'score', but keep the additional elements attached to the team element, such as <remark> statements?

I'm willing to walk through the team node, but I want to copy over any additional mark-up found in the text.

Here's the results I'm expecting:
Code:
<results_new>
   <match>
      <team>Brazil<remark>1970 World Cup champion</remark></team>
      <team>Scotland</team>
   </match>
   <match>
      <team>Morocco</team>
      <team>Norway</team>
   </match>
   <match>
      <team>Scotland</team>
      <team>Morocco</team>
   </match>
   <match>
      <team>Chili<remark>Disqualified for fake injury</remark></team>
      <team>Brazil</team>
   </match>
</results_new>
 
Old October 6th, 2008, 04:56 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

<xsl:copy-of> always does an exact copy. You can't use it if you only want a near-copy.

Use the identity template rule

<xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
</xsl:template>

coupled with extra rules for the things you don't want to copy:

<xsl:template match="@score"/>

<xsl:template match="date"/>

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
escaping of characters in xsl:attribute pramod Classic ASP XML 1 September 12th, 2008 09:11 AM
<xsl:attribute> vs {} berna_isct XSLT 1 April 6th, 2006 03:30 PM
XSL Attribute ID jonty XSLT 1 March 28th, 2006 03:53 PM
onClick as xsl attribute? mlaba XSLT 1 September 10th, 2004 02:36 AM
problem using <xsl:attribute> arvin XSLT 2 July 21st, 2003 03:13 AM





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