Tokenize function not working in XSLT - html

I am trying to use the tokenize function in XSLT and it is not working
<xsl:variable name="stringList" select="tokenize('XPath,is,fun', ',')"/>
<xsl:for-each select="$stringList">
<xsl:value-of select="." />
</xsl:for-each>
Is this anything wrong in this ? I tried this both in eclipse and in the w3schools tutorial editor
Actual code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:rg="http://www....."
xmlns="http://wwww.w3.org/1999/xhtml" >
<xsl:template match="/">
<html>
<head>
<title>Temp</title>
</head>
<body>
<xsl:variable name="stringList" select="tokenize('XPath,is,fun', ',')" />
<xsl:for-each select="$stringList">
<xsl:value-of select="." /><br/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Tokenize is actually an XSLT 2.0 function, and so you will need to use an XSLT processor that supports XSLT 1.0. The editor at W3Schools is XSLT 1.0 only.
I tried your XSLT at http://xslttest.appspot.com/ as an example, and it works happily.

Related

Unable to get any result from XSLT

I am attempting to write an XSLT for an XML file I have and I am getting no results.
A simple example of my XML:
<Monsters>
<Monster>
<Name>Dracula</Name>
<Actor>Bela Lugosi</Actor>
</Monster>
</Monsters>
And a simple XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
</head>
<body style="background: #FFFFCC">
<xsl:for-each select="Monsters/Monster">
<xsl:value-of select="Name" />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl>
The resulting display is entirely empty and I just know it's something silly that I'm missing.
You don't have a proper closing tag for xsl:stylesheet element.

Time format in XSLT 2.0

I worked with xslt 2.0 and Java( Saxon-HE ).Saxon-HE do not support xslt date-time format.I have current-date() and current-time() functions and I need them in the following format time(17:31), date(03/06).Please help me and give any suggestion hot to do it.Thanks a lot of
Please try the below tranformation:
<?xml version='1.0'?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="text"/>
<xsl:template match="test">
<xsl:variable name="currenttime" select="current-dateTime()" as="xs:dateTime"/>
<xsl:value-of select="format-dateTime($currenttime,'[H]')"/><xsl:text>:</xsl:text><xsl:value-of select="format-dateTime($currenttime,'[m]')"/>
<xsl:text>
</xsl:text>
<xsl:value-of select="format-dateTime($currenttime,'[M]')"/><xsl:text>/</xsl:text><xsl:value-of select="format-dateTime($currenttime,'[D]')"/>
</xsl:template>
</xsl:stylesheet>

Retrieve XML Variable in XSL file and say YES if found

I have problems with the Xpath expression test="$roles/roles/role='HOBSCS1GB'" . Can anyone help in solving. Thanks
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">
<xsl:variable name="roles">
<roles>
<role>HOBSCS1ROI</role>
<role>HOBSCS1GB</role>
<role>HOBSCS1FT</role>
</roles>
</xsl:variable>
<xsl:if test="$roles/roles/role='HOBSCS1GB'">
<xsl:value-of select="'YES'"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Assuming you want to find if a the roles element has one or more role elements with text = 'HOBSCS1GB': (Works in Saxon)
<xsl:if test="$roles/roles[role='HOBSCS1GB']">
<xsl:value-of select="'YES'"/>
</xsl:if>
Note that certain parsers like Microsoft may require you to tell the parser that $roles is a result tree fragment, by using node-set(), like so: (Works in msxsl)
<xsl:stylesheet ... xmlns:msxsl="urn:schemas-microsoft-com:xslt" ... />
<xsl:if test="msxsl:node-set($roles)/roles[role='HOBSCS1GB']">
<xsl:value-of select="'YES'"/>
</xsl:if>
Or in xsltproc:
<xsl:stylesheet ... xmlns:exsl="http://exslt.org/common" ... />
<xsl:if test="exsl:node-set($roles)/roles[role='HOBSCS1GB']">
<xsl:value-of select="'YES'"/>
</xsl:if>

can we insert HTML tags in XSL Variable

I just want to confirm whether we can insert html tags inside the xsl variable?
example
<xsl:variable name="htmlContent">
<html>
<body>
hiiii
</body>
</html>
</xsl:variable>
if i use
<xsl:value-of select="$htmlContent"/>
I shoud get
<html>
<body>
hiiii
</body>
</html>
Is it possible? i have tried
<xsl:value-of disable-output-escaping="yes" select="$htmlContent"/>
Eventhough i am not getting the desired output
Do not use value-of, which gets the text value of the selected node. Instead use copy-of, which copies the entire tree (nodes and all) into the output:
<xsl:copy-of select="$htmlContent"/>
Here is a full example:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:variable name="htmlContent">
<html><body>hiiii</body></html>
</xsl:variable>
<xsl:template match="/">
<xsl:element name="htmlText">
<xsl:copy-of select="$htmlContent"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
This will always produce the xml:
<htmlText>
<html>
<body>hiiii</body>
</html>
</htmlText>

how to interpret HTML in XSL

I have the following xml
<results>
<first-name>Carl<first-name>
<data><b> This is carl's data </b></data>
</results>
How do I include the bold tags which is present in the <data> tag to be a part of the output but rendered as an HTML
When I say <xsl:value-of select="results/data"/> The output is
<b> This is carl's data </b>
I want to achieve "This is carl's data" as the output in bold.
Well <xsl:copy-of select="results/data/node()"/> is a start but if the requirement is part of a larger problem then you are better off writing a template for data elements which uses apply-templates to push the child nodes through some template(s) for copying HTML elements through to the output.
I am sure someone will let me know if I am being naive:
<?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" indent="yes"/>
<xsl:template match="/results">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="first-name">
<xsl:value-of select="." />
<xsl:text>: </xsl:text>
</xsl:template>
<xsl:template match="data">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="b">
<b>
<xsl:value-of select="." />
</b>
</xsl:template>
</xsl:stylesheet>