I am making a website using a CMS, in the home page I need to get some Pictures and discriptions randomly,, I need each time i refresh the page to get new nodes.
This is how I am calling the nodes in XSL:
<xsl:for-each select ="TSPRoot/Blocks/Block[#ID=134]/Articles/Article[position() < 4]">
<div class="layout-08">
<a class="title" href="detailed.aspx?id={ID}">
<xsl:choose >
<xsl:when test ="string-length(ImageURL) > 0">
<img src="_handlers/resizeimage.ashx?src={ImageURL}&height=149&width=206" title="{Headline}"/>
</xsl:when>
<xsl:otherwise>
<img src="_frontend/ux/images/en_logo.png" width="206" height="149" title="{Headline}"/>
</xsl:otherwise>
</xsl:choose>
</a>
<div class="bg">
<xsl:value-of select ="Headline"/>
</div>
</div>
</xsl:for-each>
This gets the newest (3) nodes, I need random (3) nodes each time i refresh.
I would give you only some ideas of possible solution.
Technically XSLT doesn't offer a method random numbers. However, this problem can be solved in at least three different ways:
Generate pseudo-random number in Java/C#/PHP code before executing XSLT template and pass the generated number as an XSLT parameter.
Use the extension function: http://exslt.org/random/functions/random-sequence/index.html
Use current-dateTime() (XSLT 2.0) function as a seed for your random number procedure. With a couple of string and math operations you could convert it into desired quasi-random number. I think that for your needs this solution is enough.
EDIT - Ad 3.
I have made some experiments with this idea. And I have created some sample code. I know that this code generate a sequence of really pseudo random numbers, but for simple web page like in the question it might be enough.
The following XSLT:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:rnd="http://whatever.org/random">
<xsl:output indent="yes" />
<xsl:function name="rnd:pseudoRandomInternal">
<xsl:param name="numSeed" as="xs:decimal"/>
<xsl:variable name="squareSeed" select="xs:string($numSeed*$numSeed)"/>
<xsl:variable name="oneDivSeed" select="substring($squareSeed, 3, string-length($squareSeed) - 3)"/>
<xsl:variable name="cc" select="if (string-length($oneDivSeed) > 6) then
substring($oneDivSeed, string-length($oneDivSeed) - 6) else
$oneDivSeed"/>
<xsl:value-of select="xs:decimal($cc)"/>
</xsl:function>
<xsl:function name="rnd:pseudoRandom" as="xs:decimal*">
<xsl:param name="range" as="xs:integer"/>
<xsl:choose>
<xsl:when test="$range = 1">
<xsl:variable name="seed" select="substring(xs:string(current-time()), 1, 12)" as="xs:string"/>
<xsl:variable name="numSeed" select="xs:decimal(translate($seed, ':.+-', ''))"/>
<xsl:sequence select="(rnd:pseudoRandomInternal($numSeed))" />
</xsl:when>
<xsl:otherwise>
<xsl:variable name="subSequence" select="rnd:pseudoRandom($range - 1)"/>
<xsl:variable name="newValue" select="rnd:pseudoRandomInternal($subSequence[1])" as="xs:decimal*"/>
<xsl:sequence select="($newValue, $subSequence)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xsl:template match="/">
<xsl:variable name="rr" select="rnd:pseudoRandom(10)" as="xs:decimal*"/>
<output>
<xsl:for-each select="$rr">
<item>
<xsl:value-of select=". * 3 mod 11" />
</item>
</xsl:for-each>
</output>
</xsl:template>
</xsl:stylesheet>
generated the following output:
<output>
<item>10</item>
<item>9</item>
<item>6</item>
<item>9</item>
<item>9</item>
<item>10</item>
<item>3</item>
<item>5</item>
<item>9</item>
<item>4</item>
</output>
Related
I got a question, I try to convert an XML to JSON using XSLT version 1.0.
As far as the name goes I get it right but the value is another story.
<datapost>
<fields>
<field>
<id>emailId</id>
<name>emailName</name>
<values>
<value>info#example.com</value>
</values>
</field>
</fields>
AS IT CURRENTLY IS:
At the moment I get only the "name" correct but the "value" (emailIdName & emailId & info#example.com) is all the values squashed together what I obviously don't want.
{
"emailName":{
"emailIdemailNameinfo#example.com"
}
}
EXPECTED TO BE:
I want to get only the "name" and the "value" in values (info#example.com)
This is the result that I WANT to get:
{
"emailName":{
"info#example.com"
}
}
This is the code I use:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="no" encoding="UTF-16" omit-xml-declaration="yes"/>
<xsl:template match="/">
<body>{
<xsl:call-template name="fieldsName"></xsl:call-template>
<xsl:text>
</xsl:text>
</body>
</xsl:template>
<xsl:template name="fieldsName">
<xsl:for-each select="//datapost[position()=1]/fields/field">
"
<xsl:value-of select="name"/>" :
<xsl:call-template name="fieldsValue"/>}
</xsl:for-each>
</xsl:template>
<!-- Array Element -->
<xsl:template match="*" mode="ArrayElement">
<xsl:call-template name="fieldsValue"/>
</xsl:template>
<!-- Object Properties -->
<xsl:template name="fieldsValue">
<xsl:variable name="childName" select="//datapost[position()=1]/fields/field/values/value"/>
<xsl:choose>
<xsl:when test="not(*|#*)">"
<xsl:value-of select="."/>"
</xsl:when>
<xsl:when test="count(*[name()=$childName]) > 1">{ "
<xsl:value-of select="$childName"/>" :[
<xsl:apply-templates select="*" mode="ArrayElement"/>] }
</xsl:when>
<xsl:otherwise>{
<xsl:apply-templates select="#*"/>
<xsl:apply-templates select="*"/>
}
</xsl:otherwise>
</xsl:choose>
<xsl:if test="following-sibling::*">,</xsl:if>
</xsl:template>
<!-- Attribute Property -->
<xsl:template match="#*">"
<xsl:value-of select="name"/>" : "
<xsl:value-of select="."/>",
</xsl:template>
</xsl:stylesheet>
The element names in your XML are poorly chosen, and this has probably confused you (it certainly confused me). One would expect "field" to contain a single field, but it actually contains all of them. So change
<xsl:for-each select="//datapost[position()=1]/fields/field">
to
<xsl:for-each select="//datapost[position()=1]/fields/field/*">
or better still,
<xsl:for-each select="datapost/fields/field/*">
since the other parts of the expression are redundant verbiage.
Then you need to look at the template containing the variable
<xsl:variable name="childName" select="//datapost[position()=1]/fields/field/values/value"/>
This is selecting all the values in the document, not just the values of the current element. I'm not entirely sure what you're trying to achieve here, and it doesn't seem to be covered by your test data, but I suspect you're trying to do some kind of grouping of elements that have the same name. For that you need to use grouping facilities: xsl:for-each-group in XSLT 2.0+, Muenchian grouping in XSLT 1.0. You haven't said which XSLT version you're using, but all of this would be an awful lot easier if you used XSLT 2.0+.
I got a xml that I need to turn into a json.
I'm mostly fine except with a base64 multiline
<file>TU0...AAA
FOO...BCD
FOO...012
FOO...ZYX</file>
In json multiline is not possible, this should be rewritten in 1 line only as
"file":"TU0...AAA\nFOO...BCD\nFOO...012\nFOO...ZYX"
With "real" two-char string "\n" to concatenate each line.
Can I do that in xslt 1.0 ?
I know I can use translate but that's for one char only.
I'll try
translate(.,'
',' ')
This will replace returns by space and maybe this won't break the base64 decoding of the json.
But, if i want to do it the "right way", I guess I'll need custom funcs.
In my case returns seems to be "
".
But if someone comes with a solution that works with all combinations (
) that would be great.
My primary target is chrome web browser but running fine in all browsers would be great.
If you just want to get rid of the linefeeds you could use the normalize-space($string)function, e.g.:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
exclude-result-prefixes="xd"
version="1.0">
<xd:doc scope="stylesheet">
<xd:desc>
<xd:p><xd:b>Created on:</xd:b> Apr 22, 2020</xd:p>
<xd:p><xd:b>Author:</xd:b> bwb</xd:p>
<xd:p>generates a normalized text output of the file element</xd:p>
</xd:desc>
</xd:doc>
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:apply-templates select="file"/>
</xsl:template>
<xsl:template match="file">
<xsl:value-of select="normalize-space(.)"/>
</xsl:template>
</xsl:stylesheet>
You could then still replace the whitespaces by something else( forJSON maybe with ,)
If you definitely want the \nthough, you could try the following stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
exclude-result-prefixes="xs math xd"
version="1.0">
<xd:doc scope="stylesheet">
<xd:desc>
<xd:p><xd:b>Created on:</xd:b> Apr 22, 2020</xd:p>
<xd:p><xd:b>Author:</xd:b> bwb</xd:p>
<xd:p>Override default text() template by adding a search and replace funtionality</xd:p>
</xd:desc>
</xd:doc>
<xsl:output method="text"/>
<xd:doc scope="component">
<xd:desc>The string that should be searched and replaced by $param-replaceString</xd:desc>
</xd:doc>
<xsl:param name="param-searchString" select="'
'"/><!-- actually you also wnat to replace the whitespaces, that's why the searchString looks so strange -->
<xd:doc>
<xd:desc>The string that replace any occurence of $param-searchString</xd:desc>
</xd:doc>
<xsl:param name="param-replaceString" select="'\n'"/>
<xd:doc scope="component">
<xd:desc>Override for default text() template testing for $param-searchString presence and calling replace template</xd:desc>
</xd:doc>
<xsl:template match="text()">
<xsl:choose>
<xsl:when test="contains(., $param-searchString)">
<xsl:call-template name="replace">
<xsl:with-param name="InputString" select="."/>
<xsl:with-param name="searchString" select="$param-searchString"/>
<xsl:with-param name="replaceString" select="$param-replaceString"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="replace">
<xsl:param name="InputString"/>
<xsl:param name="searchString"/>
<xsl:param name="replaceString"/>
<xsl:choose>
<xsl:when test="contains($InputString, $searchString)">
<xsl:variable name="token-before-first-match" select="substring-before($InputString, $searchString)"/>
<xsl:variable name="token-after-first-match" select="substring-after(., concat($token-before-first-match, $searchString))"/>
<xsl:value-of select="concat($token-before-first-match, $replaceString)"/>
<xsl:choose>
<xsl:when test="contains($token-after-first-match, $searchString)">
<xsl:call-template name="replace">
<xsl:with-param name="InputString" select="$token-after-first-match"/>
<xsl:with-param name="searchString" select="$searchString"/>
<xsl:with-param name="replaceString" select="$replaceString"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$token-after-first-match"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$InputString"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
I want to add a new tag <br\> to my XML each 10 characters. (without including inner nodes)
For example if my input is:
<main>
01234567890123
<a>
link
</a>
45678901234567901234
</main>
I expect is to be somthing like:
<main>
0123456789<br\>0123
<a>
link
</a>
456789<br\>012345679<br\>01234
</main>
I wrote this function:
<!-- Add new "HTML Break" Every X chars -->
<xsl:template name="Add-HTML-Break-After-X-Chars">
<xsl:param name="text" />
<xsl:param name="max-length" />
<xsl:choose>
<xsl:when test="string-length($text) > $max-length">
<!-- Show the X length of the text -->
<xsl:copy-of select="substring($text,1,$max-length)" />
<!-- Adding the special tag -->
<br/>
<!-- continue to add the rest of the text -->
<xsl:call-template name="Add-HTML-Break-After-X-Chars">
<xsl:with-param name="text" select="substring($text, $max-length + 1)" />
<xsl:with-param name="max-length" select="$max-length" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<!-- Show the text (length is less then X) -->
<xsl:copy-of select="$text" />
</xsl:otherwise>
</xsl:choose>
The problem is that I'm getting the output without the inner tags:
<main>
0123456789<br\>0123
link
45<br\>6789012345<br\>67901234
</main>
I'm using this code to call the template:
<xsl:call-template name="Add-HTML-Break-After-X-Chars">
<xsl:with-param name="text" select="main" />
<xsl:with-param name="max-length" select="10" />
</xsl:call-template>
I tried also "value-of" instead of "copy-of".
Is there a way I can keep the tags?
If so, how can I keep them and make a "safe" insert of the new tag?
This is difficult to do in XSLT, because it processes each text node individually, with no information being carried over from the preceding text nodes.
Here's a possible approach:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="main/text()" name="wrap">
<xsl:param name="text" select="."/>
<xsl:param name="line-length" select="10"/>
<xsl:param name="carry">
<xsl:variable name="lengths">
<xsl:for-each select="preceding-sibling::text()">
<length>
<xsl:value-of select="string-length()"/>
</length>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="sum(exsl:node-set($lengths)/length) mod $line-length"/>
</xsl:param>
<xsl:value-of select="substring($text, 1, $line-length - $carry)"/>
<br/>
<xsl:if test="$carry + string-length($text) > $line-length">
<!-- recursive call -->
<xsl:call-template name="wrap">
<xsl:with-param name="text" select="substring($text, $line-length - $carry + 1)"/>
<xsl:with-param name="carry" select="0"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
When this is applied to the following test input:
XML
<main>A123456789B123<a>link1</a>456789C123456789D12345678<a>link2</a>9E123456789F1234567</main>
the result will be:
<?xml version="1.0" encoding="UTF-8"?>
<main>A123456789<br/>B123<a>link1</a>456789<br/>C123456789<br/>D12345678<a>link2</a>9<br/>E123456789<br/>F1234567</main>
Have the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<core:renderedItem renderedContentUUID="e8c957a5-03e0-41f4-83a5-297e43bd660f" family="ResearchOutput" type="ContributionToJournal" rendering="harvard" workflows="researchoutput" workflowStates="approved" external="false" classification="/dk/atira/pure/researchoutput/researchoutputtypes/contributiontojournal/article" state="/dk/atira/pure/publication/status/inpress">
<div class="rendering rendering_researchoutput rendering_researchoutput_harvard rendering_contributiontojournal rendering_harvard rendering_contributiontojournal_harvard">
<span>Reid, CT</span>
& Nsoh, W 2014, '
<span class="harvard_title">Whose Ecosystem is it Anyway: Private and Public Rights under New Approaches to Biodiversity Conservation</span>
'
<span>
<em>Journal of Human Rights and the Environment</em>
</span>
.
</div>
</core:renderedItem>
I'm new to XSLT and I'm trying to output the XML using XSLT in the following HTML format:
<div class="publications">
<p>
<span>Reid, CT</span>
& Nsoh, W 2014, '
<span class="harvard_title">Whose Ecosystem is it Anyway: Private and Public Rights under New Approaches to Biodiversity Conservation</span>
'
<span><em>Journal of Human Rights and the Environment</em></span>
.
</p>
</div>
The href link always starts with 'http://some.website.com/portal/en/research/' and the URL needs to be built up from that base + renderedContentUUID. I've managed this (sort of!) with help from others here but I'm stuck with the need to change the HTML output to build in the href etc. Also, the "harvard_title" string has to be changed to all lower case and if there is a colon in the title, I need to just take the string up to the colon. Then all spaces have to be changed into '-'s. This is how the URL looks at present:
http://somewebsite.com/portal/en/research/whose-ecosystem-is-it-anyway(e8c957a5-03e0-41f4-83a5-297e43bd660f).html
My xslt so far:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:core="http://atira.dk/schemas/pure4/model/core/stable"
xmlns:x="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="x">
<xsl:output method="html"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="lower">
abcdefghijklmnopqrstuvwxyz
</xsl:variable>
<xsl:variable name="upper">
ABCDEFGHIJKLMNOPQRSTUVWXYZ
</xsl:variable>
<xsl:variable name="pubURL"/>
<xsl:template match="//*">
<html>
<body>
<h1>Staff Publications</h1>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="//*">
<h2>Journal Articles</h2>
<xsl:for-each select="//core:renderedItem">
<xsl:sort select="#type"/>
<xsl:choose>
<xsl:when test ="#type = 'ContributionToJournal'">
<xsl:variable name="uuid" select ="#renderedContentUUID"/>
<xsl:variable name="show" select="translate(translate((div/span[#class = 'harvard_title']),' ','-'), $upper, $lower)" />
<xsl:if test="contains($show, ':')">
<xsl:variable name="pubURL" select="concat('http://somewebsite.com/portal/en/research/', substring-before($show, ':'),'(',$uuid,').html')"/>
</xsl:if>
<xsl:if test="not(contains($show, ':'))">
<xsl:variable name="pubURL" select="concat('http://somewebsite.com/portal/en/research/',$show,'(',$uuid,').html')"/>
</xsl:if>
<xsl:value-of select ="$pubURL"/>
<div class="publications">
<p>
<xsl:copy-of select="div"/>
</p>
</div>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Can anyone shed some light into this for me. I'm unsure about how to proceed constructing the href from the pubURL and 'inserting' it into the in the HTML output.
It's very difficult to answer your question because (1) your XML is not valid and (2) your current stylesheet is confusing (for lack of better word).
To pinpoint the answer to the exact problem you asked about: if your stylesheet had the following template (in the appropriate place):
<xsl:template match="span[#class='harvard_title']">
<span class="harvard_title">
<a>
<xsl:attribute name="href">
<xsl:text>http://some.website.com/portal/en/research/</xsl:text>
<xsl:value-of select="translate(translate(substring-before(., ':'), $upper, $lower), ' ', '-') "/>
<xsl:value-of select="concat('(', ../../#renderedContentUUID, ').html')"/>
</xsl:attribute>
<xsl:copy-of select="node()"/>
</a>
</span>
</xsl:template>
then this template would write the following output to the result tree:
<span class="harvard_title">Whose Ecosystem is it Anyway: Private and Public Rights under New Approaches to Biodiversity Conservation</span>
I am trying to debug some xsl code that puts links gathered from an XML Document with their descriptions in a list, it seems to be working, except when handling multiple links gathered from a single string that are delimited with a "|" and "^" symbol.
The XML Line that contains this code is very similar to this:
<WebContent>
<content_type_desc>Links</content_type_desc>
<content_value>Google|http://www.google.com^Yahoo|http://www.yahoo.com^Bing|Http://www.bing.com</content_value>
</WebContent>
The xsl that handles this is like
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="html" indent="yes" encoding="utf-8"/>
<xsl:template match="/">
<!--Links-->
<xsl:variable name="links" select="//WebContent[content_type_desc='Links']/content_value "/>
<!--Links-->
<!--Links section-->
<xsl:if test="$links != ''">
<br />
<xsl:choose>
<xsl:when test="contains($links,'^')">
<xsl:variable name="URL" select="substring-after(substring-before($links,'^'),'|')"/>
<xsl:variable name="URLText" select="substring-before($links,'|')"/>
<strong>Links: </strong><br />
<xsl:value-of select="$URLText"/>
<br />
</xsl:when>
<xsl:otherwise>
<xsl:variable name="URL" select="substring-after($links,'|')"/>
<xsl:variable name="URLText" select="substring-before($links,'|')"/>
<strong>Links: </strong><br />
<xsl:value-of select="$URLText"/>
<br />
</xsl:otherwise>
</xsl:choose>
<br />
</xsl:if>
<!--Links section-->
</xsl:template>
</xsl:stylesheet>
What's wrong with this is that it only outputs
<a href="http://www.google.com>Google</a>
When I would like it to output
<a href="http://www.google.com>Google</a>
<a href="http://www.yahoo.com>Yahoo</a>
<a href="http://www.bing.com>Bing</a>
Any help will be greatly appreciated as this has me completely stumped.
Assuming you are actually only using XSLT 1.0, to solve this you could make use of a named-template which is called recursively.
At the moment, the code is only processing the URL before the first ^ in the code. What you could do is put the main bulk of the code that checks the links variable in a named template. Then, in the xsl:when condition that runs when the URL does contain a ^ you add a recursive call to the named template, but passing in only the substring after the ^.
Try this XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="html" indent="yes" encoding="utf-8"/>
<xsl:template match="/">
<xsl:param name="links" select="//WebContent[content_type_desc='Links']/content_value "/>
<xsl:if test="$links != ''">
<strong>Links: </strong>
<br />
<xsl:call-template name="splitlinks">
<xsl:with-param name="links" select="$links" />
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template name="splitlinks">
<xsl:param name="links" />
<xsl:choose>
<xsl:when test="contains($links,'^')">
<xsl:variable name="URL" select="substring-after(substring-before($links,'^'),'|')"/>
<xsl:variable name="URLText" select="substring-before($links,'|')"/>
<xsl:value-of select="$URLText"/>
<br />
<xsl:call-template name="splitlinks">
<xsl:with-param name="links" select="substring-after($links,'^')" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="URL" select="substring-after($links,'|')"/>
<xsl:variable name="URLText" select="substring-before($links,'|')"/>
<xsl:value-of select="$URLText"/>
<br />
</xsl:otherwise>
</xsl:choose>
<br />
</xsl:template>
</xsl:stylesheet>
Given that you have tagged your question as classic ASP your XSLT processor is likely some version of MSXML for which there is an implementation of http://www.exslt.org/str/functions/tokenize/index.html so you could use that.