Using a link in XSL Files - xslt

I am completely new to XML so hopefully my explanations will make sense..
I am using an XSL file with 2 sections of HTML text, reading from 2 templates set up within the file.
The first text and associated template is a table, and I have set up a link within that. I want the link to then point to a picture that is part of the 2nd HTML text/template.
The link in the table is setup in that it appears as a link, underlined, etc. However it doesnt go anywhere when clicked.
The second section works fine on it's own, eg, pictures and text appears.
But what I cant figure out is how to actually get the link to work. I've tried numerous things but nothing has worked so far. And I-m not sure whether I am very close and just perhaps need to alter a line of code. Or whether I need to be doing something very different. Also, there are no error messages, and everything displays well, it's just the link itself that doesnt work.
<xsl:template match="portion">
<tr>
<td valign="top"><xsl:value-of select="food-description"/></td>
<td valign="top"><xsl:value-of select="food-number"/></td>
<!--the following is the link text-->
<td valign="top"><a><xsl:attribute name="href">#<xsl:value-of select="portion- photo/#file"/></xsl:attribute>
<xsl:value-of select="portion-photo/#file"/></a><br/>
</td>
</tr>
</xsl:template>
<xsl:template match="portion-photo">
<!--I know that this is the code that is not correct, however, believe it should be something similar-->
<a><xsl:attribute name="portion-photo"><xsl:value-of select="./#file"/></xsl:attribute></a>
<p><xsl:value-of select="../food-description"/>
<xsl:value-of select="./#file"/></p>
<img>
<xsl:attribute name="src"><xsl:value-of select="./#file"/></xsl:attribute>
<xsl:attribute name="width"><xsl:value-of select="ceiling(./#w div v2)"/></xsl:attribute>
<xsl:attribute name="height"><xsl:value-of select="ceiling(./#h div 2)"/></xsl:attribute>
</img>
</xsl:template>

Something like the following should work. Just add the missing name attribute to the anchor element:
<xsl:template match="portion">
...
<a href="#{portion-photo/#file}">
<xsl:value-of select="portion-photo/#file"/>
</a>
...
</xsl:template>
<xsl:template match="portion-photo">
<a name="{#file}">
<xsl:value-of select="#file"/>
</a>
</xsl:template>
However, you have to ensure that #file evaluates to a valid anchor name. If the values of all file attributes are unique, you could also create save IDs with generate-id():
<xsl:template match="portion">
...
<a href="#{generate-id(portion-photo)}">
<xsl:value-of select="portion-photo/#file"/>
</a>
...
</xsl:template>
<xsl:template match="portion-photo">
<a name="{generate-id()}">
<xsl:value-of select="#file"/>
</a>
</xsl:template>

Related

xslt matching and stripping

I have a XML structure like this, just some valid XML structure mixed with HTML tags. I am trying to match <p>MyHeader</p> in the section and set it to empty.
That is after running the XSLT on this structure, i don't want to print the <p>MyHeader</p> tag.
<abstract>
<section>
<p>MyHeader</p>
<p> other content in section </p>
<h1> other content in section </h1>
</section>
</abstract>
Here's what I am trying in the XSL
<xsl:template match="abstract/section/p">
<xsl:choose>
<xsl:when test="text() ='MyHeader'"></xsl:when>
<xsl:otherwise><xsl:apply-templates /></xsl:otherwise>
</xsl:choose>
</xsl:template>
any ideas on what's wrong with my code above? I dont see <p>MyHeader</p> tag being stripped out.
<xsl:template match="abstract/section/p">
<xsl:choose>
<xsl:when test="text() ='MyHeader'"></xsl:when>
<xsl:otherwise><xsl:apply-templates /></xsl:otherwise>
</xsl:choose>
</xsl:template>
what's wrong with my code
Nothing wrong with the code shown -- the problem is in the code you haven't shown to us.
Guessing:
The template isn't selected for execution.
There is an explicit <xsl:copy-of> selecting this p element.
Recommendation: just use:
<xsl:template match="abstract/section/p[.='MyHeader']"/>

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>

Umbraco XSLT select newsitem sub nodes regardless of currentpage

I need to say; I'm pretty green in xslt so, most likely, that's the main problem; nonetheless been on it for hours and can't get it.
I want to fill a column on my main template with the 5 most recent newsitems. These newsitems should be shown regardless of the currentpage.
I've tried this:
<xsl:template match="/">
<xsl:for-each select="umbraco.library:GetXmlNodeById(1075)/child::node">
<p>
<strong>
<xsl:value-of select="header"/>
</strong>
</p>
</xsl:for-each>
</xsl:template>
Where, at the moment, 1075 is my News template. Ive tried it with just: GetXmlNodeById(1076)/node (where 1076) is my NewsItem template. I've tried it with the node-Id's from the content-tree, but no luck..
Anyone able to help me out here? Am stuck and I've searched high and low on Google, the forums and documentation, but I'm most likely missing something vital here. TIA!
P.S. Using Umbraco 4.5 BTW
This should output current and child nodes.
<xsl:copy-of select="umbraco.library:GetXmlNodeById(1075)"/>
In Umbraco 4.5 the schema has changed, from /node[#nodeTypeAlias='News'] to /News [#isDoc]
http://blog.leekelleher.com/2010/04/02/working-with-xslt-using-new-xml-schema-in-umbraco-4-1/
So your xslt should look like
<xsl:template match="/">
<ul>
<xsl:for-each select="umbraco.library:GetXmlNodeById(1075)/News [#isDoc]">
<li><xsl:value-of select="#nodeName"/></li>
</xsl:for-each>
</ul>
</xsl:template>

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