Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,459
    Thanks
    1
    Thanked 23 Times in 21 Posts

    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:

    Code:
    geo:line[ends="both"] {
       text-decoration: overline
       }
    But beyond that, I need a little help...

    For instance, with an arc:

    Code:
    <geo:arc id="ABC">...</geo:arc>
    the presentation would need to be in the format:

    Code:
    <svg:g transform="translate(10,20)">
    <svg:path d="M 0 10 A 20 10 0 0 1 26 10" fill="none" stroke="#000000" />
    <svg:text x="0" y="20" font-family="Times New Roman">ABC</svg:text>
    </svg:g>
    With a two-letter id attribute for geo:arc, I'd need:

    Code:
    <svg:g transform="translate(10,0)">
    <svg:path d="M 0 10 A 20 20 0 0 1 18 10" fill="none" stroke="#000000" />
    <svg:text x="0" y="20" font-family="Times New Roman">AB</svg:text>
    </svg:g>
    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?
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    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.

    A quick example document and XSL document:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/xsl" href="test.xsl"?>
    
    <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:
    Code:
    <?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>
    
    	<xsl:template match="test">
    		<div>
    			<xsl:value-of select="."/>
    		</div>
    	</xsl:template>
    </xsl:stylesheet>
    Works beautifully in Mozilla 1.0 and IE6.
    Last edited by jkd; 06-24-2002 at 02:26 AM.

  • #3
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,459
    Thanks
    1
    Thanked 23 Times in 21 Posts

    Cool

    Thanks, Jason. Now if you'd only turn off your smileys...
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog

  • #4
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Originally posted by Alex Vincent
    Thanks, Jason. Now if you'd only turn off your smileys...
    Oops, you could have turned them off yourself you know Mr. Moderator .

    Anyway, just disabled them, and proudly proclaiming post #100.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •