XSLT: Selecting elements by length of content text?
Here's an interesting situation. I'm working on an XML language for geometry, but I have a small problem in rendering a symbolism for arcs, rays and lines. For line segments, it's pretty simple: I associate a CSS styling with the element in question:
A little different, obviously. So I'm wondering how to construct the <xsl:template>...</xsl:template> match attribute to distinguish between a two-character text content and a three-character text content.
Advice?
Say you want to check the length of the @id node of the context node:
string-length(string(@id))
Will return that number, so you can attach that as a predicate with a =2 to select only nodes with @id value equal to 2.
<docEl>
<test id="aa">Element with id = "aa"</test>
<test id="aaa">Element with id = "aaa"</test>
<test id="ab">Element with id = "ab"</test>
<test id="ac">Element with id = "ac"</test>
<test id="abb">Element with id = "abb"</test>
</docEl>
And the XSLT document:
<?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="/">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Test</title>
</head>
<body>
All elements with id lengths = 2:
<xsl:apply-templates select="docEl/test[string-length(string(@id)) = 2]"/>
</body>
</html>
</xsl:template>