Project

Profile

Help

RE: HTML entity parsing in David Carlisle's htmlparse.xsl... ยป key-failure3.xsl

Martin Honnen, 2020-07-03 00:15

 
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:d="http://example.com/dc"
exclude-result-prefixes="#all"
expand-text="yes">
<xsl:param name="entity-name" as="xs:string">aacute</xsl:param>
<xsl:param name="entity-reference" as="xs:string"><![CDATA[&aacute;]]></xsl:param>
<xsl:output method="html"/>
<xsl:template match="/" name="xsl:initial-template">
<html>
<head>
<title>Test</title>
</head>
<body>
<section>
<h1>key test</h1>
<p><code><xsl:value-of select="$entity-name || ': ' || $d:ents/key('d:ents', $entity-name)"/></code></p>
</section>
<section>
<h1>function test</h1>
<p><code><xsl:value-of select="$entity-reference || ': ' || d:chars($entity-reference)"/></code></p>
</section>
</body>
</html>
</xsl:template>
<xsl:variable name="d:ents">
<entity name="Aacute">&#xC1;</entity>
<entity name="aacute">&#xE1;</entity>
<entity name="Acirc">&#xC2;</entity>
</xsl:variable>
<xsl:key name="d:ents" match="entity" use="@name"/>

<xsl:function name="d:chars">
<xsl:param name="s" as="xs:string"/>
<xsl:value-of>
<xsl:analyze-string select="$s" regex="&amp;(#?)(x?)([0-9a-fA-F]+|[a-zA-Z][a-zA-Z0-9]*);">
<xsl:matching-substring>
<xsl:choose>
<xsl:when test="$d:ents/key('d:ents',regex-group(3))">
<xsl:value-of select="$d:ents/key('d:ents',regex-group(3))"/>
</xsl:when>
<xsl:otherwise>
<xsl:message>htmlparse: Unknown entity: <xsl:value-of select="regex-group(3)"/></xsl:message>
<xsl:text>&amp;</xsl:text>
<xsl:value-of select="regex-group(3)"/>
<xsl:text>;</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:value-of>
</xsl:function>
</xsl:stylesheet>
    (1-1/1)