XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
V. CaspiSubject: Parametized search and replace for XML elements
Author: V. Caspi
Date: 10 Oct 2006 07:05 AM
Hi,
I'm very new in working with XSLT. I need to define a transformation which gets as parameters: 1) XML element name and 2) new value for it and replaces all the XML elements with this name with the new value.

I've managed doing it with attributes (see example below), but have difficulties with element name match parameterization in templates etc.

Could you please give me some useful example?

Thanks,
caspi.

-------------------------------------------------------------
Attributes parameterized replacement example:
--------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml"/>
<xsl:param name="attrName"/>
<xsl:param name="attrValue"/>

<xsl:template match="node()|@*">

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

<xsl:template match="@*" priority="10">
<xsl:attribute name="{name()}">
<xsl:choose>
<xsl:when test="name()=$attrName"><xsl:value-of select="$attrValue"/></xsl:when>
<xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>

Postnext
Ivan PedruzziSubject: Parametized search and replace for XML elements
Author: Ivan Pedruzzi
Date: 10 Oct 2006 10:29 AM

If you need to match any element use the following
<xsl:template match="*">

Ivan Pedruzzi
Stylus Studio Team
http://www.stylusstudio.com/xml_download.html

Posttop
V. CaspiSubject: Parametized search and replace for XML elements
Author: V. Caspi
Date: 10 Oct 2006 11:21 AM
Thank you, Ivan! :-)
Actually I've solved the problem already. So, I'll try to describe it better together with the solution.

I have some XML structured like that:
<row>
<a>..</a>
<b>..</b>
<c>..</c>
....
</row>
<row>
...........

I get a map of row fields to be updated together with their new values.
Each field is mapped to an XML element.
E.g., a: 23, c:12.
And I need to update all the elements appearances.

What I wanted to to is to create some generic XSLT definition file, which gets elementName and elementValue (inner text) and to run it in a loop for each parameter. This decision put me into troubles. Still I don't know how can it be done in an elegant generic way.

So, my solution was dynamic creation of the following XSLT definition file based on a common header and footer, while concatenating to it template nodes for each parameter field I need to update.

The following example shows the XSLT file built for parameters named 'b' and 'c'.

May be this will help anybody in future. :-)

-----------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml"/>

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

<xsl:template match="b">
<xsl:element name="b">NEW_B_VALUE</xsl:element>
</xsl:template>

<xsl:template match="c">
<xsl:element name="c">NEW_C_VALUE</xsl:element>
</xsl:template>

</xsl:stylesheet>

Thanks and BR,
caspi.

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.