I have a following XML document with the content:
<?xml version="1.0" encoding="UTF-8"?>
<body xmlns="http://www.w3.org/1999/xhtml">
<div >
<p > </p>
<h1 id="sigil_toc_id_6">Other Sources Referred To</h1>
<p > </p>
<RefMaterial type="other">
<RefTitle>Agreement on Trade-Related Aspects of <searchHighlight xmlns="">Intellectual</searchHighlight> Property Rights (“TRIPS Agreement”). See Marrakesh Agreement Establishing the World Trade Organization, Annex 1C</RefTitle>
</RefMaterial>
</div>
</body>
Now I am trying to convert this XML to HTML using the following XSL stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp " ">
]>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:param name="portlet"/>
<xsl:output method="html" encoding="UTF-8" indent="no"/>
<xsl:template match="*:body[descendant::*:RefMaterial]">
<div >
<div >
<xsl:for-each select="descendant::node()">
<xsl:choose>
<xsl:when test="name()='RefMaterial' and not(string-length(@type)=0)">
<div >
<div >
<xsl:value-of select="*:RefTitle except *:ref"/>
</div>
</div>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</div>
</div>
</xsl:template>
<xsl:template match="searchHighlight">
<span id="search-highlight-{format-number(count(preceding::searchHighlight) 1,'0000')}">
<xsl:apply-templates/>
</span>
</xsl:template>
</xsl:stylesheet>
and this is the current HTML obtained:
<div >
<div >
<div >
<div >Agreement on Trade-Related Aspects of Intellectual Property Rights (“TRIPS Agreement”). See Marrakesh Agreement Establishing the World Trade Organization, Annex 1C</div>
</div>
</div>
</div>
How should I alter the XSL to obtain the following HTML line. Basically to put the search-highlight class on to the word.
<div >Agreement on Trade-Related Aspects of <span id="search-highlight-0001">Intellectual</span> Property Rights (“TRIPS Agreement”). See Marrakesh Agreement Establishing the World Trade Organization, Annex 1C</div>
Many thanks for reading up this post!
CodePudding user response:
Instead of
<xsl:value-of select="*:RefTitle except *:ref"/>
which outputs the text including all subnodes, regardless of what these subnodes are, use
<xsl:apply-templates select="*:RefTitle except *:ref"/>
which applies to every node individually: text directly contained in RefTitle
is output, but subnodes like searchHighlight
are treated according to their own template, so that your <xsl:template match="searchHighlight">
comes into play.