0

Inside a TEI header of a tei file I have declared some chars: `

<charDecl>
   <char xml:id="char1">
     <charName>Vnc.</charName>
     <desc>Tractus longus</desc>
    </char>

    <char xml:id="char2">
     <charName>Drag.</charName>
     <desc>Interclusio uncinata</desc>
    </char>
...
</charDecl>

`

Inside body text of the tei I have tag : Lorem ipsum <g ref="#char2"/> dolor sit II. Cotylus habet <g ref="#char1">—</g> I want it will display like this: Lorem ipsum [Drag.] dolor sit II. Cotylus habet —[Vnc.]

How to do it with a xsl stylesheet xsl:template? I tried this but doesn't work:

 <xsl:key name="char" match="tei:teiHeader/encodingDesc/charDecl/char" use="@xml:id"/>

<xsl:template match="tei:g[@ref]">

    <xsl:apply-templates />
    <span title="<xsl:value-of select="key('char',substring-after(@ref,'#'))/desc"/>">[<xsl:value-of select="key('char',substring-after(@ref,'#'))/charName"/>]</span>
</xsl:template>

Inside the head of the file tei I link to stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<?oxygen RNGSchema="file:teilite.rnc" type="compact"?>
<?xml-stylesheet type="text/xsl" href="../mainstyle.xsl"?>
<TEI xmlns="http://www.tei-c.org/ns/1.0">

Inside the main style I have:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:eg="http://www.tei-c.org/ns/Examples"
  xmlns:tei="http://www.tei-c.org/ns/1.0" 
  xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
  xmlns:exsl="http://exslt.org/common"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  xmlns:fn="http://www.w3.org/2005/xpath-functions"
  extension-element-prefixes="exsl msxsl"
  xmlns="http://www.w3.org/1999/xhtml" 
  xmlns:html="http://www.w3.org/1999/xhtml" 
  exclude-result-prefixes="xsl tei xd eg fn #default">
...
<xsl:include href="rendchars.xsl"/>

Inside the rendchars.xsl it starts:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns="http://www.w3.org/1999/xhtml"
    xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0"
    xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:html="http://www.w3.org/1999/xhtml"
    xmlns:rng="http://relaxng.org/ns/structure/1.0" xmlns:tei="http://www.tei-c.org/ns/1.0"
    xmlns:teix="http://www.tei-c.org/ns/Examples" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:teidocx="http://www.tei-c.org/ns/teidocx/1.0"
    exclude-result-prefixes="a fo html rng tei teix teidocx" version="2.0">

I also tryed these codes that didn't work for me:

<xsl:key name="chname" match="tei:teiHeader/encodingDesc/charDecl/char" use="@xml:id"/>
<xsl:template match="g[@ref]">
    <span title="{key('chname',substring-after(@ref,'#'))/tei:desc}">[<xsl:value-of select="key('chname',substring-after(@ref,'#'))/tei:charName"/>]</span><xsl:text></xsl:text>
 </xsl:template>

and

<xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="charDecl"/>
    <xsl:template match="g[@ref]">
      <xsl:value-of select="id(substring-after(@ref,'#'))/charName"/>
    </xsl:template>

and

<xsl:key name="chname" match="tei:teiHeader/tei:encodingDesc/tei:charDecl/tei:char" use="@xml:id"/>
     <xsl:template match="g[@ref]">
        <xsl:apply-templates/>
        <span title="{key('chname',substring-after(@ref,'#'))/tei:desc}">[<xsl:value-of select="key('chname',substring-after(@ref,'#'))/tei:charName"/>]</span><xsl:text></xsl:text>
     </xsl:template>
steplab
  • 11
  • 6
  • Which XSLT version, which XSLT processor do you use? Given the use of `xml:id` I would think that you don't even need a key in XSLT 2.0 but could use `id`. As for the attempt to use a key, show us your key definition and any namespaces used and declared in XML input and the XSLT code. – Martin Honnen Jul 25 '17 at 15:47

2 Answers2

0

Assuming XSLT 2.0 it should suffice to use the id function:

<xsl:template match="g[@ref]">
  <xsl:value-of select="id(substring(@ref, 2))/charName"/>
</xsl:template>

Complete example: http://xsltransform.net/a9Gix6.

As for your attempt, if all elements are in the same namespace then I think you want <span title="{key('char',substring-after(@ref,'#'))/tei:desc}">[<xsl:value-of select="key('char',substring-after(@ref,'#'))/tei:charName"/>]</span>, that is, you need to make sure you use the prefix for the TEI elements in all paths. And the key would need to be changed to <xsl:key name="char" match="tei:teiHeader/tei:encodingDesc/tei:charDecl/tei:char" use="@xml:id"/>

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
0

I resolved with this:

   <xsl:key name="chname" match="tei:teiHeader/tei:encodingDesc/tei:charDecl/tei:char" use="@xml:id" />
             <xsl:template match="tei:g[@ref]">
                <xsl:apply-templates/>
                <span title="{key('chname',substring-after(@ref,'#'))/tei:desc}" >[<xsl:value-of select="key('chname',substring-after(@ref,'#'))/tei:charName"/>]</span><xsl:text></xsl:text>
             </xsl:template>
steplab
  • 11
  • 6