 |
| 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
|
|
|
|

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

February 4th, 2010, 01:07 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
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:
|
|
|

February 4th, 2010, 03:42 PM
|
|
Registered User
|
|
Join Date: Feb 2010
Posts: 5
Thanks: 4
Thanked 0 Times in 0 Posts
|
|
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>
|
|

February 4th, 2010, 05:07 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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>
|
|
The Following User Says Thank You to samjudson For This Useful Post:
|
|
|

February 4th, 2010, 05:27 PM
|
|
Registered User
|
|
Join Date: Feb 2010
Posts: 5
Thanks: 4
Thanked 0 Times in 0 Posts
|
|
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.
|
|

February 4th, 2010, 05:36 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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.
|
|
The Following User Says Thank You to samjudson For This Useful Post:
|
|
|

February 4th, 2010, 05:58 PM
|
|
Registered User
|
|
Join Date: Feb 2010
Posts: 5
Thanks: 4
Thanked 0 Times in 0 Posts
|
|
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!
|
|

February 5th, 2010, 05:41 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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>
|
|
The Following User Says Thank You to samjudson For This Useful Post:
|
|
|

February 5th, 2010, 09:26 AM
|
|
Registered User
|
|
Join Date: Feb 2010
Posts: 5
Thanks: 4
Thanked 0 Times in 0 Posts
|
|
Thanks, Sam, this helps a lot. Appreciate it!
|
|
 |