I am quite new to xslt, so any help will be much appreciated. Below is my sample xml file.
<DocumentElement>
<Records>
<date>2014-07-01 00:00</date>
</Records>
<Records>
<date>2014-08-03 00:00</date>
</Records>
<Records>
<date>2013-08-03 00:00</date>
</Records>
<DocumentElement>
What I need is just to select distinct years from the dates.
Currently I have the below xslt which brings duplicate years.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:ms="urn:schemas-microsoft-com:xslt">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="years" select="DocumentElement/Records/date"/>
<ul>
<xsl:for-each select="$years">
<li>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="concat('?archive=',substring( ., 1, 4))"/>
</xsl:attribute>
<xsl:value-of select="substring( ., 1, 4)"/>
</xsl:element>
</li>
</xsl:for-each>
</ul>
</xsl:template>
</xsl:stylesheet>
Which gives me the results below:
<ul>
<li>
2014
</li>
<li>
2014
</li>
<li>
2013
</li>
</ul>
But my expected result should be
<ul>
<li>
2014
</li>
<li>
2013
</li>
</ul>
I tried the following below but I get empty output
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:ms="urn:schemas-microsoft-com:xslt">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="years" select="substring(DocumentElement/Records/date, 1, 4)"/>
<ul>
<xsl:for-each select="$years[not(.=preceding::*)]">
<li>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="concat('?archive=',substring( ., 1, 4))"/>
</xsl:attribute>
<xsl:value-of select="substring( ., 1, 4)"/>
</xsl:element>
</li>
</xsl:for-each>
</ul>
</xsl:template>
</xsl:stylesheet>
Any help will be much appreciated. thanks.
Muenchian grouping is much more effective way of grouping in XSLT1.0 than using preceding/following/preceding-sibling/following-sibling axis. Try this:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="yes"/>
<xsl:key name="Date" match="DocumentElement/Records/date" use="substring(.,1,4)"/>
<xsl:template match="/">
<ul>
<xsl:for-each select="DocumentElement/Records/date[generate-id() = generate-id(key('Date', substring(.,1,4))[1])]">
<li>
<a href="{concat('?archive=',substring(.,1,4))}">
<xsl:value-of select="substring( ., 1, 4)"/>
</a>
</li>
</xsl:for-each>
</ul>
</xsl:template>
</xsl:stylesheet>
Related
I have this piece of XSLT
<xsl:template match="/">
<xsl:variable name="boxes" select="$currentPage/boxes/descendant::nodeId [text() != '']" />
<xsl:if test="count($boxes) > 0">
<div id="boxContainer">
<ul class="noList" id="frontBoxes">
<xsl:for-each select="$boxes">
<xsl:variable name="node" select="umbraco.library:GetXmlNodeById(.)" />
<xsl:variable name="imageNodes" select="umbraco.library:Split($node/descendant::boxImage, ',')" />
<xsl:for-each select="$imageNodes/descendant::value">
<xsl:variable name="image" select="umbraco.library:GetMedia(.,0)" />
<li>
<a class="boxLink">
<xsl:attribute name="href">
<xsl:value-of select="umbraco.library:NiceUrl($node/descendant::boxLink)" />
</xsl:attribute>
<xsl:value-of select="$node/#nodeName" />
</a>
<img src="{Eksponent.CropUp:Url($image/umbracoFile, 'frontBoxes')}" width="{$image/umbracoWidth}" height="{$image/umbracoHeight}" alt="" />
<xsl:value-of select="$node/descendant::bodyText" />
</li>
</xsl:for-each>
</xsl:for-each>
</ul>
</div>
</xsl:if>
</xsl:template>
If runs through a maximum and minimum of for front page boxes with an image, text and a link.
How can I get the last item so that I can output class="last" in the last <li>
I have tried with <xsl:if test="position()"> inside the last for-each but that doesn't work as intended.
use
<xsl:choose>
<xsl:when test="position()=last()">
<!-- set class="list" -->
</xsl:when>
<xsl:otherwise>
<!-- without class="list" -->
</xsl:otherwise>
</xsl:choose>
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="#..OfferLetter.xslt"?>
<Doc>
<assembly>
<Heading>Offer Letter</Heading>
</assembly>
<RefNo>Ref No:0007</RefNo>
<Date></Date>
<to>To</to>
<name></name>
<city></city>
<dear>
<a>Dear Mr.</a>
<name></name>
</dear>
<p1>
<a1>
With reference to your application and the subsequent personal interview attended by you,
we are pleased to inform that you have been selected for employment in ..
(hereinafter referred to as “Company”).
We are delighted to make you the following offer for employment.
</a1>
</p1>
</Doc>
here my xslt code for that..
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:template match="/">
<html>
<body>
<h2 style ="text-align: center;">Offer Letter</h2>
<h3 style="text-align:Right; margin-right: 110px;">Ref No:K070813</h3>
<h3 style="text-align:Right ; margin-right: 224px; ">Date:</h3>
<h3 style="text-align:Left; margin-left: 50px;">To</h3>
<h3 style="text-align:Left; margin-left: 50px;">MR.</h3>
<h3 style="text-align:Left; margin-left: 50px;">Hyderabad</h3>
<br></br>
<h3 style="text-align:Left; margin-left: 50px;">Dear Mr.</h3>
<xsl:for-each select="Doc/p1">
<h3 style="text-align:Left; margin-left: 50px;">
<xsl:value-of select="a1"/>
</h3>
</xsl:for-each>
</body>
</html>
here my TransformHRML() code
public static void TransformXML()
{
// Create a resolver with default credentials.
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;
// transform the OfferLetter.xml file to HTML
XslTransform transform = new XslTransform();
// load up the stylesheetfile:
transform.Load(HttpContext.Current.Server.MapPath("OfferLetter.xslt"));
// perform the transformation
transform.Transform(#"..\OfferLetter.xml", #"..\OfferLetter.html", resolver);
// transform the OfferLetter.xml file to comma delimited format
// load up the stylesheet
transform.Transform(HttpContext.Current.Server.MapPath("OfferLetter.xslt"), #"..\OfferLetter.html",resolver);
}
please help me..
There are a bunch of close-tag issues, and the extraneous # in the xml file as picked up by Tim.
After tidying these up, your xslt works, but I would push you to use apply-templates and not for-each:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="OfferLetter.xslt"?>
<Doc>
<assembly>
<Heading>Offer Letter</Heading>
</assembly>
<RefNo>Ref No:0007</RefNo>
<!--etc-->
<p1>
<a1>
With reference to your application and the subsequent , ...
</a1>
</p1>
<p1>
<a1>
Another Paragraph
</a1>
</p1>
</Doc>
With the XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:template match="/">
<html>
<body>
<!--Did you mean to hard code these fields? -->
<h2 style ="text-align: center;">
<xsl:value-of select="assembly/Heading"/>
</h2>
<h3 style="text-align:Right; margin-right: 110px;">
<xsl:value-of select="assembly/RefNo"/>
</h3>
<!--etc-->
<xsl:apply-templates select="Doc/p1">
</xsl:apply-templates>
</body>
</html>
</xsl:template>
<xsl:template match="p1">
<h3 style="text-align:Left; margin-left: 50px;">
<xsl:value-of select="a1"/>
</h3>
</xsl:template>
</xsl:stylesheet>
I've created a tridion web site, now i want to add some search control like Google custom search, is there any option provide by tridion for the same?
my option :
i have to create a user contol and implement Google custom search in there then render it on my pages. or any other approach'd be better.
search control (contain text box and a search button) should be able to search text.
search results should be formatted as search engines do.
When you say Google, are you talking about the Google Search Appliance (GSA)?
Assuming that is the case: You seem to have done quite a lot of work with XSLT, so in my opinion the easiest way to do thes is have your usercontrol call the XML API for the GSA with the relevant query, and simply transform the response with XSLT. This prevents you from having to recompile the control when you need layout changes, and you can publish the XSLT from Tridion.
I use the following XSLT to generate the search results on this page http://medicine.yale.edu/search.aspx?q=test&x=0&y=0&site=YSM_School_of_Medicine
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" omit-xml-declaration="yes"/>
<xsl:param name="SITE_CATALOG"/>
<xsl:param name="SITE_NAME"/>
<xsl:param name="MAX_PAGES_IN_NAVIGATION">10</xsl:param>
<xsl:param name="TOTALNUMBER" select="/GSP/RES/M"/>
<xsl:param name="QUERY" select="/GSP/PARAM[#name='q']/#value"/>
<xsl:param name="SEARCHED_SITE_CATALOG" select="/GSP/PARAM[#name='site']/#value"/>
<xsl:template match="/">
<xsl:variable name="STARTNUMBER" select="/GSP/RES/#SN"/>
<xsl:variable name="ENDNUMBER" select="/GSP/RES/#EN"/>
<xsl:variable name="TIMER" select="/GSP/TM"/>
<!--begin search results -->
<div class="google-search-form bordered-box shaded-f5 padded-20">
<xsl:call-template name="WriteSearchForm"/>
</div>
<xsl:if test="/GSP/Spelling">
<p>Did you mean <a href="?q={/GSP/Spelling/Suggestion/#q}&site={/GSP/PARAM[#name='site']/#value}">
<xsl:value-of select="/GSP/Spelling/Suggestion/#q"/>
</a>?</p>
</xsl:if>
<div id="search-results-pagination">
<xsl:for-each select="/GSP/RES/R">
<xsl:choose>
<xsl:when test="#L='2'">
<div class="grouped-search-result">
<h4>
<a href="{U}">
<xsl:value-of select="T" disable-output-escaping="yes"/>
</a>
</h4>
<p class="search-result-body">
<xsl:value-of select="S" disable-output-escaping="yes"/>
</p>
<p class="gray95">
<span class="green">
<xsl:choose>
<xsl:when test="string-length(U) > 88">
<xsl:value-of select="substring(U, 0, 88)"/>...
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="U"/>
</xsl:otherwise>
</xsl:choose>
</span>
</p>
<p>
<a href="?as_sitesearch={HN/#U}&q={$QUERY}&site={$SITE_CATALOG}" class="more-results-link">
[ <span>
More results from <xsl:value-of select="HN/#U"/>
</span> ]
</a>
</p>
</div>
</xsl:when>
<xsl:otherwise>
<div class="pagination-element search-result">
<h4><a href="{U}">
<xsl:call-template name="WriteMimeTypeIndicator">
<xsl:with-param name="RESULT" select="."/>
</xsl:call-template>
<xsl:value-of select="T" disable-output-escaping="yes"/>
</a></h4>
<p class="search-result-body">
<xsl:value-of select="S" disable-output-escaping="yes"/>
</p>
<p>
<span class="green">
<xsl:choose>
<xsl:when test="string-length(U) > 92">
<xsl:value-of select="substring(U, 0, 92)"/>...
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="U"/>
</xsl:otherwise>
</xsl:choose>
</span> - <xsl:value-of select="HAS/C/#SZ"/>
</p>
</div>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</div>
<hr class="dashed" />
<xsl:call-template name="WritePageNavigation"/>
<!--<div id="pagination-controls" class="pagination-controls float-rt"></div>-->
<!-- end pagination-controls -->
<div class="results-footer-notes">
<p class="small-gray95">Note: This search service may return results that are not approved by Yale University, or results that may not reflect the official views of Yale University.</p>
</div>
<div class="clear"></div>
<div class="google-search-form bordered-box shaded-f5 padded-20 big-margin-top">
<xsl:call-template name="WriteSearchForm"/>
</div>
<!-- end search results -->
</xsl:template>
<xsl:template name="WritePageNavigation">
<xsl:variable name="RESULTS_ON_THIS_PAGE" select="count(/GSP/RES/R)"/>
<xsl:variable name="TOTAL_RESULT_COUNT" select="/GSP/RES/M"/>
<xsl:variable name="TOTAL_PAGES" select="ceiling($TOTAL_RESULT_COUNT div $RESULTS_ON_THIS_PAGE)"/>
<xsl:variable name="CURRENT_PAGE" select="floor((/GSP/RES/#SN) div $RESULTS_ON_THIS_PAGE) + 1"/>
<xsl:variable name="START_RESULT" select="/GSP/RES/#SN"/>
<!--
Results On This Page: <xsl:value-of select="$RESULTS_ON_THIS_PAGE"/>
<br/>
Total Results: <xsl:value-of select="$TOTAL_RESULT_COUNT"/>
<br/>
Total Pages: <xsl:value-of select="$TOTAL_PAGES"/>
<br/>
Current Page: <xsl:value-of select="$CURRENT_PAGE"/>
<br/>
Start Result: <xsl:value-of select="$START_RESULT"/>
<br/>
Start Navigation Page: <xsl:value-of select="$CURRENT_PAGE - ($MAX_PAGES_IN_NAVIGATION div 2)"/>
-->
<xsl:if test="$TOTAL_PAGES > 1">
<ul class="google-search-pagination">
<xsl:if test="$CURRENT_PAGE > 1">
<li class="pagination-previous">
Previous
</li>
</xsl:if>
<xsl:call-template name="WritePaging">
<xsl:with-param name="CURRENT_PAGE" select="$CURRENT_PAGE"/>
<xsl:with-param name="NEXT_PAGE_START" select="$RESULTS_ON_THIS_PAGE + 1"/>
<xsl:with-param name="TOTAL_RESULTS" select="$TOTAL_RESULT_COUNT"/>
<xsl:with-param name="RESULTS_ON_PAGE" select="$RESULTS_ON_THIS_PAGE"/>
<xsl:with-param name="START_PAGE">
<xsl:choose>
<xsl:when test="($CURRENT_PAGE - ($MAX_PAGES_IN_NAVIGATION div 2)) > 0">
<xsl:value-of select="$CURRENT_PAGE - ($MAX_PAGES_IN_NAVIGATION div 2)"/>
</xsl:when>
<xsl:otherwise>1</xsl:otherwise>
</xsl:choose>
</xsl:with-param>
<xsl:with-param name="END_PAGE">
<xsl:choose>
<xsl:when test="($CURRENT_PAGE - ($MAX_PAGES_IN_NAVIGATION div 2)) > 0">
<xsl:value-of select="$MAX_PAGES_IN_NAVIGATION + $CURRENT_PAGE - floor($MAX_PAGES_IN_NAVIGATION div 2) - 1"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$MAX_PAGES_IN_NAVIGATION"/>
</xsl:otherwise>
</xsl:choose>
</xsl:with-param>
</xsl:call-template>
<li class="pagination-next">
Next
</li>
</ul>
</xsl:if>
</xsl:template>
<xsl:template name="WritePaging">
<xsl:param name="NEXT_PAGE_START"/>
<xsl:param name="RESULTS_ON_PAGE"/>
<xsl:param name="TOTAL_RESULTS"/>
<xsl:param name="CURRENT_PAGE"/>
<xsl:param name="START_PAGE"/>
<xsl:param name="END_PAGE"/>
<!--
NEXT_PAGE_START: <xsl:value-of select="$NEXT_PAGE_START"/><br/>
RESULTS_ON_PAGE: <xsl:value-of select="$RESULTS_ON_PAGE"/><br/>
TOTAL_RESULTS: <xsl:value-of select="$TOTAL_RESULTS"/><br/>
CURRENT_PAGE: <xsl:value-of select="$CURRENT_PAGE"/><br/>
START_PAGE: <xsl:value-of select="$START_PAGE"/><br/>
END_PAGE: <xsl:value-of select="$END_PAGE"/><br/>
-->
<li>
<xsl:if test="$CURRENT_PAGE = $START_PAGE">
<xsl:attribute name="class">selected</xsl:attribute>
<a href="#">
<xsl:value-of select="$START_PAGE"/>
</a>
</xsl:if>
<xsl:if test="$CURRENT_PAGE != $START_PAGE">
<a href="?q={/GSP/PARAM[#name='q']/#value}&start={($START_PAGE - 1)*$RESULTS_ON_PAGE}&site={/GSP/PARAM[#name='site']/#value}">
<xsl:value-of select="$START_PAGE"/>
</a>
</xsl:if>
</li>
<xsl:if test="$START_PAGE < $END_PAGE">
<xsl:if test="$NEXT_PAGE_START < $TOTAL_RESULTS">
<xsl:call-template name="WritePaging">
<xsl:with-param name="NEXT_PAGE_START" select="$NEXT_PAGE_START + $RESULTS_ON_PAGE"/>
<xsl:with-param name="TOTAL_RESULTS" select="$TOTAL_RESULTS"/>
<xsl:with-param name="RESULTS_ON_PAGE" select="$RESULTS_ON_PAGE"/>
<xsl:with-param name="START_PAGE" select="$START_PAGE + 1"/>
<xsl:with-param name="CURRENT_PAGE" select="$CURRENT_PAGE"/>
<xsl:with-param name="END_PAGE" select="$END_PAGE"/>
</xsl:call-template>
</xsl:if>
</xsl:if>
</xsl:template>
<xsl:template name="WriteSearchForm">
<form name="gs" method="GET" action="#">
<p class="wrapper">
<input type="text" name="q" maxlength="256" value="{$QUERY}" class="google-search-keywords"/>
<select name="site" class="google-search-site">
<option value="{$SITE_CATALOG}">
<xsl:if test="$SEARCHED_SITE_CATALOG = $SITE_CATALOG">
<xsl:attribute name="selected">true</xsl:attribute>
</xsl:if>
<xsl:value-of select="$SITE_NAME" disable-output-escaping="yes"/>
</option>
<option value="Yale_University">---------------</option>
<option value="YSM_School_of_Medicine">
<xsl:if test="$SEARCHED_SITE_CATALOG = 'YSM_School_of_Medicine'">
<xsl:attribute name="selected">true</xsl:attribute>
</xsl:if>
School of Medicine
</option>
<option value="Yale_Medical_Group">
<xsl:if test="$SEARCHED_SITE_CATALOG = 'Yale_Medical_Group'">
<xsl:attribute name="selected">true</xsl:attribute>
</xsl:if>
Yale Medical Group
</option>
<option value="Yale_New_Haven_Hospital">
<xsl:if test="$SEARCHED_SITE_CATALOG = 'Yale_New_Haven_Hospital'">
<xsl:attribute name="selected">true</xsl:attribute>
</xsl:if>
Yale-New Haven Hospital
</option>
<option value="Medical_Center">
<xsl:if test="$SEARCHED_SITE_CATALOG = 'Medical_Center'">
<xsl:attribute name="selected">true</xsl:attribute>
</xsl:if>
Medical Center (all)
</option>
<option value="Medical_Library">
<xsl:if test="$SEARCHED_SITE_CATALOG = 'Medical_Library'">
<xsl:attribute name="selected">true</xsl:attribute>
</xsl:if>
Medical Library
</option>
<option value="Yale_University">---------------</option>
<option value="Yale_Libraries">
<xsl:if test="$SEARCHED_SITE_CATALOG = 'Yale_Libraries'">
<xsl:attribute name="selected">true</xsl:attribute>
</xsl:if>
Yale Libraries
</option>
<option value="Finance_and_Administration">
<xsl:if test="$SEARCHED_SITE_CATALOG = 'Finance_and_Administration'">
<xsl:attribute name="selected">true</xsl:attribute>
</xsl:if>
Finance & Admin
</option>
<option value="Human_Resources">
<xsl:if test="$SEARCHED_SITE_CATALOG = 'Human_Resources'">
<xsl:attribute name="selected">true</xsl:attribute>
</xsl:if>
Human Resources
</option>
<option value="ITS">
<xsl:if test="$SEARCHED_SITE_CATALOG = 'ITS'">
<xsl:attribute name="selected">true</xsl:attribute>
</xsl:if>
Information Technology
</option>
<option value="Yale_University">
<xsl:if test="$SEARCHED_SITE_CATALOG = 'Yale_University'">
<xsl:attribute name="selected">true</xsl:attribute>
</xsl:if>
Yale University
</option>
</select>
<button type="submit" value="submit" class="google-search-button theme-background">
<img src="/files/images/button_search_icon_translucent_sm.png" />Google Search
</button>
</p>
<p class="no-space-bottom float-rt" style="margin-right:50px;">
<a href="http://www.yale.edu/its/web/search/tips.html" class="search-tips">
<img src="/files/images/icon_question.gif" alt="" style="vertical-align:middle" /> Search Tips
</a>
</p>
<p class="no-space-bottom gray95">
Results <span class="green">
<xsl:value-of select="/GSP/RES/#SN"/>
</span> - <span class="green">
<xsl:value-of select="/GSP/RES/#EN"/>
</span> of about <span class="green"><xsl:value-of select="$TOTALNUMBER"/></span> for <strong><xsl:value-of select="$QUERY"/></strong>
</p>
</form>
</xsl:template>
<xsl:template name="WriteMimeTypeIndicator">
<xsl:param name="RESULT"/>
<xsl:choose>
<xsl:when test="not($RESULT/#MIME)"></xsl:when>
<xsl:when test="$RESULT/#MIME = 'application/msword'">[DOC] </xsl:when>
<xsl:when test="$RESULT/#MIME = 'application/octet-stream'">TEST OPTIONS</xsl:when>
<xsl:when test="$RESULT/#MIME = 'application/pdf'">[PDF] </xsl:when>
<xsl:otherwise>TODO:<xsl:value-of select="$RESULT/#MIME"/></xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
If I select a DIV tag in a XHTML file with XSLT, like //*[#id='background'] How do I add a style, like a background color or other CSS styles like borders to the DIV? And if I have a list inside the DIV ID=background, how can I style the list, like removing the bullets? :)
This is really easy with XSLT. For instance, your input is:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title></title>
</head>
<body>
<div id="background">
<ul style="list-style-type: bullet">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
</div>
</body>
</html>
You can use the identity transform to copy the input XML as is, and override the nodes of interest:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:x="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="x">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*" />
</xsl:copy>
</xsl:template>
<xsl:template match="x:div[#id='background']">
<xsl:copy>
<xsl:attribute name="style">
<xsl:text>border-style:solid;border-width:medium</xsl:text>
</xsl:attribute>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="x:ul[ancestor::*
[name()='div' and #id='background']]/#style">
<xsl:attribute name="style">
<xsl:text>list-style-type: none</xsl:text>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
The output will be:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title></title>
</head>
<body>
<div style="border-style:solid;border-width:medium" id="background">
<ul style="list-style-type: none">
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
</div>
</body>
</html>
Any help much appreciated on this.
I'd like to remove certain nodes from being published in a feed from an external xml file, which I've styled with xslt. Here is the feed:http://www.wcwg.info/feeds/localevents.aspx?a=00392&p=CM159EH&m=20.
The nodes I would like to remove are:
Local Events posted to WhereCanWeGo.com.00392CM15
9EH31/10/1007/11/1010001111111111111111111031
October
2010http://www.wherecanwego.com/events/signin.aspxww.wherecanwego.com/events/signin.aspx
Could anybody be kind enough to direct me how to remove these initial nodes (parameters)? They are the postcode, account number, feed URL etc.
I'm desperate to get this finished, but it's the final hurdle! Many thanks in advance for anyone who responds...
The stylesheet (fragment)
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="item">
<div class="local_events">
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="Venue"/>
<xsl:apply-templates select="Times"/>
<xsl:apply-templates select="Dates"/>
<xsl:apply-templates select="DetailsURL"/>
</div><div style="height:1px;border-bottom:1px dotted #cfcfcf;"></div>
</xsl:template>
<xsl:template match="title">
<h2><a class="title" target="_blank" rel="nofollow" href="{../DetailsURL}"><xsl:value-of select="."/></a></h2>
</xsl:template>
<xsl:template match="Venue">
<span>Location: </span>
<xsl:value-of select="."/>
<br />
</xsl:template>
<xsl:template match="Times">
<span>Details: </span>
<xsl:value-of select="."/>
<br />
</xsl:template>
<xsl:template match="Dates">
<span>Dates: </span>
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="DetailsURL">
<a style="font-weight:normal;margin-left:0.5em;" target="_blank" rel="nofollow" href="{.}"><xsl:text>Full details...</xsl:text></a>
</xsl:template>
From your poorly formulated question and analyzing the feed, it seems that you want to get rid of all children of the top (LocalEvents) node that are not named item.
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:strip-space elements="*"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*/*[not(self::item)]"/>
</xsl:stylesheet>
when applied on the provided feed (http://www.wcwg.info/feeds/localevents.aspx?a=00392&p=CM159EH&m=20), produces the wanted result.
Do note: how the wanted elements are deleted (not processed, ignored) by an empty template matching them and overriding the identity rule that is used to copy the rest of the nodes "as-is".
If you already use an XSLT stylesheet that does not do exactly what you want then please post the stylesheet or a link to it.
[edit]
In your posted stylesheet change
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
to
<xsl:template match="/">
<xsl:apply-templates select="//item"/>
</xsl:template>
This has now been corrected to the following which now works, for anyone else in the same situation:
<xsl:template match="/">
<xsl:apply-templates select="/LocalEvents/item"/>
</xsl:template>
<xsl:template match="item">
<div class="local_events">
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="Venue"/>
<xsl:apply-templates select="Times"/>
<xsl:apply-templates select="Dates"/>
<xsl:apply-templates select="DetailsURL"/>
</div><div style="height:1px;border-bottom:1px dotted #cfcfcf;"></div>
</xsl:template>
<xsl:template match="title">
<h2><a class="title" target="_blank" rel="nofollow" href="{../DetailsURL}"><xsl:value-of select="."/></a></h2>
</xsl:template>
<xsl:template match="Venue">
<span>Location: </span>
<xsl:value-of select="."/>
<br />
</xsl:template>
<xsl:template match="Times">
<span>Details: </span>
<xsl:value-of select="."/>
<br />
</xsl:template>
<xsl:template match="Dates">
<span>Dates: </span>
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="DetailsURL">
<a style="font-weight:normal;margin-left:0.5em;" target="_blank" rel="nofollow" href="{.}"><xsl:text>Full details...</xsl:text></a>
</xsl:template>
</xsl:stylesheet>
Other approuch. This stylesheet:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:preserve-space elements="Times"/>
<xsl:template match="text()"/>
<xsl:template match="item">
<div class="local_events">
<xsl:apply-templates/>
</div>
<div style="height:1px;border-bottom:1px dotted #cfcfcf;"></div>
</xsl:template>
<xsl:template match="title">
<h2>
<a class="title" target="_blank" rel="nofollow" href="{../DetailsURL}">
<xsl:value-of select="."/>
</a>
</h2>
</xsl:template>
<xsl:template match="Venue">
<span>Location: </span>
<xsl:value-of select="."/>
<br />
</xsl:template>
<xsl:template match="Times">
<span>Details: </span>
<xsl:value-of select="."/>
<br />
</xsl:template>
<xsl:template match="Dates">
<span>Dates: </span>
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="DetailsURL">
<a style="font-weight:normal;margin-left:0.5em;" target="_blank" rel="nofollow" href="{.}">
<xsl:text>Full details...</xsl:text>
</a>
</xsl:template>
</xsl:stylesheet>
Note: When doing a one to one transformation in the same hierarchy, it's enough to declare the rules for specific elements in input source and to overwrite the built-in rule for text node (output the string value) with an empty rule.