XSLT transformation removes HTML elements from mixed content
Is it possible for XSLT to store anchors and other inline HTML tags in XML?
Reference Information. I am trying to convert an HTML document to XML with an XSL stylesheet using XSLT. The original HTML document contained content in which anchor tags were placed (for example, some hyperlinks here and there). I copied this content into my XML, but the XSLT output is missing anchor tags.
XML example:
<?xml version="1.0" ?>
<observations>
<observation><a href="http://jwz.org">Hyperlinks</a> disappear.</observation>
</observations>
XSL example:
<?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 have read several similar articles on stackoverflow and checked out the identity conversion wikipedia page; I started getting interesting results with xsl: copy-of, but I didn't understand enough about XSLT to get all the words and tags embedded in each XML element into the resulting HTML. Any help would be appreciated.
source to share
Write a separate template to match the elements a
, copy their attributes and content.
What's wrong with your approach? In your code
<xsl:value-of select="observation"/>
simply sends the element's string value to the output observation
. Its string value is the concatenation of all the text nodes it contains. But you need not only the text nodes, but the elements themselves a
.
The default XSLT behavior is to "skip" element nodes due to inline template. So, if you don't mention a
in a pattern match, it is simply ignored and only its textual content is displayed.
Stylesheet
Note. This stylesheet still relies somewhat on the default XSLT behavior. The order of events will look like this:
The pattern in which to
match="/observations"
match. He addshtml
andbody
. You then need to find the template rule for the contentobservations
. The inline template matchesobservation
, does nothing about it, and looks for a template to handle its content. For an element, thea
matching template is matched against copies of the element and attributes. Finally, inline copies of the templates are text nodes insideobservation
anda
.
<?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><a href="http://jwz.org">Hyperlinks</a> disappear.
</body>
</html>
source to share