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 September 12th, 2007, 12:43 PM
Registered User
 
Join Date: Sep 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default copying xml attributes

Hi, new to the forum and xslt.

XSLT:

<?xml version="1.0" encoding="utf-16" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="http://ACORD.org/Standards/Life/2">
    <xsl:output method="xml"/>


    <xsl:template match="/ns:TXLife/ns:UserAuthRequest/ns:UserPswd/ns:Pswd">
            &lt;<xsl:value-of select="name()"/>&gt;<xsl:value-of select="99999999"/>&lt;/<xsl:value-of select="name()"/>&gt;
    </xsl:template>

     <xsl:template match="*">
         &lt;<xsl:value-of select="name()"/>&gt;
         <xsl:apply-templates select="*"/>
         <xsl:value-of select="text()"/>
         &lt;/<xsl:value-of select="name()"/>&gt;
    </xsl:template>
</xsl:stylesheet>

I'm trying to accomplish a simple task: Create a near-copy of an XML document, but substitute some values in the transformation.

It seems to be a decent start, but I have two questions:

1) in the "*" template, until I added 'select="*"' to the apply-templates, the text in my nodes was duplicated. Not where I create the tags, just the text:

         &lt;ns:UserTime&gt;
         14:33:11-06:0014:33:11-06:00
         &lt;/ns:UserTime&gt;

After adding the select, works as expected. If it were processing the 'UserTime' node twice, why didn't I get the tags twice as well? Clearly something about recursion I'm not seeing.

2) Can someone point me in a direction to ensure the attributes of nodes are copied over as well?

Thanks a million. I've enjoyed reading/using the forum and books.
 
Old September 12th, 2007, 01:05 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Don't try to create markup by hand. Create element nodes (using xsl:element for example) and let the serializer do the markup for you.

You've sort-of reinvented the important concept of the identity template by yourself, congratulations. I usually write it as:

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

which I suspect will meet your needs better.

Doing

    <xsl:apply-templates select="*"/>
    <xsl:value-of select="text()"/>

seems a bad idea as it will process all the child elements, then all the child text nodes, which loses the order if elements and text nodes are mixed together. Doing <xsl:apply-templates select="node()"/> (which is the default with no select) does the same thing but retains the order.

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Copying Source Node attributes to output node pvsat XSLT 2 November 3rd, 2005 09:46 AM
Problem copying XML nodes francislang XSLT 9 October 21st, 2005 10:37 AM
Copying XML nodes from one document to another hughcr C# 2 May 12th, 2005 01:20 AM
XML to XML, checking attributes raoulvb XSLT 4 December 9th, 2004 10:15 AM
XML attributes miguel.ossa C# 2 February 9th, 2004 07:59 AM





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