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 February 4th, 2010, 01:02 PM
Registered User
 
Join Date: Feb 2010
Posts: 5
Thanks: 4
Thanked 0 Times in 0 Posts
Default <xsl:copy-of> with <xsl:element>

I need to make a deep copy of a node in my source document and add a prefix to the element name in the output document, for example:

Source:

Code:
<stuffidon'twanttocopy>
<level1>
<level2>"text"</level2>
<level2>"text">
<level3>"more text"</level3>
</level2>
</level1>
</stuffidon'twanttocopy>
Desired output:

Code:
<prefix:level1>
<prefix:level2>"text"</prefix:level2>
<prefix:level2>"text">
<prefix:level3>"more text"</prefix:level3>
</prefix:level2>
</prefix:level1>
I've tried constructing a template using <xsl:copy-of> in combination with <xsl:element> without success. Can anyone suggest a sample template so I get the syntax right?
 
Old February 4th, 2010, 01:07 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Code:
<xsl:template match="*">
  <xsl:element name="prefix:{local-name()}" namespace="http://example.com/">
     <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

<xsl:template match="stuffidontwanttocopy">
  <xsl:apply-templates/>
</xsl:template>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
learningxsl (February 4th, 2010)
 
Old February 4th, 2010, 03:42 PM
Registered User
 
Join Date: Feb 2010
Posts: 5
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Martin: Many thanks, it's almost working now! I still have trouble excluding <stuffIdontwanttocopy>, though--can't find the right match pattern.

If my source document looks like this:

Code:
<root>
<stuff1>
<nestedstuff1/>
<nestedstuff2/>
</stuff1>
<stuff2:this is the only node I want to keep>
<nestedstuff1/>
<nestedstuff2/>
</stuff2>
<stuff3>
<nestedstuff1/>
<nestedstuff2/>
</stuff3>
</root>
then how do I phrase the match attribute in the second template to strip out everything but <stuff2> and its subelements?

Code:
<xsl:template match="*">
  <xsl:element name="prefix:{local-name()}" namespace="http://example.com/">
     <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

<xsl:template match="???">
  <xsl:apply-templates/>
</xsl:template>
 
Old February 4th, 2010, 05:07 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

If you don't want to keep an element, or anything in it, then do this:

<xsl:template match="stuff1" />

If you want to keep the stuff inside it, but not the element itself, then do this:

<xsl:template match="stuff2">
<xsl:apply-templates select="*"/>
</xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
learningxsl (February 4th, 2010)
 
Old February 4th, 2010, 05:27 PM
Registered User
 
Join Date: Feb 2010
Posts: 5
Thanks: 4
Thanked 0 Times in 0 Posts
Default

I want to keep <stuff2> with all its children, attributes and content. However, if I do

Code:
<xsl:template match="stuff1" />
I get rid of <stuff1> only, not the root element which is its parent and not its descendants either. Of course I can't match on the root element because then I don't get the child node I want to keep, either.

Thanks as always.
 
Old February 4th, 2010, 05:36 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I'm not 100% clear on what you are expecting your output to look like. I'm guessing your real XML doesn't have elements called stuff1 etc.

Any chance of some real input XML, and what you are expecting your output to look like.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
learningxsl (February 4th, 2010)
 
Old February 4th, 2010, 05:58 PM
Registered User
 
Join Date: Feb 2010
Posts: 5
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Sam,

you're right, I was trying to simplify.

My input XML looks like this:

Code:
<?xml version="1.0" encoding="UTF-8"?>

