URL Encoding in XSL in DataPower [duplicate] - xslt

I have a problem writing my XSL. I have a link with the following code:
<a>
<xsl:attribute name="href">
<xsl:value-of select="concat($myUrl,'&projectNumber=',$projectNumber)"/>
</xsl:attribute>
<xsl:attribute name="title">
<xsl:value-of select="title"/>
</xsl:attribute>
LINK
</a>
So I need to pass a variable projectNumber to the end of myUrl. This works just fine and I get ...myUrl...&projectNumber=...projectNumber... in HTML.
The problem is, that the variable projectNumber sometimes has some characters which have to be escaped in the href of my link. I tried using the XSL function str:escape-uri() in many different ways, but still no success...
For example if myUrl is www.example.com/example.aspx?a=b and projectNumber is aaaūaaa
I get href like www.example.com/example.aspx?a=b&projectNumber=aaaūaaa, but I need to get www.example.com/example.aspx?a=b&projectNumber=aaa%C5%ABaaa. (%C5%AB is the way that 'ū' is escaped) Any suggestions? Thanks.

If you are using XSLT 1.0,
then refer this.
In case of XSLT 2.0, you may use this to solve the issue.
<xsl:value-of select="concat($myUrl,'&projectNumber=',encode-for-uri($projectNumber))"/>

Related

Assigning dynamic value to attribute in xslt file

We are working on integrating one of the server where target server required input payload in below format. In this we have to pass couple of argument and there value where one of them is dynamic.
As highlighted in below payload, for attribute storeID, we need to pass a value which should be coming from a transformation. I dont see an option through which I can do the transformation in SOA 11g
note :- I have a variable created in BPEL and same variable needs to be passed here.
Could someone pls help on this.
<modifyRequest dn="storeID=123456780,ou=store,o=xxxx.com">
<modification name="TelephoneNo" operation="replace">
<value>1231231230</value>
</modification>
This is how my xslt file look like :-
<ns6:modifyRequest>
<xsl:attribute name="dn">
<xsl:text disable-output-escaping="no">storeID=123456780,ou=sites,o=xxxx.com</xsl:text>
</xsl:attribute>
<ns6:modification>
<xsl:attribute name="name">
<xsl:text disable-output-escaping="no">TelephoneNo</xsl:text>
</xsl:attribute>
<xsl:attribute name="operation">
<xsl:text disable-output-escaping="no">replace</xsl:text>
</xsl:attribute>
<ns6:value>
<xsl:text disable-output-escaping="no">1212121212</xsl:text>
</ns6:value>
</ns6:modification>
</ns6:modifyRequest>
In addition to using xsl:text to output fixed (constant) text, you can use xsl:value-of to output text resulting from an evaluation of an XPath expression.
You can simply interleave xsl:text and xsl:value-of. Assuming for example that you have a variable named storeID:
<xsl:text>storeID=</xsl:text>
<xsl:value-of select="$storeID"/>
<xsl:text>,ou=sites,o=xxxx.com</xsl:text>
Or, alternatively, have a look at the concat XPath function for concatenating strings.
<xsl:value-of select="concat('storeID=', $storeID, ',ou=sites,o=xxxx.com')"/>

Adding html class according to xslt statement

xslt is pretty new for me. Is it possible to do something similar to my code below. I know it is possible in other template languages.
<div class="<xsl:if test="position()=1">myclass</xsl:if>">Hello</div>
You could wrap an xsl:attribute in an xsl:if...
<div>
<xsl:if test="position()=1">
<xsl:attribute name="class">myclass</xsl:attribute>
</xsl:if>
<xsl:text>Hello</xsl:text>
</div>
Also, in XSLT 2.0, you can write the xsl:attribute like this:
<xsl:attribute name="class" select="'myClass'"/>
Another XSLT 2.0 option, if you don't mind having an empty class="", is to use an if in an AVT (Attribute Value Template):
<div class="{if (position()=1) then . else ''}">...</div>
The then may vary depending on context.
It should be something like this:
<xsl:variable name="myclass" select="variablenode" />
<div class="adf">
<xsl:if test="yournode[position()=1]">
<xsl:value-of select="$myclass"/>
</xsl:if>
Hello</div>
But please give us your source XML, the XSLT you have so far and the expected output. Otherwise we can only guess.

How to dynamically assign name and href for anchor tag in xsl

