I’m working on a site In that site some pages gets data from XML through XSLT. But the date is displayed as YYYY-MM-DD which ideally is taken from the XML which was in this format. I would like to convert this format to DD-MM-YYYY through XSLT or some other possible way.
Please suggest me an idea to go ahead or provide me the code to achieve this ASAP.
This is the format of xml giving
<published date="2009-09-28T07:06:00 CET" />
and i want to convert this into
<published date="28-09-2009T07:06:00 CET" />
and this is xsl file
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table class="bdr-bot" width="100%" border="0" cellspacing="0" cellpadding="0" style="clear:both">
<tr>
<th width="15%" class="bdr">Date</th>
<th class="bdr">Title</th>
</tr>
<xsl:for-each select="hexML/body/press_releases/press_release">
<xsl:if test="contains(published/#date, '2009')">
<tr>
<td valign="top">
<xsl:value-of select="substring-before(published/#date, 'T')"/>
</td>
<td valign="top">
<xsl:value-of select="headline"/>
</td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
Now tell me the solution? is this possible with fn:reverse?
If the XML is in the format YYYY-MM-DD, you should be able to use Xpath's tokenize function to split up your string where - occurs, and then reorder it. Something akin to:
<xsl:variable name="dt" value="tokenize(Date, '-')"/>
<xsl:value-of select="concat(dt[3],'-',dt[2],'-',dt[1])"/>
This is just off the top of my head (and untested), but you get the general idea. You should be able to split up the date and reorder the pieces.
Assuming
<xml>
<date>2009-11-18</date>
</xml>
This XSLT 1.0 solution would do it:
<xsl:template match="date">
<xsl:copy>
<xsl:value-of select="
concat(
substring(., 9, 2),
'-',
substring(., 6, 2),
'-',
substring(., 1, 4)
)
" />
</xsl:copy>
</xsl:template>
If your date can be
<xml>
<date>2009-11-1</date>
</xml>
you would have to use the slightly more complicated
<xsl:template match="date">
<xsl:copy>
<xsl:value-of select="
concat(
substring-after(substring-after(., '-'), '-'),
'-',
substring-before(substring-after(., '-'), '-'),
'-',
substring-before(., '-')
)
" />
</xsl:copy>
</xsl:template>
you could also use a template.
<?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>
<table class="bdr-bot" width="100%" border="0" cellspacing="0" cellpadding="0" style="clear:both">
<tr>
<th width="15%" class="bdr">Date</th>
<th class="bdr">Title</th>
</tr>
<!-- <xsl:for-each select="hexML/body/press_releases/press_release">-->
<xsl:if test="contains(published/#date, '2009')">
<tr>
<td valign="top">
<xsl:call-template name="FormatDate">
<xsl:with-param name="DateTime" select="published/#date"/>
</xsl:call-template>
</td>
<td valign="top">
<a href="result-page.aspx?ResultPageURL={location/#href}">
<xsl:value-of select="headline"/>
</a>
</td>
</tr>
</xsl:if>
<!--</xsl:for-each>-->
</table>
</body>
</html>
</xsl:template>
<xsl:template name="FormatDate">
<xsl:param name="DateTime"/>
<xsl:value-of select="substring($DateTime,9,2)"/>-<xsl:value-of select="substring($DateTime,6,2)"/>-<xsl:value-of select="substring($DateTime,1,4)"/><xsl:text> CET</xsl:text>
</xsl:template>
</xsl:stylesheet>
It appears that you need to use an XSLT 2.0 schema aware processor to get built-in support for what you want to do with the xs:dateTime data type and the format-date function.
See http://www.w3.org/TR/xmlschema-2/#dateTime for the requirements for XSLT 2.0 being able to parse the string you have.
The ·lexical space· of dateTime
consists of finite-length sequences of
characters of the form: '-'? yyyy '-'
mm '-' dd 'T' hh ':' mm ':' ss ('.'
s+)? (zzzzzz)?
See http://www.dpawson.co.uk/xsl/rev2/dates.html#d16685e16 for generating output.
format-date( xs:date(
concat(
substring($d,1,4),
'-',
substring($d,7,2),
'-',
substring($d,5,2))),
'[D01] [MNn] [Y0001]')
Related
I'm getting the error "Server Error in '/' Application. Input string was not in a correct format." when viewing a page from Umbraco that has the XSLT below inserted.
I think it is being caused by trying to convert a string to a number in this XSLT file. Probably from this line:
<xsl:if test="string-length($rteLinkText) != 0 and string($rteLink) = number($rteLink)">
If I remove the XSLT from the page it renders fine.
I just wanted to check with anyone who knows XSLT that I wasn't missing anything obvious.
Thanks for taking a look!
Full code of the XSLT below:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp " ">
]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:umbraco.library="urn:umbraco.library"
exclude-result-prefixes="umbraco.library ">
<xsl:import href="../xslt/globalFunctions.xslt" />
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:param name="currentPage"/>
<xsl:param name="rteImage" select="/macro/rteImage" />
<xsl:param name="rteText" select="/macro/rteText" />
<xsl:param name="rteAuthor" select="/macro/rteAuthor" />
<xsl:param name="rteLinkText" select="/macro/rteLinkText" />
<xsl:param name="rteLink" select="/macro/rteLink" />
<xsl:param name="rteDonationText" select="/macro/rteDonationText" />
<xsl:param name="rteDonationLinkText" select="/macro/rteDonationLinkText" />
<xsl:param name="rteDonationLink" select="/macro/rteDonationLink" />
<xsl:template match="/">
<blockquote class="quote">
<xsl:variable name="imageRaw">
<xsl:call-template name="getImageUrlfromXML">
<xsl:with-param name="mediaItemXML" select="$rteImage/Image"/>
<xsl:with-param name="height" />
<xsl:with-param name="width" select="'100'" />
</xsl:call-template>
</xsl:variable>
<xsl:if test="string-length($imageRaw) != 0">
<img class="thumbImg" src="{$imageRaw}" />
</xsl:if>
<xsl:if test="string-length($rteText) != 0">
<p>
<xsl:value-of select="$rteText"/>
</p>
</xsl:if>
<xsl:if test="string-length($rteAuthor) != 0">
<h4 class="quotee">
<xsl:value-of select="$rteAuthor"/>
</h4>
</xsl:if>
<xsl:if test="string-length($rteLinkText) != 0 and string($rteLink) = number($rteLink)">
<a class="quoteLink" href="{umbraco.library:NiceUrl($rteLink)}">
<xsl:value-of select="$rteLinkText"/>
</a>
</xsl:if>
<xsl:if test="string-length($rteDonationText) != 0 or (string-length($rteDonationLinkText) != 0 and string-length ($rteDonationLink) != 0)">
<div class="helpUs">
<xsl:if test="string-length($rteDonationText) != 0">
<h2>
<xsl:value-of select="$rteDonationText"/>
</h2>
</xsl:if>
<xsl:if test="string-length($rteDonationLinkText) != 0 and string-length($rteDonationLink) != 0">
<a href="{$rteDonationLink}" target="_blank" class="donateBtn">
<xsl:value-of select="$rteDonationLinkText"/>
</a>
</xsl:if>
</div>
</xsl:if>
</blockquote>
</xsl:template>
</xsl:stylesheet>
I think the issue is this part of your logic:
string($rteLink) = number($rteLink)
those two will never be equal, as they're different types (one numeric and one string)? It's also possible that the $rteLink variable can't be cast to a number.
Should it not be something like:
<xsl:if test="string-length($rteLinkText) != 0 and string-length($rteLink) != 0 and number($rteLink) > 0">
If i want XSLTransform-1 policy to get executed only when the URL has "tennis" keyword in it,how can i do it using Java Script policy in apigee?
URL: http://shaleen-test.apigee.net/v1/espn--api/tennis/athletes/296? apikey=rgnmd3naaw2qwv79fdtjgz77
Policy:XSLTransform-1
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XSL async="false" continueOnError="false" enabled="true" name="xsltransform-1">
<DisplayName>XSLTransform-1</DisplayName>
<FaultRules/>
<Properties/>
<Source>response</Source>
<ResourceURL>xsl://xsltransform-1</ResourceURL>
<Parameters ignoreUnresolvedVariables="true"/>
<OutputVariable>abc</OutputVariable>
</XSL>
XSL File:xsltransform-1
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
<xsl:template match="/">
<html>
<body>
<h1>PROFILE OF PLAYERS</h1>
<h2> <xsl:value-of select="Root/sports/name" /> </h2>
<h3>Report Details</h3>
<table border="1">
<tr bgcolor="#9acd32">
<th>PLAYER ID</th>
<th>FIRST NAME</th>
<th>LAST NAME</th>
<th>FULL NAME</th>
<th>SHORT NAME</th>
</tr>
<xsl:for-each select="Root/sports/leagues/athletes">
<tr>
<td><xsl:value-of select="id" /></td>
<td><xsl:value-of select="firstName" /></td>
<td><xsl:value-of select="lastName" /></td>
<td><xsl:value-of select="fullName" /></td>
<td><xsl:value-of select="shortName" /></td>
<!--<td><xsl:value-of select="Root/sports/leagues/athletes/id" /></td>
<td><xsl:value-of select="Root/sports/leagues/athletes/firstName" /></td>
<td><xsl:value-of select="Root/sports/leagues/athletes/lastName" /></td>
<td><xsl:value-of select="Root/sports/leagues/athletes/fullName" /></td>
<td><xsl:value-of select="Root/sports/leagues/athletes/shortName" /></td>-->
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
This can be achieved by have a conditional execution of the XSLT.You can use extract variables to extract a particular value from the request path.And if that variable's value is 'tennis' you can execute the XSLT Policy.
Is there any specific reason why you want to control the flow from Javascript?
I Have an XML something like this
<NavigatorItems>
<Navigator Name="Product">
<ModifierName>Product1</ModifierName>
<ModifierLink>www.Product1.com</ModifierLink>
<ModifierName>Product2</ModifierName>
<ModifierLink>www.Product2.com</ModifierLink>
<ShowAll>www.ProductMain.com</ShowAll>
</Navigator>
<Navigator Name="Article">
<ModifierName>Article1</ModifierName>
<ModifierLink>www.Article1.com</ModifierLink>
<ModifierName>Article2</ModifierName>
<ModifierLink>www.Article2.com</ModifierLink>
<ShowAll>www.ArticleMain.com</ShowAll>
</Navigator>
</NavigatorItems>
I Need to show something like this:
I tried the following XSLT but it throws some error (mismatch):
XML Parsing Error: mismatched tag. Expected: </ModifierName>
My Code:
<xsl:for-each select="NavigatorItems/Navigator">
<xsl:variable name="link" select="ModifierLink"/>
<tr>
<td><a href ="{$link}"><xsl:value-of select="ModifierName"/></td>
</tr>
<xsl:test select="ShowAll">
<xsl:variable name="linkShowAll" select="ShowAll"/>
<tr> <td> <a href="{$linkShowAll}"> View More Results <td> </tr>
</xsl:test>
</xsl:for-each>
Where Am I going wrong ? Please Suggest...
There were a number of problems with your code. I think I've fixed them all, but let me know if you have any problems with this:
<?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:output method="html" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<xsl:for-each select="NavigatorItems/Navigator">
<xsl:variable name="link" select="ModifierLink"/>
<tr>
<td>
<a><xsl:attribute name="href"><xsl:value-of select="ModifierLink"/></xsl:attribute><xsl:value-of select="ModifierName" /></a>
</td>
</tr>
<xsl:if test select="ShowAll != ''">
<tr>
<td>
<a><xsl:attribute name="href"><xsl:value-of select="ShowAll"/>View More Results</a>
</td>
</tr>
</xsl:test>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
I am using a third party asp.net control to pull and display the latest content from the database. The control pulls the title of the latest published content using a xsl file. My issue is the title(content piece) being too long. They place I used to display has no room for about 100 characters. I need to trim(not the white spaces) end part and may be limit it to a few words or some characters. It uses the xsl file -
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<table border="0" cellspacing="0" cellpadding="0" width="100%">
<xsl:for-each select="Collection/Content">
<tr>
<td>
<a>
<xsl:attribute name="href">
<xsl:choose>
<xsl:when test="Type ='Assets' or Type = 8 ">
javascript:void window.open('showcontent.aspx?id=<xsl:value-of select="ID"/>')
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="QuickLink"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:value-of select="Title"/>
</a>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
This part, <xsl:value-of select="Title"/> is where you the title is and I need to shorten it.. (putting ... in the end perhaps)
How can I do it? Can I get this done in the xsl file itself without using JQuery? Many thanks,
<xsl:choose>
<xsl:when test="string-length(Title) > 100">
<xsl:value-of select="substring(Title, 1, 97)" />...
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="Title" />
</xsl:otherwise>
</xsl:choose>
You can use the substring(text, startingIndex, length) XSLT function:
<xsl:value-of select="substring(Title, 1, 100)"/>
Note that substring index starts from 1, instead of the usual 0.
More at http://zvon.org/xxl/XSLTreference/Output/xpathFunctionIndex.html
Note also, that .NET does not implements XSLT 2.0, only XSLT 1.0 (hence the above reference: its the list of XSLT 1.0 functions).
Limit the 20 characters
If your code likes this
<xsl:value-of select="EmpBio" disable-output-escaping="yes"/>
Just change with new one written below
<xsl:value-of select="substring(BioData, 1, 20)" disable-output-escaping="yes"/>
Note that the quotation marks are removed from the element name BioData
I'm trying to write some xsl to style an RSS feed. I need to trim the first 10 characters off the title of each item.
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/rss">
<ul>
<xsl:for-each select="channel/item">
<li><strong><xsl:value-of select="title"/>
</strong>
More</li>
</xsl:for-each>
</ul>
</xsl:template>
<xsl:template name="trimtitle">
<xsl:param name="string" select="." />
<xsl:if test="$string">
<xsl:text>Foo</xsl:text>
<xsl:call-template name="trimtitle">
<xsl:with-param name="string" select="substring($string, 10)" />
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match="title">
<xsl:call-template name="title" />
<xsl:value-of select="." />
</xsl:template>
</xsl:stylesheet>
I think you should write your substring function as this:
substring($string,1, 10)
Look at here
http://www.zvon.org/xxl/XSLTreference/Output/function_substring.html
What are you doing in your trimtitle template?
Why are you calling trimtitle recursive..?
The easiest way to show a trimmed string is with:
<xsl:value-of select="substring(title,0,10)"/>