XSLT: selecting a single sub-element to print

Discussion in 'XML' started by Tristan Miller, Feb 8, 2005.

  1. 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

    --
    _
    _V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
    / |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
    (7_\\ http://www.nothingisreal.com/ >< To finish what you
     
    Tristan Miller, Feb 8, 2005
    #1
    1. Advertising

  2. Tristan Miller

    Mukul Gandhi Guest

    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

    Tristan Miller <> wrote in message news:<>...
    > 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
     
    Mukul Gandhi, Feb 8, 2005
    #2
    1. Advertising

  3. David Carlisle, Feb 8, 2005
    #3
  4. Greetings.

    In article <>, David Carlisle wrote:
    >
    >> I have an XML file listing various information about text glyphs
    >> (Unicode value, HTML entity name, SGML entity name, etc.)

    >
    >
    > You might also be interested in
    >
    > http://www.w3.org/2003/entities/xml/unicode.xml
    >
    > which is essentially the same sort of file. The XSLT files in
    > http://www.w3.org/2003/entities/xml
    > generate various HTML, DTD, XSLT2 character maps, and other documents
    > from that source.


    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. :)

    Regards,
    Tristan

    --
    _
    _V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
    / |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
    (7_\\ http://www.nothingisreal.com/ >< To finish what you
     
    Tristan Miller, Feb 8, 2005
    #4

  5. > 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
     
    David Carlisle, Feb 9, 2005
    #5
  6. Greetings.

    In article <>, Mukul Gandhi
    wrote:
    > 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

    --
    _
    _V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
    / |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
    (7_\\ http://www.nothingisreal.com/ >< To finish what you
     
    Tristan Miller, Feb 9, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eych

    sub-selecting a DataTable?

    Eych, Jan 25, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    372
    Jeppe Dige Jespersen
    Jan 26, 2005
  2. keto
    Replies:
    0
    Views:
    1,045
  3. Ben
    Replies:
    2
    Views:
    955
  4. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    400
    David Cournapeau
    Dec 30, 2008
  5. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    2,170
    Lawrence D'Oliveiro
    May 20, 2011
Loading...

Share This Page