These two timestamps seems to use different GMTs on XSLT.
Timestamp #1
-> 1559780505287
-> 5/6/2019, 9:21:45 PM GMT-3 (dd/mm/yyyy)
Timestamp #2
-> 1562681762005
-> 9/7/2019, 11:16:02 AM GMT-3 (dd/mm/yyyy)
Using an online xslt test tool: https://xslttest.appspot.com/
After transformation, the timestamp #1 is 6/6/2019 and timestamp #2 is still 9/7/2019.
The timestamp #1 should be 5/6/2019.
Transformation result
How to correctly use GMT-3 on XSLT?
XML:
<java version="1.6.0_45" class="java.beans.XMLDecoder">
<object class="com.MyApp">
<void property="date1">
<object class="java.util.Date">
<long>1559780505287</long>
</object>
</void>
<void property="date2">
<object class="java.util.Date">
<long>1562681762005</long>
</object>
</void>
</object>
</java>
XSLT:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.w3.org/TR/xhtml1/strict">
<xsl:decimal-format decimal-separator="," grouping-separator="."/>
<xsl:template match="/">
<html>
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="format-detection" content="telephone=no"/>
</head>
<body>
<div class="container">
<h1>Date 1</h1>
<xsl:variable name="date1">
<xsl:value-of select="xs:date('1970-01-01') + java/object/void[#property='date1'] * xs:dayTimeDuration('PT0.001S')"/>
</xsl:variable>
<h2>
<xsl:value-of select="format-date($date1, '[D01]/[M01]/[Y0001]')"/>
</h2>
<h1>Date 2</h1>
<xsl:variable name="date2">
<xsl:value-of select="xs:date('1970-01-01') + java/object/void[#property='date2'] * xs:dayTimeDuration('PT0.001S')"/>
</xsl:variable>
<h2>
<xsl:value-of select="format-date($date2, '[D01]/[M01]/[Y0001]')"/>
</h2>
</div>
</body>
</html>
</xsl:template>
You should subtract your epoch time to GMT -3 (3Hours = 10800000 ms) since your conversion is milliseconds "PT0.001S".
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.w3.org/TR/xhtml1/strict">
<xsl:decimal-format decimal-separator="," grouping-separator="."/>
<xsl:template match="/">
<html>
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="format-detection" content="telephone=no"/>
</head>
<body>
<div class="container">
<h1>Date 1</h1>
<xsl:variable name="date1">
<xsl:value-of select="xs:date('1970-01-01') + (java/object/void[#property='date1']-10800000) * xs:dayTimeDuration('PT0.001S')"/>
</xsl:variable>
<h2>
<xsl:value-of select="format-date($date1, '[D01]/[M01]/[Y0001]')"/>
</h2>
<h1>Date 2</h1>
<xsl:variable name="date2">
<xsl:value-of select="xs:date('1970-01-01') + (java/object/void[#property='date2']-10800000) * xs:dayTimeDuration('PT0.001S')"/>
</xsl:variable>
<h2>
<xsl:value-of select="format-date($date2, '[D01]/[M01]/[Y0001]')"/>
</h2>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
I would suggest you convert your timestamp to a dateTime, adjust it to your timezone and then format it:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:template match="/java">
<html>
<body>
<xsl:for-each select="object/void">
<xsl:variable name="dateTime-GMT" select="xs:dateTime('1970-01-01T00:00:00Z') + object/long * xs:dayTimeDuration('PT0.001S')"/>
<xsl:variable name="dateTime-adjusted" select="adjust-dateTime-to-timezone($dateTime-GMT, xs:dayTimeDuration('-PT3H'))"/>
<h1>
<xsl:value-of select="#property"/>
</h1>
<h2>
<xsl:value-of select="format-dateTime($dateTime-adjusted, '[D01]/[M01]/[Y0001] [h]:[m01]:[s01] [P] [z]')"/>
</h2>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Result
<html>
<body>
<h1>date1</h1>
<h2>05/06/2019 9:21:45 p.m. GMT-03:00</h2>
<h1>date2</h1>
<h2>09/07/2019 11:16:02 a.m. GMT-03:00</h2>
</body>
</html>
Rendered:
Related
I'm working on an XSLT script to create an ePub TOC from a source file. I am able to get the content I need for the HTML files themselves but I also need to include the <unit> and <chapter> tags as well. I'm stuck how to get those into my output file.
This is my input file:
<?xml version="1.0" encoding="UTF-8"?>
<toc xmlns="http://www.standardnine.com/s9ml" data-uuid="c667450f8f7d45888630f13533a20e14">
<metadata thumbnailpath="../img/toc_thumbs/.crops/9781506250953_7c4cbc8d37244424a9f2a00ab56b5db7.jpg">
<remarks path="remarks.s9ml"/>
<edition/>
<title>SHSAT Course 2021</title>
<author/>
<publisher/>
<shortname>sn_a9645</shortname>
<pubdate>Publish Date</pubdate>
<version>Version</version>
<revision>Revision</revision>
<s9version>s9version</s9version>
<productid>Product ID</productid>
<bundleconfig path="config.s9ml"/>
<reflowable>true</reflowable>
<subtitle/>
</metadata>
<spine>
<unit designation="" enumeration="" data-uuid="f2aab3845c9d4e1988de8c6a22429af8">
<title/>
<chapter thumbnailpath="../img/toc_thumbs/.crops/9781506250953_56d8078f9660471799cb1741a3a45ade.jpg" designation="" enumeration="" data-uuid="c848cd9de0b646e1b3590904031e41f8" sandbox="true">
<title>Front Matter</title>
<exhibit path="frontmatter/fm_cover.html"/>
<exhibit path="frontmatter/fm_titlepage.html"/>
<exhibit path="frontmatter/copyright.html"/>
</chapter>
</unit>
<unit designation="Section" enumeration="1" data-uuid="d1ff46bfa6fe47dda6f27a9d871eb727">
<title>Getting Started</title>
<chapter thumbnailpath="../img/toc_thumbs/ch01_thumb.png" designation="Chapter" enumeration="1" data-uuid="6f33bec0993d4133b75e4bd9b3902ef8" sandbox="false">
<title>SHSAT Basics</title>
<exhibit path="chapter01/ch01_part.html"/>
<exhibit path="chapter01/ch01_reader_0.html"/>
</chapter>
<chapter thumbnailpath="../img/toc_thumbs/ch02_thumb.png" designation="Chapter" enumeration="2" data-uuid="2bf06864e6264270843990fe377ecb41" sandbox="false">
<title>Inside the SHSAT</title>
<exhibit path="chapter02/ch02_reader_0.html"/>
<exhibit path="chapter02/ch02_reader_1.html"/>
</chapter>
</unit>
</spine>
</toc>
This my XSLT script:
<?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"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:s9ml="http://www.standardnine.com/s9ml" exclude-result-prefixes="xs math xd xhtml s9ml"
xmlns:epub="http://www.idpf.org/2007/ops"
version="3.0">
<xsl:output method="xhtml"/>
<xsl:param name="topicPrefix"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>EPUB 3 Specifications - Table of Contents</title>
<link rel="stylesheet" type="text/css" href="../css/epub-spec.css" />
</head>
<body>
<nav epub:type="toc" id="toc">
<h1 class="title">Table of Contents</h1>
<ol>
<xsl:apply-templates select="//s9ml:exhibit" />
</ol>
</nav>
</body>
</html>
</xsl:template>
<!-- process exhibits referenced from toc.s9ml -->
<xsl:template match="s9ml:exhibit">
<xsl:element name="li" namespace="http://www.w3.org/1999/xhtml">
<xsl:variable name="count" select="position()"/>
<xsl:attribute name="id">
<xsl:value-of select="$topicPrefix"/>
<xsl:number format="0000" level="any"/>
</xsl:attribute>
<xsl:element name="a" namespace="http://www.w3.org/1999/xhtml">
<xsl:attribute name="href">
<xsl:value-of select="#path" />
</xsl:attribute>
<xsl:apply-templates select="document(#path)//xhtml:title">
</xsl:apply-templates>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
This is the output I'm getting:
<?xml version="1.0" encoding="UTF-8"?><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>EPUB 3 Specifications - Table of Contents</title>
<link rel="stylesheet" type="text/css" href="../css/epub-spec.css" />
</head>
<body>
<nav epub:type="toc" id="toc">
<h1 class="title">Table of Contents</h1>
<ol>
<li id="0001">Cover</li>
<li id="0002">Title Page</li>
<li id="0003">Copyright</li>
<li id="0004">Section 1: Getting Started</li>
<li id="0005">SHSAT Basics</li>
<li id="0006">Inside the SHSAT</li>
<li id="0007">Structure of the Test</li>
</ol>
</nav>
</body>
</html>
This is the output I need is:
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>EPUB 3 Specifications - Table of Contents</title>
<link rel="stylesheet" type="text/css" href="../css/epub-spec.css" />
</head>
<body>
<nav epub:type="toc" id="toc">
<h1 class="title">Table of Contents</h1>
<ol>
<li>Front Matter <ol>
<li id="0001">Cover</li>
<li id="0002">Title Page</li>
<li id="0003">Copyright</li>
</ol>
</li>
<li>Section 1 Getting Started <ol>
<li>Chapter 1 SHSAT Basics <ol>
<li id="0004"><a href="chapter01/ch01_part.html">Section 1: Getting
Started</a></li>
<li id="0005"><a href="chapter01/ch01_reader_0.html">SHSAT
Basics</a></li>
</ol>
</li>
<li>Chapter 2 Inside the SHSAT <ol>
<li id="0006"><a href="chapter02/ch02_reader_0.html">Inside the
SHSAT</a></li>
<li id="0007"><a href="chapter02/ch02_reader_1.html">Structure of
the Test</a></li>
</ol>
</li>
</ol>
</li>
</ol>
</nav>
</body>
</html>
It sounds as if you want to start processing <xsl:apply-templates select="//s9ml:unit"/> instead of <xsl:apply-templates select="//s9ml:exhibit" />, then write a template
<xsl:template mmatch="s9ml:unit | s9ml:chapter">
<li>
<xsl:value-of select="s9ml:title"/>
<ol>
<xsl:apply-templates select="node() except s9ml:title"/>
</ol>
</li>
</xsl:template>
to make sure you get a hierarchy of nested, ordered lists.
The whole snippets assumes you want to output XHTML result elements and have therefore declared xmlns="http://www.w3.org/1999/xhtml" on the xsl:stylesheet root element.
I'm trying to transform some xml with content encoded as html entities. I want to output the entity content as valid html.
The xml is like this ..
<?xml version="1.0" encoding="UTF-8"?><memo Version="1.0">
<header>
<meta title="==PROGRAMMING=="/>
<meta favourite="false"/>
<meta uuid="85f94ab2-77a8-XXXXXXXXXXXXXXX"/>
<meta createdTime="1551038092051"/>
</header>
<contents>
<content><p value="memo2" >=====</p><p>https://medium.freecodecamp.org/</p><p>=====</p>
</content>
</contents>
</memo>
I have some xslt as so..
xslt_src = '''
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<xsl:apply-templates select="memo/header/meta"/>
</head>
<body>
<xsl:apply-templates select="memo/contents"/>
</body>
</html>
</xsl:template>
<xsl:template match="memo/header/meta">
<xsl:apply-templates select="#title"/>
</xsl:template>
<xsl:template match="memo/contents">
<div class='content'>
<xsl:value-of select="content/text()"/>
</div>
</xsl:template>
<xsl:template match="#title">
<span id='title'>
<xsl:value-of select="."/>
</span>
</xsl:template>
</xsl:stylesheet>
'''
I process it with lxml in Python...
_________________________________________________________________python
from lxml import etree
xslt = etree.XML(xslt_src)
transform = etree.XSLT(xslt)
src = open('simple.xml').read()
xml = etree.XML(str.encode(src))
result = transform(xml)
root = result.getroot()
print('-----------------------out 1')
print(etree.tostring(root, pretty_print=True).decode('utf-8'))
print('-----------------------out 2')
content = root.xpath('/html/body/div/text()')
print(content[0])
==============================================================
etree.tostring(root) prints the structured document but leaves the html entities as encoded in the original xml.
-----------------------out 1
<html>
<head>
<span id="title">==PROGRAMMING==</span>
</head>
<body>
<div class="content"><p value="memo2" >=====</p><p>https://medium.freecodecamp.org/</p><p>=====</p>
</div>
</body>
</html>
but if I print root.xpath('/html/body/div/text()')[0] (the node with the html content) I get what I want...
-----------------------out 2
<p value="memo2" >=====</p><p>https://medium.freecodecamp.org/</p><p>=====</p>
=======================================================================
My question is: how can I make etree.tostring(root) replace the html entities with valid html, as is printed when I use the text attribute directly?
Cheers!
bitrat
Instead of:
<xsl:value-of select="content/text()"/>
try:
<xsl:value-of select="content" disable-output-escaping="yes"/>
I'm writing a XSLT script and there is a section named footnotes, my script is catching all elements other than footnotes. I'm using XSL2.0 and Below is my XML
<?xml version="1.0" encoding="utf-8"?>
<chapter num="D" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:acknowledgement="file://acknowledgement.xsd" xmlns:admission="file://admission.xsd" xmlns:affidavit="file://affidavit.xsd" xmlns:agreement="file://agreement.xsd" xmlns:appeal="file://appeal.xsd" xmlns:appearance="file://appearance.xsd" xmlns:application="file://application.xsd" xmlns:assent="file://assent.xsd" xmlns:authorisation="file://authorisation.xsd" xmlns:award="file://award.xsd" xmlns:brief="file://brief.xsd" xmlns:caveat="file://caveat.xsd" xmlns:certificate="file://certificate.xsd" xmlns:checklist="file://checklist.xsd" xmlns:claim="file://claim.xsd" xmlns:clause="file://clause.xsd" xmlns:comparisontable="file://comparisontable.xsd" xmlns:conditionsofcontract="file://conditionsofcontract.xsd" xmlns:declaration="file://declaration.xsd" xmlns:defaultjudgment="file://defaultjudgment.xsd" xmlns:defence="file://defence.xsd" xmlns:demand="file://demand.xsd" xmlns:flowchart="file://flowchart.xsd" xmlns:form="file://form.xsd" xmlns:guarantee="file://guarantee.xsd" xmlns:instruction="file://instruction.xsd" xmlns:judgment="file://judgment.xsd" xmlns:letter="file://letter.xsd" xmlns:minutes="file://minutes.xsd" xmlns:notice="file://notice.xsd" xmlns:oath="file://oath.xsd" xmlns:order="file://order.xsd" xmlns:patent="file://patent.xsd" xmlns:petition="file://petition.xsd" xmlns:powerofattorney="file://powerofattorney.xsd" xmlns:praecipe="file://praecipe.xsd" xmlns:questionnaire="file://questionnaire.xsd" xmlns:recognisance="file://recognisance.xsd" xmlns:release="file://release.xsd" xmlns:renunciation="file://renunciation.xsd" xmlns:reply="file://reply.xsd" xmlns:report="file://report.xsd" xmlns:request="file://request.xsd" xmlns:schedule="file://schedule.xsd" xmlns:statementofclaim="file://statementofclaim.xsd" xmlns:subpoena="file://subpoena.xsd" xmlns:summons="file://summons.xsd" xmlns:undertaking="file://undertaking.xsd" xmlns:warrant="file://warrant.xsd" xmlns:writ="file://writ.xsd" xmlns:book="file://book.xsd" xmlns:forms="file://forms.xsd" xmlns:misc="file://misc.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:htm="http://www.w3.org/TR/html5/">
<htm:p align="center">
<htm:b>Bold Content</htm:b>
<footnote num="1">
<para>
Footnote content.
</para>
</footnote>
</htm:p>
</chapter>
And below is my XSLT.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ntw="Number2Word.uri" xmlns:altova="http://www.altova.com/xslt-extensions" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:acknowledgement="file://acknowledgement.xsd" xmlns:admission="file://admission.xsd" xmlns:affidavit="file://affidavit.xsd" xmlns:agreement="file://agreement.xsd" xmlns:appeal="file://appeal.xsd" xmlns:appearance="file://appearance.xsd" xmlns:application="file://application.xsd" xmlns:assent="file://assent.xsd" xmlns:authorisation="file://authorisation.xsd" xmlns:award="file://award.xsd" xmlns:brief="file://brief.xsd" xmlns:caveat="file://caveat.xsd" xmlns:certificate="file://certificate.xsd" xmlns:checklist="file://checklist.xsd" xmlns:claim="file://claim.xsd" xmlns:clause="file://clause.xsd" xmlns:comparisontable="file://comparisontable.xsd" xmlns:conditionsofcontract="file://conditionsofcontract.xsd" xmlns:declaration="file://declaration.xsd" xmlns:defaultjudgment="file://defaultjudgment.xsd" xmlns:defence="file://defence.xsd" xmlns:demand="file://demand.xsd" xmlns:flowchart="file://flowchart.xsd" xmlns:form="file://form.xsd" xmlns:guarantee="file://guarantee.xsd" xmlns:instruction="file://instruction.xsd" xmlns:judgment="file://judgment.xsd" xmlns:letter="file://letter.xsd" xmlns:minutes="file://minutes.xsd" xmlns:notice="file://notice.xsd" xmlns:oath="file://oath.xsd" xmlns:order="file://order.xsd" xmlns:patent="file://patent.xsd" xmlns:petition="file://petition.xsd" xmlns:powerofattorney="file://powerofattorney.xsd" xmlns:praecipe="file://praecipe.xsd" xmlns:questionnaire="file://questionnaire.xsd" xmlns:recognisance="file://recognisance.xsd" xmlns:release="file://release.xsd" xmlns:renunciation="file://renunciation.xsd" xmlns:reply="file://reply.xsd" xmlns:report="file://report.xsd" xmlns:request="file://request.xsd" xmlns:schedule="file://schedule.xsd" xmlns:statementofclaim="file://statementofclaim.xsd" xmlns:subpoena="file://subpoena.xsd" xmlns:summons="file://summons.xsd" xmlns:undertaking="file://undertaking.xsd" xmlns:warrant="file://warrant.xsd" xmlns:writ="file://writ.xsd" xmlns:book="file://book.xsd" xmlns:forms="file://forms.xsd" xmlns:misc="file://misc.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:htm="http://www.w3.org/TR/html5/" xpath-default-namespace="http://foo.bar.com/ns" exclude-result-prefixes="#all">
<xsl:output method="html"/>
<xsl:strip-space elements="*"/>
<!--<xsl:preserve-space elements="para"/>-->
<xsl:variable name="nums" as="element()+">
<ntw:nums num="1" word="first"/>
<ntw:nums num="2" word="second"/>
<ntw:nums num="3" word="third"/>
<ntw:nums num="4" word="forth"/>
<ntw:nums num="5" word="fifth"/>
<ntw:nums num="6" word="sixth"/>
<ntw:nums num="7" word="seventh"/>
<ntw:nums num="8" word="eighth"/>
<ntw:nums num="9" word="nighth"/>
<ntw:nums num="10" word="tenth"/>
</xsl:variable>
<xsl:template match="/">
<xsl:text disable-output-escaping="yes"><![CDATA[<!DOCTYPE html>]]></xsl:text>
<html>
<head>
<xsl:text disable-output-escaping="yes"><![CDATA[</meta>]]></xsl:text>
<title>
<xsl:value-of select="chapter/title/text()"/>
</title>
<link rel="stylesheet" href="C:\Users\u0138039\Desktop\Proview\MY\2016\CCA3ED\XML\XSLT\main.css" type="text/css"/>
<xsl:text disable-output-escaping="yes"><![CDATA[</link>]]></xsl:text>
</head>
<body>
<section class="tr_chapter">
<xsl:apply-templates/>
</section>
<xsl:if test="//footnote">
<section class="tr_footnotes">
<xsl:text disable-output-escaping="yes"><![CDATA[<hr />]]></xsl:text>
<xsl:apply-templates select="//page | //footnote" mode="footnote"/>
</section>
</xsl:if>
</body>
</html>
</xsl:template>
<xsl:template match="chapter">
<xsl:apply-templates select="descendant::title[1]/page" mode="first"/>
<div class="chapter">
<xsl:variable name="num_S">
<xsl:value-of select="."/>
</xsl:variable>
<a name="CLIHK_CH_{$num_S}"/>
<div class="chapter-title">
<span class="chapter-num">
<xsl:value-of select="normalize-space(concat('Chapter ',format-number(./#num,'0')))"/>
</span>
<xsl:text disable-output-escaping="yes"><![CDATA[<br /><br />]]></xsl:text>
<xsl:apply-templates select="title/node() except descendant-or-self::page"/>
<xsl:text disable-output-escaping="yes"><![CDATA[<br /><br />]]></xsl:text>
<xsl:apply-templates select="chapter-meta"/>
</div>
<xsl:apply-templates select="child::node()[not(self::title|self::chapter-meta)]"/>
</div>
</xsl:template>
<xsl:template match="htm:b">
<span class="font-style-bold">
<xsl:apply-templates/>
</span>
</xsl:template>
<xsl:template match="htm:p">
<xsl:apply-templates select="./node()[1][self::page]" mode="first"/>
<div>
<xsl:choose>
<xsl:when test="./#align">
<xsl:attribute name="class"><xsl:text>para align-</xsl:text><xsl:value-of select="./#align"/></xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="class"><xsl:text>para</xsl:text></xsl:attribute>
</xsl:otherwise>
</xsl:choose>
<xsl:apply-templates/>
</div>
</xsl:template>
<xsl:template match="footnote">
<xsl:variable name="cnt" select="count(preceding::footnote)+1"/>
<xsl:variable name="varHeaderNote" select='concat("f",$cnt)'/>
<xsl:variable name="varFootNote" select='concat("#ftn.",$cnt)'/>
<sup>
<a name="{$varHeaderNote}" href="{$varFootNote}" class="tr_ftn">
<xsl:value-of select="#num"/>
</a>
</sup>
</xsl:template>
<xsl:template match="footnote" mode="footnote">
<xsl:variable name="cnt" select="count(preceding::footnote)+1"/>
<div class="tr_footnote">
<div class="footnote">
<sup>
<a>
<xsl:attribute name="name"><xsl:text>ftn.</xsl:text><xsl:value-of select="$cnt"/></xsl:attribute>
<xsl:attribute name="href"><xsl:text>#f</xsl:text><xsl:value-of select="$cnt"/></xsl:attribute>
<xsl:attribute name="class"><xsl:text>tr_ftn</xsl:text></xsl:attribute>
<xsl:value-of select="#num"/>
</a>
</sup>
<xsl:apply-templates/>
</div>
</div>
</xsl:template>
</xsl:stylesheet>
My current output is as below
<!DOCTYPE html><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
<title></title>
<link rel="stylesheet" href="C:\Users\u0138039\Desktop\Proview\MY\2016\CCA3ED\XML\XSLT\main.css" type="text/css"></link>
</head>
<body>
<section class="tr_chapter">
<div class="para align-center"><span class="font-style-bold">Bold Content</span>
Footnote content.
</div>
</section>
</body>
</html>
But my expected output is
<!DOCTYPE html><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
<title></title>
<link rel="stylesheet" href="C:\Users\u0138039\Desktop\Proview\MY\2016\CCA3ED\XML\XSLT\main.css" type="text/css"></link>
</head>
<body>
<section class="tr_chapter">
<div class="para align-center"><span class="font-style-bold">Bold Content</span><sup>
<a name="f1" href="#ftn.1" class="tr_ftn">1</a>
</sup>
</div>
</section>
<section class="tr_footnotes"><hr/>
<div class="tr_footnote">
<div class="footnote">
<sup>
<a name="ftn.1" href="#f1" class="tr_ftn">1</a>
</sup>
<div class="para">Footnote content.
</div>
</div>
</div></section>
</body>
</html>
Here is a working demo
please let me know where Am I going wrong and how can I fix this.
Thanks
Your stylesheet declares:
xpath-default-namespace="http://foo.bar.com/ns"
The footnote in your XML is in no namespace, therefore the test in:
<xsl:if test="//footnote">
returns false.
I'm new to XSLT and I can't understand why the root get processed twice (at least this is my interpretation of this output).
EDIT: (I'm using Saxon-HE with XSLT 2.0) but also tested with several online processes, getting always the same result.
XSLT file
<?xml version="1.0" encoding="UTF-8"?>
<!-- XResume.xsl: resume.xml ==> resume.xhtml -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xpath-default-namespace="https://github.com/IME-SE8/XResume">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head>
<meta charset="utf-8" />
<meta lang="en" />
<meta name="description" content="Personal Resume and Portfolio" />
<title><xsl:value-of select="resume/personalInformation/name/attribute::shortForm" /> Website</title>
</head>
<body>
<xsl:apply-templates select="resume"/>
</body>
</html>
</xsl:template>
<xsl:template match="resume">
<div class="resume">
<div class="header">
<div class="name"><xsl:value-of select="personalInformation/name" /></div>
<div class="contacts">
<xsl:for-each select="personalInformation/contact">
<div class="contactInformation">
<p><xsl:value-of select="organization" /></p>
<p><xsl:value-of select="address" /></p>
<p><xsl:value-of select="phoneNumber" /></p>
<p><xsl:value-of select="email" /></p>
</div>
</xsl:for-each>
</div>
</div>
<div class="sections">
<xsl:apply-templates />
</div>
</div>
</xsl:template>
<xsl:template match="interests"></xsl:template>
<xsl:template match="education"></xsl:template>
<xsl:template match="skills"></xsl:template>
<xsl:template match="experiences"></xsl:template>
<xsl:template match="projects"></xsl:template>
<xsl:template match="awards"></xsl:template>
</xsl:stylesheet>
XML file
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl"
href="https://github.com/IME-SE8/XResume/master/XResume.xsl"?>
<resume
xmlns="https://github.com/IME-SE8/XResume"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://github.com/IME-SE8/XResume XResume.xsd">
<personalInformation>
<name first="John" last="Doe" shortForm="JD">John Doe</name>
<contact type="institutional">
<organization>StackOverflow Institute of Technology</organization>
<address>Internet</address>
<phoneNumber>+1 (666) 666-9999</phoneNumber>
<email>john#d.oe</email>
</contact>
</personalInformation>
<interests>
<interest>Q and A</interest>
<interest>XSLT</interest>
</interests>
<education></education>
<skills></skills>
<experiences></experiences>
<projects></projects>
<awards></awards>
</resume>
HTML output
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta lang="en">
<meta name="description" content="Personal Resume and Portfolio">
<title>JD Website</title>
</head>
<body>
<div class="resume">
<div class="header">
<div class="name">John Doe</div>
<div class="contacts">
<div class="contactInformation">
<p>StackOverflow Institute of Technology</p>
<p>Internet</p>
<p>+1 (666) 666-9999</p>
<p>john#d.oe</p>
</div>
</div>
</div>
<div class="sections">
John Doe
StackOverflow Institute of Technology
Internet
+1 (666) 666-9999
john#d.oe
</div>
</div>
</body>
</html>
(yes, with that amount of blank lines)
The output header div is perfectly fine, but inside the sections div that apply-templates renders all the information in the div header again but without the HTML tags.
Is there any XSLT processing detail am I missing? Does the template match sets the context in a way that the matched element is now considered a root or something like that?
The problem is here:
<div class="sections">
<xsl:apply-templates />
</div>
This applies templates to all child nodes of the current node (resume), including the personalInformation element.
As there is no matching template specified for personalInformation, the builtin XSLT templates are used by the XSLT processor and applying them results in outputting the concatenation of all descendent text-nodes of the personalInformation element.
Solution:
Replace:
<div class="sections">
<xsl:apply-templates />
</div>
with:
<div class="sections">
<xsl:apply-templates select="*[not(self::personalInformation)]" />
</div>
The result of the transformation now doesn't contain the noted problematic output:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta lang="en">
<meta name="description" content="Personal Resume and Portfolio">
<title>JD Website</title>
</head>
<body>
<div class="resume">
<div class="header">
<div class="name">John Doe</div>
<div class="contacts">
<div class="contactInformation">
<p>StackOverflow Institute of Technology</p>
<p>Internet</p>
<p>+1 (666) 666-9999</p>
<p>john#d.oe</p>
</div>
</div>
</div>
<div class="sections"></div>
</div>
</body>
</html>
You haven't supplied an expected output, so I will guess at your intended outcome. Here is an XSLT 2.0 solution. If you need XSLT 1.0, please comment, and I can add. But just remember that if your transform engine is the browser, you have no excuse not to use XSLT 2.0. (Refer Saxon CE).
XSLT 2.0 Solution
This XSLT 2.0 stylesheet ...
<xsl:transform
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:r="https://github.com/IME-SE8/XResume"
exclude-result-prefixes="r"
version="2.0">
<xsl:output method="html" version="5" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="/">
<html>
<head>
<meta lang="en" />
<meta name="description" content="Personal Resume and Portfolio" />
<title><xsl:value-of select="r:resume/r:personalInformation/r:name/#shortForm" /> Website</title>
</head>
<body>
<xsl:apply-templates select="r:resume"/>
</body>
</html>
</xsl:template>
<xsl:template match="r:resume">
<div class="resume">
<div class="header">
<div class="name"><xsl:value-of select="r:personalInformation/r:name" /></div>
<div class="contacts">
<xsl:apply-templates select="r:personalInformation/r:contact" />
</div>
</div>
<div class="sections">
<xsl:apply-templates select="* except r:personalInformation" />
</div>
</div>
</xsl:template>
<xsl:template match="r:contact">
<div class="contactInformation">
<xsl:apply-templates />
</div>
</xsl:template>
<xsl:template match="r:organization|r:address|r:phoneNumber|r:email">
<p><xsl:value-of select="." /></p>
</xsl:template>
<xsl:template match="r:education|r:skills|r:experiences|r:projects|r:awards">
<h2><xsl:value-of select="local-name()" /></h2>
<p><xsl:value-of select="." /></p>
</xsl:template>
<xsl:template match="r:interests">
<h2>interests</h2>
<ul>
<xsl:apply-templates />
</ul>
</xsl:template>
<xsl:template match="r:interest">
<li>
<xsl:value-of select="." />
</li>
</xsl:template>
<xsl:template match="*" />
</xsl:transform>
... when applied to this input document ...
<resume
xmlns="https://github.com/IME-SE8/XResume"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://github.com/IME-SE8/XResume XResume.xsd">
<personalInformation>
<name first="John" last="Doe" shortForm="JD">John Doe</name>
<contact type="institutional">
<organization>StackOverflow Institute of Technology</organization>
<address>Internet</address>
<phoneNumber>+1 (666) 666-9999</phoneNumber>
<email>john#d.oe</email>
</contact>
</personalInformation>
<interests>
<interest>Q and A</interest>
<interest>XSLT</interest>
</interests>
<education></education>
<skills></skills>
<experiences></experiences>
<projects></projects>
<awards></awards>
</resume>
... will yield this output html page ....
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta lang="en">
<meta name="description" content="Personal Resume and Portfolio">
<title>JD Website</title>
</head>
<body>
<div class="resume">
<div class="header">
<div class="name">John Doe</div>
<div class="contacts">
<div class="contactInformation">
<p>StackOverflow Institute of Technology</p>
<p>Internet</p>
<p>+1 (666) 666-9999</p>
<p>john#d.oe</p>
</div>
</div>
</div>
<div class="sections">
<h2>interests</h2>
<ul>
<li>Q and A</li>
<li>XSLT</li>
</ul>
<h2>education</h2>
<p></p>
<h2>skills</h2>
<p></p>
<h2>experiences</h2>
<p></p>
<h2>projects</h2>
<p></p>
<h2>awards</h2>
<p></p>
</div>
</div>
</body>
</html>
Explanation: Why were you getting root processed twice
In short because your <div class="sections"><xsl:apply-templates /></div> instruction did not specify a select attribute. The default selection applied, which was at that point the document root.
All the elements in your source document are in a namespace, but your stylesheet is written to process elements in no namespace. Welcome to the club, and join the 10m other people who have fallen into this trap. Essentially, you resume elements don't match the match="resume" template, so the default template kicks in, and this outputs the raw text with no tags. For the solution, search on "XSLT default namespace" and choose any one of about 1000 answers.
On re-reading, I see that you've used xpath-default-namespace="https://github.com/IME-SE8/XResume", which should fix the problem if you are using an XSLT 2.0 processor, or trigger an error if you're using an XSLT 1.0 processor. So it might be useful (actually, it's always useful) to tell us what processor you are using and how you are running it.
I'm just trying to select the node 'productgroep' with attribute value 'cd'. This is not working and I really don't understand why, I searched for answers but didn't find any.
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Oefening_8.xsl"?>
<catalogus>
<!-- cd catalogus -->
<productgroep type="cd">
<item referentienummer="7051444" catalogusnummer="1800022" EAN="0025218000222">
...
</productgroep>
<productgroep type="film">
<item referentienummer="8051445" catalogusnummer="2800023" EAN="5051888073650">
....
</productgroep>
</catalogus
XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head>
<title>Oefening_8.xsl</title>
<meta charset="utf-8"/>
<link href="Oefening_8.css" type="text/css" rel="stylesheet"/>
</head>
<body>
<h1></h1>
<xsl:template match="productgroep[#type='cd']">
</xsl:template>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
An <xsl:template/> cannot be a child of a <xsl:template/>, so your stylesheet is currently invalid and probably giving an error somewhere, depending on how you are using the XML and XSL.
One solution is to create the separate <xsl:template>s and use <xsl:apply-templates /> to processes the children of the source element.
<xsl:template match="/">
<html>
<head>
<title>Oefening_8.xsl</title>
<meta charset="utf-8"/>
<link href="Oefening_8.css" type="text/css" rel="stylesheet"/>
</head>
<body>
<h1></h1>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="productgroep[#type='cd']">
<xsl:value-of select="item/#catalogusnummer"/> <!-- print #catalogusnummer for example -->
</xsl:template>
As #andyb pointed out, you can't have a template inside a template. It may be that you meant to use xsl:apply-templates where you have xsl:template, but that wouldn't have worked either with the path you used, because the current context there is the node above catalogus. Your options are to change the initial xsl:template to select the root element with either:
or
or to use the full path in the xsl:apply-templates:
I prefer the first option:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template match="/*">
<html>
<head>
<title>Oefening_8.xsl</title>
<meta charset="utf-8"/>
<link href="Oefening_8.css" type="text/css" rel="stylesheet"/>
</head>
<body>
<h1></h1>
<xsl:apply-templates select="productgroep[#type='cd']" />
</body>
</html>
</xsl:template>
</xsl:stylesheet>