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 18th, 2005, 03:56 AM
Registered User
 
Join Date: Oct 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem using XSLT to change XML attribute

Hi all,

Given the following XML document...

<data>
 <letter id="sample">
  <group id="12345">
    <source id="12345">
    </source>
    <target id="12345">
    </target>
    <condition id="12345" />
  </group>
 </letter>
</data>

I'm trying to write an XSL transform which copies the document and replaces the numeric id attributes with random numbers.

I've nearly got this sorted although it works for when elements don't have an id attribute but not for when they do. I want to replace existing id attribute values as well. My transform is below;

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet extension-element-prefixes="java" xmlns:java="http://xml.apache.org/xslt/java"
    version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:variable name="random" select="java:java.util.Random.new()"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="group|source|target|condition">
        <xsl:copy>
            <xsl:attribute name="id">
                <xsl:value-of select="java:java.lang.Math.abs(java:nextInt($rand om))"/>
            </xsl:attribute>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

I'm using Xalan and Oxygen to do the transforming.

Thanks in advance,

Chris

 
Old October 18th, 2005, 06:26 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Simplest fix is just to reverse these two instructions:

            <xsl:attribute name="id">
                <xsl:value-of select="java:java.lang.Math.abs(java:nextInt($rand om))"/>
            </xsl:attribute>
            <xsl:apply-templates select="@*|node()"/>


If you write two attributes with the same name to the same element, the second one wins.

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
change an attribute value akentanaka XSLT 2 October 17th, 2008 04:52 AM
Problem in XSLT with "//" in attribute value kaps77 XSLT 3 November 6th, 2007 10:57 AM
XML to XML with xslt problem bertvanpet XSLT 1 July 5th, 2007 06:25 AM
XSLT change class attribute by ID? matallen XSLT 8 March 1st, 2006 05:00 PM
Change 1 attribute safin XSLT 1 October 3rd, 2005 12:19 PM





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