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 July 12th, 2007, 11:23 PM
Registered User
 
Join Date: Jul 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dynamic replacement of attribute value

Hi,
I need to read an XML, and based on the attribute value, I need to query a java HashMap and replace the corresponding attribute value. For example, if I have a HashMap of [country name, abbrev name], in the following XML:


<items>
    <location num="1" country="United States">
    <location num="2" country="Canada">
    <location num="3" country="United Kingdom">
    <location num="4" country="India">
</items>


The resulting XML should look like:

<items>
    <location num="1" country="US">
    <location num="2" country="CA">
    <location num="3" country="UK">
    <location num="4" country="IN">
</items>


Using Java (JAXP) how can I replace the said values?

Ideally, my XSL would look somewhat like:


<xsl:template match="location[@country]">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:attribute name="country">
      <xsl:value-of select="myHashMap.get('countryname').toString()"/>
    </xsl:attribute>
  </xsl:copy>
</xsl:template>


Is there an easier way to do such a replacement?
thanks for your help.


 
Old July 13th, 2007, 03:57 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Many JAXP implementations allow you to call Java methods on Java objects as extension functions, but the details depend on the product you are using (it's not defined in the JAXP interface itself). So consult your product documentation. In Saxon you could pass the HashMap directly as a parameter to the transformer and then access it as:

<xsl:param name="map"/>

...

<xsl:value-of select="Map:get($map, 'countryname')"
  xmlns:Map="java:java.util.HashMap"/>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 13th, 2007, 12:02 PM
Registered User
 
Join Date: Jul 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Mike,
That's great! Sorry for a newbie question, but is it possible for me to do the same Java-XSL transformation using SAXON? I don't have to use JAXP, just anything that'll get my job done.
I would also appreciate some links to Java/XSLT examples using SAXON and link to download SAXON.jar.
thanks a bunch.


 
Old July 13th, 2007, 12:54 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You can download the open-source version of Saxon from

http://sourceforge.net/project/showf...group_id=29872

or Saxon-SA from http://www.saxonica.com/

The latest version is 8.9.0.4

You should be able to use your JAXP application essentially unchanged, just set the system property javax.xml.transform.TransformerFactory to "net.sf.saxon.TransformerFactoryImpl".

There's info on using Saxon from JAXP at http://www.saxonica.com/documentatio...embedding.html

and information on using extension functions with Saxon at http://www.saxonica.com/documentatio...functions.html

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
Replacement for DDE mackas.net VB.NET 2 July 20th, 2010 11:39 AM
Problem about replacement of already loaded js helpless_freshman Javascript 7 August 31st, 2007 08:14 PM
treeview replacement need an idea@opinion zerzex ASP.NET 2.0 Basics 0 February 24th, 2007 11:35 AM
Text Replacement Problem. rupen Classic ASP Basics 4 August 12th, 2005 04:00 AM
Replacement beyondforsaken VB How-To 0 June 14th, 2003 10:16 AM





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