Whilst transforming XHTML to XHTML with XSL I have a problem with namespaces. Consider as an example input:
<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Test</title></head>
<body>
<p>Remove this</p>
</body>
</html>
Then the following transformation does not work (e.g. does not remove the <p />):
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="#*|node()" name="copy">
<xsl:copy><xsl:apply-templates select="#*|node()" /></xsl:copy>
</xsl:template>
<xsl:template match="p" />
</xsl:stylesheet>
But this one does:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
<xsl:template match="#*|node()" name="copy">
<xsl:copy><xsl:apply-templates select="#*|node()" /></xsl:copy>
</xsl:template>
<xsl:template match="xhtml:p" />
</xsl:stylesheet>
My problem and question is: How can I change the XSLT so that I do not have to add prefixes to all the XHTML elements and it still get to match them? From what I tried so far, adding a default namespace like <xsl:stylesheet xmlns="http://www.w3.org/1999/xhtml" /> does not achieve this.
Thanks for your help!
How can I change the XSLT so that I do not have to add prefixes to all the XHTML elements and it still get to match them?
This is possible, but I would recommens defining the namespace and using the preefix in referring to elements in this namespace:
<xsl:template match="*[local-name()='p']" />
Warning:
This technique can be safe only if it is guaranteed that there arent two elements with the same local-name() but in different namespaces.
Related
To my eyes, this should be a valid XHTML document:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<a:html xmlns:a="http://www.w3.org/1999/xhtml">
<a:head>
<a:title>Document title</a:title>
</a:head>
<a:body>
<a:h1>Hello World!</a:h1>
</a:body>
</a:html>
However, it is not handled like XHTML by neither Firefox nor Internet Explorer, instead showing as text (with tags not visible). If I remove the namespace prefix…
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Document title</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
… it is rendered fine. I would have thought that both documents are semantically equal. Why does the first one not work?
You have to make sure that browsers get the hint to interpret the document as application/xhtml+xml (or possibly one of the other XML MIME types) instead of text/html.
On a server, you can achieve this by sending the corresponding Content-Type HTTP header:
Content-Type: application/xhtml+xml
Locally, you can typically achieve this by using .xhtml instead of .html as file extension (but this might depend on your system).
I have a XML file with code like
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet href="rss2.xsl" type="text/xsl" media="screen"?> <rss version="2.0"> <channel>
<title>TITLE</title>
<description>Description</description>
<link>
some link
</link>
<copyright>Copyrights 2012, All Rights Reserved</copyright>
<item>
<title> title 1</title>
<description> desc</description>
<link>
some link
</link>
</item> </channel> </rss>
And an XSL file for this is
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<xsl:template match="/">
<html>
<head>
<title>XML RSS Feed</title>
<link type="text/css" rel="stylesheet" href="rss_xsl.css" >
</head>
<body>
<h2>XML RSS Feed</h2>
<hr />
<div id="content">
<xsl:for-each select="rss/channel/item">
<div class="article">
<h3><xsl:value-of select="title"/></h3>
<p><xsl:value-of select="description"/></p>
</div>
</xsl:for-each>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The issue is when i comment the stylesheet code then chrome shows the feed. when i include the style the feeds are no more visible. How can this stop showing the feeds. is their some thing wrong with this inclusion of Style for XSL files..
Well the stylesheet markup is not well-formed, due to <link type="text/css" rel="stylesheet" href="rss_xsl.css" > which should be <link type="text/css" rel="stylesheet" href="rss_xsl.css"/>. Then I think the approach works when loading over HTTP. If you load the XML document from the file system then I think Chrome refuses to load the the stylesheet linked to, for security reasons. There might be command line options or settings to change the behavior.
I have an xhtml file that I'm attempting to transform such that:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN" "http://www.w3.org/TR/MathML2/dtd/xhtml-math11-f.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>yada</title>
<meta.....>
</head>
<body>
<p>Something</p>
<p>awesome</p>
</body>
</html>
becomes a
<title>yada</title>
<meta.....>
<p>Something</p>
<p>awesome</p>
The key thing that I'm getting at is that the <head> and <body> tags are removed from the document. I don't want to run this through sed or awk to remove them.
Everything that I've tried either has the whole thing in html or converts it all into pure text.
Background on problem: I've got a backup of my blog written in multimarkdown, I'm hoping to put them into different format but I need to get over this issue first.
Note: I started off with the identity template.
something like this? (bear with me, its been ages since I've done XSL actively)
<xsl:for-each select="head">
<xsl:copy-of select="."/>
</xsl:for-each>
<xsl:for-each select="body">
<xsl:copy-of select="."/>
</xsl:for-each>
Sounds like you want the identity transform for everything below html and body:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/html|/html/head|/html/body">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Are you sure this isn't the usual namespace problem? Does the input really look like you showed us, or did you leave out the namespaces because you didn't realise they made all the difference?
how do i implement css in xsl file? i tried this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<link rel="stylesheet" type="text/css" href="dxsl.css"/>
<xsl:output method="html" />
but it threw the error:
XSLTProcessor::importStylesheet(): Found a top-level element link with null namespace URI
and
Warning: XSLTProcessor::transformToXml(): No stylesheet associated to this object
Your html (link tag) must be inside an xsl:template. The xsl:template must be inside an xsl:stylesheet.
<?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" />
<xsl:template match="/*//*[1]">
<link rel="stylesheet" type="text/css" href="dxsl.css"/>
</xsl:template>
</xsl:stylesheet>
If I understand you correctly, you wish for the output to have a particular stylesheet?
XSL is a language used to transform XML from one format to another (in a sense it's like applying a css stylesheet). What would happen in a typical use case is that you would take some xml file and use XSL to transform it, say to XHTML. In this output, you can include a stylesheet using the link element if you wanted, but XSL doesnt really make use of CSS as such. (So basically, try putting the CSS in the XSL as part of the transformation to have the XHTML output use it.)
If this is an XML document you simply need to include the reference to the XSL and it should handle the transformation for you automatically.
'link' is a HTML element and you're trying to use it as an XML one. XSL modifies input into another document. You don't use CSS in an XSL file. You insert it into an (X)HTML file and apply it there.
As another answer said, the link must be generated by the transform. Here's an example that generates a small HTML doc with link to CSS from a simple xml doc. The css path comes from the data as well.
Try this
When I tried to convert my website to xhtml, things went perfectly until I realized that IE doesn't support it.
I went to the xhtml FAQ's section about IE, and tried out the workaround there, using an identity transformation to trick IE into rendering it as html in quirks mode. Unfortunately, this seems to make firefox (and possibly other browsers?) display the css background only over the text of the page.
So here is the code:
test.xhtml:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="copy.xsl"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="backgroundexample.css" />
</head>
<body>
<h1>This is a heading.</h1>
<p>This is a paragraph.</p>
</body>
</html>
backgroundexample.css:
body {
background-color:red;
}
copy.xsl:
<stylesheet version="1.0"
xmlns="http://www.w3.org/1999/XSL/Transform">
<template match="/">
<copy-of select="."/>
</template>
</stylesheet>
The effect of this is that the background only displays over the text, not the whole page.
I could not find the problem here or by googling, so please tell me if this has already been asked (and i am therefore bad at searching).
Can't verify your problem. Did you have tried something like this?
html, body {
background-color:red;
}