I have a requirement where the xml might have one or more services (name might be different), and I am having a content which should have all of these available services from the xml something like below
<li>CMS</li>
<li>DIS</li>
but above I have hardcoded the a tag content and href since I know these are the values, but in real time I would not be knowing these names, so how to set href and anchor tag contents based on xml values?
So far I got the below for-each statement, which gets me all the service names from the xml
<xsl:variable name="number">
<xsl:number/>
</xsl:variable>
<xsl:for-each select="csmclient/product/domainmetadata/domainmetadata_service">
<li><xsl:value-of select="#name"/><xsl:value-of select="position()"/></li>
</xsl:for-each>
.
.
.
<!--far below end-->
<xsl:for-each select="domainmetadata/domainmetadata_service">
<h3>Service Name: <span style="color:#328aa4"><a name="_ser{$number}" href="#_top"><xsl:value-of select="#name"/></a></span></h3>
.
.
.
</xsl:for-each>
but it does not seem to work, it gives me all my services but the link does not work. Any other ideas?
Note: I took help from this question link which had a similar requirement.
There is a xslt function generate-id() which gives and unique textual identifier for any node in the xml.
http://www.w3.org/TR/xslt#function-generate-id
Use something like below, should work
<xsl:for-each select="csmclient/product/domainmetadata/domainmetadata_service">
<li><xsl:value-of select="#name"/></li>
</xsl:for-each>
<xsl:for-each select="domainmetadata/domainmetadata_service">
<h3>Service Name: <span style="color:#328aa4"><a name="{generate-id()}" href="#_top"><xsl:value-of select="#name"/></a></span></h3>
</xsl:for-each>
<li><xsl:value-of select="#name"/><xsl:value-of select="position()"/></li>
but it does not seem to work ...
What you are trying to write is this (not discussing at all if this uniquely identifies the node):
<li>
<a href="#_ser{$number}{#name}{position()}"/>
</li>

Hyperlinks within XSLT Templates

I am trying to create hyperlinks using XML information and XSLT templates. Here is the XML source.
<smartText>
Among individual stocks, the top percentage gainers in the S. and P. 500 are
<smartTextLink smartTextRic="http://investing.domain.com/research/stocks/snapshot
/snapshot.asp?ric=HBAN.O">Huntington Bancshares Inc</smartTextLink>
and
<smartTextLink smartTextRic="http://investing.domain.com/research/stocks/snapshot
/snapshot.asp?ric=EK">Eastman Kodak Co</smartTextLink>
.
</smartText>
I want the output to look like this, with the company names being hyperlinks based on the "smartTextLink" tags in the Xml.
Among individual stocks, the top percentage gainers in the S.&P. 500 are Eastman Kodak Co and Huntington Bancshares Inc.
Here are the templates that I am using right now. I can get the text to display, but not the hyperlinks.
<xsl:template match="smartText">
<p class="smartText">
<xsl:apply-templates select="child::node()" />
</p>
</xsl:template>
<xsl:template match="smartTextLink">
<a>
<xsl:apply-templates select="child::node()" />
<xsl:attribute name="href">
<xsl:value-of select="#smartTextRic"/>
</xsl:attribute>
</a>
</xsl:template>
I have tried multiple variations to try to get the hyperlinks to work correctly. I am thinking that the template match="smartTextLink" is not being instantiated for some reason. Does anyone have any ideas on how I can make this work?
EDIT: After reviewing some of the answers, it is still not working in my overall application.
I am calling the smartText template from within my main template
using the following statement...
<xsl:value-of select="marketSummaryModuleData/smartText"/>
Could this also be a part of the problem?
Thank you
Shane
Either move the xsl:attribute before any children, or use an attribute value template.
<xsl:template match="smartTextLink">
<a href="{#smartTextRic}">
<xsl:apply-templates/>
</a>
</xsl:template>
From the creating attributes section of the XSLT 1 spec:
The following are all errors:
Adding an attribute to an element after children have been added to it; implementations may either signal the error or ignore the attribute.
Try this - worked for me:
<xsl:template match="smartText">
<p class="smartText">
<xsl:apply-templates/>
</p>
</xsl:template>
<xsl:template match="smartTextLink">
<a>
<xsl:attribute name="href">
<xsl:value-of select="#smartTextRic"/>
</xsl:attribute>
<xsl:value-of select="text()"/>
</a>
</xsl:template>
Trick is - <xsl:attribute> first, before you do any other processing.
Marc

XSLT foreach

Hi i have a strange issue with matching specific attribute of xml node.
Example code that doesnt work:
<xsl:for-each select="../../unit/service/price/season[#name=$period_name]">
<xsl:attribute name="std_bed_price">
<xsl:value-of select="../#amount"/>
</xsl:attribute>
</xsl:for-each>
Example code that DOES work but i don't like this way too much:
<xsl:for-each select="../../unit/service/price/season">
<xsl:if test="#name = $period_name">
<xsl:attribute name="std_bed_price">
<xsl:value-of select="../#amount"/>
</xsl:attribute>
</xsl:if>
</xsl:for-each>
If in first example i replace the variable name with some of the values like 'A' it works,
i also tested what variable name is selected and it has the correct data inside (so, 'A','B','C' ...)
Anyone had this problem before?
Tnx
You might try changing it to an apply-templates instead of a foreach. Something like the following should work.
<xsl:template match="price">
<xsl:attribute name="std_bed_price">
<xsl:value-of select="#amount" />
</xsl:attribute>
</xsl:template>
And then call it like:
<xsl:apply-template select="../../unit/service/price/[season/#name=$period_name]" />
Not seen this before. Could it be an ambiguous #name attribute. Therefore try accessing it like below?
select="../../unit/service/price/season[./#name=$period_name]
Other than that, sorry, to me it looks like it should work perfectly both ways.