<ead>    
    <eadheader>
        <eadid>ID Number</eadid>
        <filedesc>
            <titlestmt>
                <titleproper>Title, <date
                    type="inclusive">Inclusive Date</date> (bulk <date
                        type="bulk">Bulk Date</date>) </titleproper>
            </titlestmt>   
        </filedesc>
        <profiledesc>
            <creation>Created by Somebody.</creation>
            <langusage>Written in <language encodinganalog="dc:language" langcode="eng"
                >English</language>. </langusage>
        </profiledesc>
    </eadheader>
    <archdesc>
        <did>
            <head>Summary Information</head>
            <unitid>Call Number</unitid>
            <repository label="Location: ">
                <corpname>Repository Name</corpname>
                <subarea>Name Line 2</subarea>
                <subarea>Name Line 3</subarea>
                <address>
                    <addressline>Address Line 1 </addressline>
                    <addressline>Address Line 2</addressline>
                </address>
            </repository>
            <unittitle label="Title and dates: ">Title
                <unitdate type="inclusive"
                    >Inclusive Date</unitdate> (bulk <unitdate 
                        type="bulk">Bulk Date</unitdate>) </unittitle>
            <physdesc label="Size: ">
                <extent >Extent Statement</extent>
            </physdesc>
            <origination label="Creator: ">
                <persname >Creator's Name
                 </persname>
            </origination>
            <langmaterial label="Language(s) of material: ">
                <language langcode="eng">English</language>. Other languages include <language langcode="fre"
                    >French</language>. </langmaterial>
            <abstract label="Abstract: ">Abstract here.</abstract>
            <physloc label="Storage note:">Storage note here.</physloc>
        </did>
    </archdesc>
</ead>
My desired output is only one child node, with only some of its descendants, and all of the element names changed to include a prefix:

Code:
<ead:did>
    <ead:head>Summary Information</ead:head>
    <ead:unitid>Call Number</ead:unitid>
    <ead:unittitle label="Title and dates: ">Title
        <ead:unitdate type="inclusive"
            >Inclusive Date</ead:unitdate> (bulk <ead:unitdate 
                        type="bulk">Bulk Date</ead:unitdate>) </ead:unittitle>
    <ead:physdesc label="Size: ">
        <ead:extent >Extent Statement</ead:extent>
            </ead:physdesc>
    <ead:origination label="Creator: ">
        <ead:persname >Creator's Name
                 </ead:persname>
            </ead:origination>
    <ead:langmaterial label="Language(s) of material: ">
        <ead:language langcode="eng">English</ead:language>. Other languages include <ead:language langcode="fre"
                    >French</ead:language>. </ead:langmaterial>
    <ead:abstract label="Abstract: ">Abstract here.</ead:abstract>
    <ead:physloc label="Storage note:">Storage note here.</ead:physloc>
        </ead:did>
Thanks for your help!
 
Old February 5th, 2010, 05:41 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I don't usually do this, but here is the complete stylesheet. Hopefully you can see the purpose of the 4 different templates included.

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ead="http://test.com"
                version="2.0">
    
   <xsl:strip-space elements="*"/>
   <xsl:output indent="yes" method="xml"/>

   <!-- Main template -->
<xsl:template match="ead">
      <ead:did>
         <xsl:apply-templates select="*"/>
      </ead:did>
   </xsl:template>

   <!-- Template for elements to ignore, but process inner children -->
<xsl:template match="archdesc | did">
      <xsl:apply-templates select="node()"/>
   </xsl:template>
        
      <!-- Template for elements to process -->   
      <xsl:template match="element()">
            <xsl:element name="ead:{local-name()}" namespace="http://test.com">
                     <xsl:copy-of select="@*"/>
                  <xsl:apply-templates select="node()"/>
            </xsl:element>
      </xsl:template>
      
      <!-- Template for elements to be ignored completely -->
      <xsl:template match="eadheader | repository"/>

</xsl:stylesheet>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
learningxsl (February 5th, 2010)
 
Old February 5th, 2010, 09:26 AM
Registered User
 
Join Date: Feb 2010
Posts: 5
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Thanks, Sam, this helps a lot. Appreciate it!





Similar Threads
Thread Thread Starter Forum Replies Last Post
embedded <xsl:element> into <xsl:with-param> petergoodman XSLT 2 July 9th, 2008 06:36 AM
Performance for <xsl:import> and <xsl:include> vikkiefd XSLT 2 April 16th, 2008 08:06 AM
<xsl:for-each> inside another <xsl:for-each> suersh79 XSLT 2 December 29th, 2006 01:24 AM
name attribute for <xsl:element> rushman XSLT 2 June 9th, 2005 09:04 AM
<xsl:choose> and <xsl:otherwise> problem djmarquette XSLT 4 January 21st, 2005 01:56 PM





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