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 March 13th, 2006, 05:27 PM
Registered User
 
Join Date: Mar 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default deleting or replacing a single element

Hi everyone,

i'm new to xsl so bare with me. I have the following xml snippet

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
        <property name="driverClassName">
            <value>com.mysql.jdbc.Driver</value>
        </property>
        <property name="url">
            <value>jdbc:mysql://localhost/someDB</value>
        </property>
        <property name="username">
            <value>aUsername</value>
        </property>
        <property name="password">
            <value>aPassword</value>
        </property>
    </bean>

i would like to change the url value to something else (eg. jdbc:mysql://localhost/devDB)

This is what I have so far.... but all it does is add another "value" node instead of replacing the current one...

<?xml version="1.0" encoding="UTF-8" ?>
  <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:xalan="http://xml.apache.org/xslt">
  <xsl:output method="xml" indent="yes" doctype-system="http://java.sun.com/dtd/web-app_2_3.dtd" doctype-public="-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" />

      <xsl:template match = "property[@name='url']">
          <xsl:element name="{name()}">
              <xsl:call-template name = "putInAttributeName"/>
              <xsl:element name = "value">jdbc:mysql://localhost/devDB</xsl:element>
              <xsl:apply-templates select="*"/>
          </xsl:element>
      </xsl:template>


      <xsl:template name="putInAttributeName">
          <xsl:attribute name="name">
              <xsl:value-of select="@name" />
          </xsl:attribute>
      </xsl:template>

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

  </xsl:transform>

can i get some examples on how to go about doing this?

thanks,
Hasdy

 
Old March 13th, 2006, 06:32 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You've got the general idea. But the only template you need in addition to the identity template, I think, is

      <xsl:template match = "property[@name='url']/value">
        <value>jdbc:mysql://localhost/devDB</value>
      </xsl:template>

Alternatively, you could probably just delete the

<xsl:apply-templates select="*"/>

from your template rule, because it's this instruction that's causing the old value element to be copied.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 14th, 2006, 11:34 AM
Registered User
 
Join Date: Mar 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Works great! thanks for the help!

 
Old March 14th, 2006, 12:28 PM
Registered User
 
Join Date: Mar 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

One more thing......

How do you transform elements inside the node tag?

For example...

<Context path="/hello" docBase="hello" debug="0"
reloadable="true" antiJARLocking="true" antiResourceLocking="true"/>

and I want to change the path and docBase to "goodBye"



      <xsl:template match = "Context">

           what goes in here????

      </xsl:template>

Thanks in advance,
Hasdy







Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem adding element to the previous element dani1 XSLT 5 September 10th, 2008 01:38 AM
Trying to save is deleting and replacing records ! Bjay VB Databases Basics 1 August 16th, 2007 02:12 PM
Replacing nbsp delinear Javascript 2 February 5th, 2005 12:22 PM
adding of element and assigning to one element sushovandatta XSLT 2 November 16th, 2004 07:04 PM
ASP.Net Single Single-on with Oracle Application S guhanath Oracle 0 October 6th, 2004 05:05 AM





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