• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Simple yet perplexing XSLT question

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to transform the following simple XML, replacing test1 with test2 and test2 with test3.

<?xml version="1.0" encoding="UTF-8"?>
<bla xmlns="http://java.sun.com/xml/ns/javaee">
<display-name>test1</display-name>
<module>
<web>
<web-uri>RAV.war</web-uri>
<context-root>test2</context-root>
</web>
</module>
</bla>

And here's my XSL:

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

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />

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

<xsl:template match="/bla/display-name">
<display-name>test2<display-name>
</xsl:template>

<xsl:template match="/bla/module/web/context-root">
<context-root>test3</context-root>
</xsl:template>
</xsl:stylesheet>

This should work but the result is that I am not seeing any transformation at all (it only applied identity transformation). It didn't match /bla/display-name or /bla/module/web/context-root.

When I removed the default namespace ("xmlns="http://java.sun.com/xml/ns/javaee") for <bla> tag, then the transformation worked as expected.

How come it could not match /bla/ if it contained this default namespace? How can I fix XSL so that it works? Thank you so much.
 
Sheriff
Posts: 28368
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your problem isn't exactly with XSLT, it is an XPath problem. XPath is unable to match elements in the default namespace. (As your experiment with removing the namespace declaration demonstrates.)

You could use an XPath expression like this one:
 
Jean Robillard
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. I really needed to understand how namespaces really worked. The following URL was helpful.

http://radio.weblogs.com/0118231/stories/2006/10/03/xslt10PatternMatchingTipsForSourceDocumentsWithNamespaces.html
 
She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic