XSLT: selecting a single sub-element to print

T

Tristan Miller

Greetings.

I have an XML file listing various information about text glyphs (Unicode
value, HTML entity name, SGML entity name, etc.). All glyphs have a
Unicode value, but not all of them have HTML or SGML entity names.

I want to print out a list of these glyphs, using the HTML entity name if
it is available; otherwise the Unicode value should be printed. The
trouble is that I don't know how I can print only one or the other.

Each glyph is represented by an element <char>. Inside is a set of 0 or
more <entity> elements with "set" attributes. If a glyph has an HTML
entity, then it will contain an <entity> with the "set" attribute
beginning with the characters "html". All <char> elements also contain
the Unicode value in the enclosed <unicode> element.

For example, I want the output of the following XML file to be as follows:

bar
2004
fred
2006

Can anyone help?

<char>
<entity name="foo" set="iso-8879-pub">...</entity>
<entity name="bar" set="html4-special">...</entity>
<unicode value="2003">...</unicode>
</char>

<char>
<entity name="baz" set="iso-8879-pub">...</entity>
<unicode value="2004">...</unicode>
</char>

<char>
<entity name="fred" set="html4-alpha">...</entity>
<entity name="quux" set="iso-8879-pub">...</entity>
<unicode value="2005">...</unicode>
</char>

<char>
<unicode value="2006">...</unicode>
</char>

Kind regards,
Tristan
 
M

Mukul Gandhi

Hi Tristan,
Assuming the XML is -

<?xml version="1.0"?>
<root>
<char>
<entity name="foo" set="iso-8879-pub">...</entity>
<entity name="bar" set="html4-special">...</entity>
<unicode value="2003">...</unicode>
</char>
<char>
<entity name="baz" set="iso-8879-pub">...</entity>
<unicode value="2004">...</unicode>
</char>
<char>
<entity name="fred" set="html4-alpha">...</entity>
<entity name="quux" set="iso-8879-pub">...</entity>
<unicode value="2005">...</unicode>
</char>
<char>
<unicode value="2006">...</unicode>
</char>
</root>

(Please note the use of additional <root> tag)

Please try this XSL -

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:eek:utput method="text" />

<xsl:template match="/root">
<xsl:for-each select="char">
<xsl:choose>
<xsl:when test="entity[starts-with(@set,'html')]">
<xsl:value-of select="entity/@name" />
<xsl:if test="position() != last()">
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:when>
<xsl:eek:therwise>
<xsl:value-of select="unicode/@value" />
<xsl:if test="position() != last()">
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:eek:therwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

Regards,
Mukul
 
T

Tristan Miller

D

David Carlisle

Actually, I'm using the file at <http://www.bitjungle.com/~isoent/> because
I need the LaTeX equivalents. The example I posted was a
simplification. :)

Thre are latex equivalents in unicode.xml as well (sebastian and I have
a rather long latex connection:) although actually the latex mapping
hasn't kept quite up to date with the unicode 3.x and 4 additions that
have been made to the file. The same will be true for the bitjungle file
though as I don't think it's been updated for Unicode 3 at all (eg it
doesn't list the Unicode slots for the bold and script math alphabets in
plane 1 as far as I can see.

However I didn't mean that you should necessarily switch source file,
just that the xslt files on the W3C site probably have examples of
whatever XSLT you need as the basic structure is broadly similar.

David
 
T

Tristan Miller

Greetings.

Mukul Gandhi said:
Please try this XSL -

Many thanks; your example did exactly what I wanted. The difference
between your code and what I was trying was the entity[...] syntax in the
<xsl:when> test attribute.

Regards,
Tristan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,829
Latest member
KimberAlli

Latest Threads

Top