Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 13th, 2007, 12:23 AM
Registered User
 
Join Date: Jul 2007
Location: , , .
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.


Reply With Quote
  #2 (permalink)  
Old July 13th, 2007, 04:57 AM
mhkay's Avatar
Wrox Author
Points: 18,277, Level: 58
Points: 18,277, Level: 58 Points: 18,277, Level: 58 Points: 18,277, Level: 58
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,933
Thanks: 0
Thanked 282 Times in 277 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
Reply With Quote
  #3 (permalink)  
Old July 13th, 2007, 01:02 PM
Registered User
 
Join Date: Jul 2007
Location: , , .
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.


Reply With Quote
  #4 (permalink)  
Old July 13th, 2007, 01:54 PM
mhkay's Avatar
Wrox Author
Points: 18,277, Level: 58
Points: 18,277, Level: 58 Points: 18,277, Level: 58 Points: 18,277, Level: 58
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,933
Thanks: 0
Thanked 282 Times in 277 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
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacement for DDE mackas.net VB.NET 2 July 20th, 2010 12:39 PM
Problem about replacement of already loaded js helpless_freshman Javascript 7 August 31st, 2007 09: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 05:00 AM
Replacement beyondforsaken VB How-To 0 June 14th, 2003 11:16 AM



All times are GMT -4. The time now is 07:46 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.