1

I need to transform a XML file to HTML format via XSLT and java. One of the tags contains the link of intranet page like:

http://mycompany.com&mypage=xyz&version=999

now I want to use this value in href attribute in generated html.

Before using this value I want to get rid of "amp;" and change the url like appending it with username.

Can I do it via some XSLT function or I have to replace it in java code ?

4 Answers 4

2

Can I do it via some XSLT function or I have to replace it in java code ?

This is trivial in XSLT.

This transformation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:param name="pUserName" select="'someUser'"/>

 <xsl:template match="someLink">
     <a href="{substring-before(., '?')}/{$pUserName}">someLink</a>
 </xsl:template>
</xsl:stylesheet>

When applied on this XML document:

<someLink>http://mycompany.com?mypage='xyz'&amp;version='999'</someLink>

produces the wanted, correct result:

<a href="http://mycompany.com/someUser">someLink</a>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer, I will try it and let you know. Also can I give userName dynamically via some system properties ?
@KshitijGupta: Most probably this can be done -- how to pass external parameters to the XSLT transformation is implementation (vendor) dependent. Read the documentation of the XSLT processor that you are using and there must exist a section on this topic. If you can't find this information, ask a new question and someone would probably share how this should be done.
1

try fn:replace(string,pattern,replace) and fn:concat(string,string,...)

Returns a string that is created by replacing the given pattern with the replace argument Example: replace("Bella Italia", "l", "") Result: 'Be*a Ita*ia'

Example: replace("Bella Italia", "l", "") Result: 'Bea Itaia'

fn:concat(string,string,...) Returns the concatenation of the strings Example: concat('XPath ','is ','FUN!') Result: 'XPath is FUN!'

Ref: http://www.w3schools.com/xpath/xpath_functions.asp

Note: Make sure you are using XSLT version 2.0, as the replace function is only available in XSLT version 2.0, not in version 1.0

Comments

0

First your given link is not working ,

http://mycompany.com?mypage=xyz&version=999.

I think simply you want to transform your xml into html in java.

for that visit this discussion. suggestions-for-a-java-based-templating-engine

This is done by template engine, widely used template engines in java are Freemarker and velocity. others are also available.

Here is list of Java Template Engines. Java Template Engines

Comments

0

If you want to write the & characters in the URL as it is instead of &amp; use disable output escaping. Use concat function to append anything else you want to the url.

<xsl:value-of select="$url" disable-output-escaping="yes"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.