How to generate link with XSLT - xslt

Hi i've made a php webservice that returns some xml which is transformed into html by an XML file i have . But i want to be able to click on each returned item to get more details about that item. <?php echo $itemname"?>
Recently i did the same thing but in PHP, ive tried to use this in XSLT but it doesn't work.

Use xsl:attribute:
<a>
<xsl:attribute name="href">item.php?id=<xsl:value-of select="ItemId" /></xsl:attribute>
<xsl:value-of select="ItemName" />
</a>
Alternatively, the shorter form:
<xsl:value-of select="ItemName" />
Should also work

Related

URL Encoding in XSL in DataPower [duplicate]

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))"/>

Xslt: populate innertext value of <a href="" /> in xslt during runtime

I tried a lot to see if any suggestion/answer would satisfy my requirement but could not find any.
I have an Xml file which I am using to get an output Html file using XslTransform API and an Xslt stylesheet.
My xml file has a rootnode which will have two attributes - HyperlinkDisplayText and HyperlinkValue. This xml file is generated at runtime. So, the values of these attributes are not known at compile time.
My requirement is that the html output should display a hyperlink whose display text needs to be taken from the value of 'HyperlinkDisplayText' and the hyperlink value needs to be taken from the value of "HyperlinkValue'.
For example,
<RootNode HyperlinkDisplayText="Google" HyperlinkValue="https://www.google.com/" />
This needs to be appears as Google.
For this, I tried several things in my xslt. Below is a snapshot what I am trying in my xslt file -
<xsl:choose>
<xsl:when test="#HyperlinkDisplayText and #HyperlinkValue">
<h4 style="font-family: arial" align='center'>
#HyperlinkDisplayText
</h4>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
It is not working. Any help is highly appreciated.
Regards,
kvk938
Change #HyperlinkDisplayText to <xsl:value-of select="#HyperlinkDisplayText"/>. That assumes the context node is a RootNode element.

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>

XSL Output Text

<xsl:for-each select="$all_events[g:active = true()][g:body/g:current = true()]">
<xsl:for-each select="g:body">
<h2 class="normal"><xsl:value-of select="g:sub_title" /></h2>
<xsl:for-each select="g:paragraphs">
<xsl:text><xsl:value-of select="g:paragraph" /></xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
Here is my XSL, take notice to the following line:
<xsl:text><xsl:value-of select="g:paragraph" /></xsl:text>
I tried this because the g:paragraph is coming from a WYSIWYG and it was printing out the <p> </p> tags and whatever else. This process of encapsulating it within xsl:text tags caused an error. What is the proper way to either hide the tags (because I want the styles to still be applied if included (i.e. bold, underlined)?
Edit:
The output currently is <p>whatever</p>
I want it to be whatever
Ah, I figured it out. I just had to use disable-output-escaping="yes"
You want to select the text content from a p element inside the g:paragraph element. You can do that this way:
<xsl:for-each select="g:paragraphs">
<xsl:value-of select="g:paragraph/p" />
</xsl:for-each>