I'm working with a bowling website and a software program written for management of our bowling league scores. The software also allows us to write an article on the last bowling league day. Now I'm copying the article into HTML module on the website but since it's also implemented in the same XML output file as the scores I'd like to take it out as well with XSLT. I only don't know what are the commands for this... So I have no XSLT file I'm working with. If I look it up here or on google , there seem to be tons of ways to do it but every time there's a bunch of other code as well and I'm still a beginner...
Can anybody help me?
<Infos>
<Info>
<Title><![CDATA[THIS IS THE TITLE OF THE ARTICLE]]></Title>
<Text><![CDATA[THIS THE ARTICLE]]></Text>
</Info>
</Infos>
The CDATA piece contains text, I want it to display after applying
XSLT and determine the text outcome (bold, Italic and underlined) with
XSLT
The following stylesheet:
XSLT 1.0
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8" indent="yes"/>
<xsl:template match="/Infos">
<html>
<body>
<xsl:apply-templates select="Info"/>
</body>
</html>
</xsl:template>
<xsl:template match="Info">
<h2><xsl:value-of select="Title" /></h2>
<i><xsl:value-of select="Text" /></i>
</xsl:template>
</xsl:stylesheet>
when applied to your example input, will produce the following result:
<html>
<body>
<h2>THIS IS THE TITLE OF THE ARTICLE</h2>
<i>THIS THE ARTICLE</i>
</body>
</html>
rendered as:
Related
I am generating HTML from XML sources using XSLT. The HTML shows a lot of whitespace that was not in the original XML files. Normally this is not a problem as the browser will ignore the extra whitespace characters. But I am developing an application that relies on correct positioning of the text cursor inside the HTML page. The added whitespaces do mess up the offsets, making it impossible to reliably position the cursor inside an element.
My question: how can I get my XSLT to not introduce any additional whitespaces in text nodes? I am using <xsl:strip-space elements="*"/> but that does not keep the processor from introducing lots of whitespace. It looks like some pretty-printing processing is applied to the HTML and I have no idea where this comes from. I am currently using Saxon PE 9.9.1.7
[Edit]
I created a simple example that shows the same strange behaviour. First the XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<p>This is a long sentence. Trying to reproduce a whitespace handling problem with XSLT. This manual describes the spacecraft, safety aspects, usage and maintenance procedures. Make sure the manual is available to anyone who will be using the product.</p>
</root>
Here is the simplified XSL:
<?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"
exclude-result-prefixes="xs"
version="1.0">
<xsl:output method="html" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="root">
<xsl:text disable-output-escaping="yes"><!DOCTYPE html>
</xsl:text>
<html>
<head>
<title>Test</title>
</head>
<body>
<xsl:apply-templates select="*"/>
<script src="cursor.js"></script>
</body>
</html>
</xsl:template>
<xsl:template match="p">
<p contenteditable="true" id="p1" onclick="show_position()">
<xsl:value-of select="."/>
</p>
</xsl:template>
</xsl:stylesheet>
The JavaScript file to show the current cursor position:
function show_position( )
{
alert('position: ' + document.getSelection().anchorOffset );
}
The HTML that is generated by the XSLT looks like this (shown in oXygen):
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test</title>
</head>
<body>
<p contenteditable="true" id="p1" onclick="show_position()">This is a long sentence. Trying to reproduce a whitespace handling problem with XSLT.
This manual describes the spacecraft, safety aspects, usage and maintenance procedures.
Make sure the manual is available to anyone who will be using the product.</p><script src="cursor.js"></script></body>
</html>
Viewing the HTML in a browser makes all the extra whitespaces collapse into a single space, as expected. Clicking inside the paragraph shows the current offset from the start of the paragraph. Clicking immediately before 'This manual' shows position 86. Clicking one character to the right shows position 96. The same extra whitespace is introduced in the sentence starting with 'Make sure'.
I tried with Chrome and Safari - both show identical results. It does not seem to be a browser problem, but an issue with HTML generation by the XSLT processor. I have tried other Saxon versions but the resulting HTML is always the same.
Any further info on how to prevent these extra whitespace characters in my HTML output would be highly appreciated.
The default for output method="html" is indent="yes", I think, so you could certainly explicitly set indent="no" on your xsl:output declaration.
Additionally, as you say you use Saxon PE 9.9, you have access to XSLT 3 features like suppress-indentation="p" and/or Saxon PE/EE specific settings to use a very high setting for the normal line length, check the documentation for e.g. saxon:line-length or similar.
I'm using XSLT stylesheet to render a simple XML file of the text of Alice in Wonderland into HTML.
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Alice.xsl"?>
<book>
<title>Alice's Adventures in Wonderland</title>
<text>
<chapter>
<chapter_heading>Chapter I. Down the Rabbit-Hole</chapter_heading>
<p>Alice was beginning to get very tired of sitting by her sister on the
bank, and of having nothing to do: once or twice she had peeped into the
book her sister was reading, but it had no pictures or conversations in
it, 'and what is the use of a book,' thought Alice 'without pictures or
conversations?'</p>
<p>So she was considering in her own mind (as well as she could, for the
hot day made her feel very sleepy and stupid), whether the pleasure
of making a daisy-chain would be worth the trouble of getting up and
picking the daisies, when suddenly a White Rabbit with pink eyes ran
close by her.</p>
</chapter>
</text>
</book>
Simple stuff. And we're trying to output just the Title of the Chapter and the paragraphs into an tag in HTML using this code:
<?xml version="1.0" encoding="UTF-8"?><!-- DWXMLSource="http://www.w3.org/1999/XSL/Transform" -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<article>
<xsl:apply-templates/>
</article>
</body>
</html>
</xsl:template>
<xsl:template match="title"/>
<xsl:template match="chapter">
<h3>
<xsl:apply-templates select="chapter_heading"/>
</h3>
<div class="paragraph">
<xsl:apply-templates select="p"/>
</div>
</xsl:template>
</xsl:stylesheet>
But once the XML file is opened in a browser, all of the separate 'p' tags in the XML are just grouped together into one large 'div' in the HTML.
Our group is obviously very new to XSL, but as far as we've been able to research, we don't tell why this isn't working smoothly. Any help would be appreciated!
You dind't define a template for the <p> element.
You effectively applied templates on <p> elements with your <xsl:apply-templates select="p"/> but without a specific template for them, the xslt processor simply applies default template wich just output the text content of the elements. This is why you got everything in the same <div class='paragraph'> element (which is the parent element you create before applying template on <p>).
I guess, you want to preserve the <p> elements as they are in the input, then just add the template declaration below and you'll get your <p> in your output.
<xsl:template match="p">
<xsl:copy-of select="."/>
</xsl:template>
As a general design template, when you want to copy an input with some layout features, use a "copy template pattern" with this default declarations which will work for any input (<p> <citation> and so on).
<!-- This match will select any element without any other template matching (the `<p>` elements in your case.)-->
<xsl:template match="*">
<!-- First copy the element itself -->
<xsl:copy>
<!-- Then copy the attributes if any. -->
<xsl:copy-of select="#*">
<!-- Finally apply-templates on childs, then text nodes will be outputed by default templates, and elements would go through your template flow allowing to be copied or formatted if needed (with specific templates) -->
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
I apologize if this has been asked before, but I don't even know what terms to search for. I'm a beginner with xml and xslt and am looking to create html output from a xml file. My problem looks like this:
The text in my xml file is:
<?xml version="1.0" encoding="UTF-8"?>
<body>
<p>This is some text. This is the last bit of text.</p>
<add>This is some other <hi>text</hi>.</add>
</body>
The output I want in my html is
<p>This is some text. This is some other <hi>text</hi>. This is the last bit of text.<p>
I cannot just tag
<?xml version="1.0" encoding="UTF-8"?>
<body>
<p><a>This is some text.</a> <b>This is the last bit of text.</b><p>
<c>This is some other <hi>text</hi>.</c>
</body>
What I want to do is place a marker for where the text needs to be inserted. Sort of like this:
<?xml version="1.0" encoding="UTF-8"?>
<body>
<p>This is some text. <insert n="1"/> This is the last bit of text.<p>
<addition n="1">This is some other <hi>text</hi>.</addition>
</body>
The added text can be anywhere in the document, but it I can easily assign the n manually. Any ideas how to achieve this with my xslt stylesheet?
Edit: In some cases the text within contains more tags with corresponding templates in the xsl. I tried solving this on my own, but could not figure out how to apply templates within a key, could someone point me to a solution?
Many thanks!
Marie
Let me modify your example a bit, because I suspect that wherever there is an <addition n="1">, there will eventually be an <addition n="2"> too.
Given the following example input:
<body>
<p>Four score and seven years ago our fathers brought forth, <insert n="1"/>a new nation, conceived in liberty, and dedicated to the proposition that “all men are created equal.”</p>
<p>Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived, and so dedicated, can long endure. <insert n="2"/>We come to dedicate a portion of it, as a final resting place for those who died here, that the nation might live. This we may, in all propriety do.</p>
<addition n="1">upon this continent, </addition>
<addition n="2">We are met on a great battle field of that war. </addition>
</body>
the following stylesheet:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="add" match="addition" use="#n" />
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="insert">
<xsl:value-of select="key('add', #n)"/>
</xsl:template>
<xsl:template match="addition"/>
</xsl:stylesheet>
will return:
<?xml version="1.0" encoding="UTF-8"?>
<body>
<p>Four score and seven years ago our fathers brought forth, upon this continent, a new nation, conceived in liberty, and dedicated to the proposition that “all men are created equal.”</p>
<p>Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived, and so dedicated, can long endure. We are met on a great battle field of that war. We come to dedicate a portion of it, as a final resting place for those who died here, that the nation might live. This we may, in all propriety do.</p>
</body>
Edit:
In view of your added requirement, change:
<xsl:template match="insert">
<xsl:value-of select="key('add', #n)"/>
</xsl:template>
to:
<xsl:template match="insert">
<xsl:apply-templates select="key('add', #n)/node()"/>
</xsl:template>
Is it possible for XSLT preserve anchors and other embedded HTML tags within XML?
Background: I am trying to convert an HTML document into XML with an XSL stylesheet using XSLT. The original HTML document had content interspersed with anchor tags (e.g. Some hyperlinks here and there). I've copied that content into my XML, but the XSLT output lacks anchor tags.
Example XML:
<?xml version="1.0" ?>
<observations>
<observation>Hyperlinks disappear.</observation>
</observations>
Example XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/html">
<xsl:output method="html" indent="yes" encoding="UTF-8"/>
<xsl:template match="/observations">
<html>
<body>
<xsl:value-of select="observation"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output:
<html xmlns="http://www.w3.org/1999/html">
<body>Hyperlinks disappear.</body>
</html>
I've read a few similar articles on stackoverflow and checked out the Identity transform page on wikipedia; I started to get some interesting results using xsl:copy-of, but I don't understand enough about XSLT to get all of the words and tags embedded within each XML element to appear in the resulting HTML. Any help would be appreciated.
Write a separate template to match a elements, copy their attributes and content.
What is wrong with your approach? In your code,
<xsl:value-of select="observation"/>
simply sends to the output the string value of the observation element. Its string value is the concatenation of all text nodes it contains. But you need not only the text nodes in it, but also the a elements themselves.
The default behaviour of an XSLT processor is to "skip" element nodes, because of a built-in template. So, if you do not mention a in a template match, it is simply ignored and only its text content is output.
Stylesheet
Note: This stylesheet still relies on the default behaviour of the XSLT processor to some extent. The order of events will resemble the following:
The template where match="/observations" is matched. It adds html
and body to the output. Then, a template rule must be found for the
content of observations. A built-in template matches observation,
does nothing with it, and looks for a template to process its content.
For the a element, the corresponding template is matched, with
copies the element and attributes. Finally, a built-in template copies
the text nodes inside observation and a.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/observations">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="a">
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
XML Output
<html>
<body>Hyperlinks disappear.
</body>
</html>
I don't know too much about XSL but have managed to format XML coming from 3rd party web services using XSL without too much trouble. But the other day, a site that used to work stopped working. I discovered that they made a tiny change to the XML returned by the web service. This is what used to work (greatly simplified):
Update: I see the problem now, but I don't have a solution. The problem is with xsl:if test="#xsi:type='r0:CreditTx'". Change every "r0" to "s0" in the XSL, and it does not work.
I have replaced my original code with a working example:
XML:
<?xml version="1.0" encoding="unicode"?>
<MyResp xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:r0="http://www.foo.com/2.1/schema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<r0:creditVendReceipt receiptNo="1234567890">
<r0:transactions>
<r0:tx xsi:type="r0:CreditTx">
<r0:amt value="100" />
</r0:tx>
</r0:transactions>
</r0:creditVendReceipt>
</MyResp>
XSL:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:r0="http://www.foo.com/2.1/schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsl:template match="/">
<html>
<head>
</head>
<body >
<xsl:for-each select="MyResp/r0:creditVendReceipt/r0:transactions/r0:tx">
<xsl:if test="#xsi:type='r0:CreditTx'">
<xsl:value-of select="r0:amt/#value"/>
</xsl:if>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Desired HTML:
<html xmlns:r0="http://www.foo.com/2.1/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
100
</body>
</html>
The problem came when the web service changed the xmlns' short name "a" to "a0" (it now sends xmlns:a0="http://mysite.com/webservice/1.0/schema"); the namespace and everything else is the same. I have to change "a" to "a0" in the XSL for it to work (i.e. "GetInfoResp/a0:userName"). The problem is that the short name sent by the service changes from time to time. (In the real app there are a lot of name spaces, and the short names are even changing between the various requests.)
I thought the short name was just to make the XML shorter and easier to read, and that the actual name isn't significant (betwen the XML and the XSL; within the XSL obviously it has to match).
Can I get the XSL to ignore the short name in the XML, and just use its own short name?
Sorry if this was answered before; I looked thru the other questions and didn't see this specific issue.
The "short name" is called a namespace prefix -- and you don't have to change the namespace prefix in the transformation -- in fact it may be completely different from any prefix that could be used in the XML document.
This transformation:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xYz="http://mysite.com/webservice/1.0/schema"
exclude-result-prefixes="xYz">
<xsl:template match="/">
<html>
<body >
<xsl:value-of select="GetInfoResp/xYz:userName"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
produces exactly the same result as this transformation:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:a0="http://mysite.com/webservice/1.0/schema"
exclude-result-prefixes="a0">
<xsl:template match="/">
<html>
<body >
<xsl:value-of select="GetInfoResp/a0:userName"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Both transformations, when applied on this XML document (what is provided in the question is severely malformed and had to be corrected):
<GetInfoResp xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:a0="http://mysite.com/webservice/1.0/schema">
<a0:userName>Joe</a0:userName>
</GetInfoResp>
produce the same result:
<html>
<body>Joe</body>
</html>
Lesson to learn:
What matters is the namespace, not the prefix used to shorthand it.