Search code examples
xmlxslttei

How to "grab text of a node" and put it as data-element in html


I have several entries like this:

<place label="Juan Fernandez"><placeName>Juan Fernandez</placeName><location><geo>-33.666667, -78.833333</geo></location></place>

and I want to transform it into

   <span label="Juan Fernandez" data-boo-coordinates ="-33.666667, -78.833333"> Juan Fernandez <location><geo>-33.666667, -78.833333</geo></location></span>

for the label, there is no fuss:

<xsl:template match="tei:place">
  <xsl:element name="span">
  <xsl:attribute name="label">
    <xsl:text>{@label}</xsl:text>
  </xsl:attribute>

I figured that something similar should work and tried several things, which I will list below: quite naturally the following does not work:

  <xsl:attribute name="data-boo-coordinates">
    <xsl:value-of select="geo"/>
  </xsl:attribute>

However, I thought this one should work:

  <xsl:attribute name="data-boo-coordinates">
    <xsl:value-of select="place/location/geo"/>
  </xsl:attribute>

the complete template for the place-elements

<xsl:template match="tei:place">
  <xsl:element name="span">
  <xsl:attribute name="label">
    <xsl:text>{@label}</xsl:text>
  </xsl:attribute>
  <xsl:attribute name="data-boo-coordinates">
    <xsl:value-of select="*/location/geo"/>
  </xsl:attribute>
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

As you can see, I changed a bit again, hoping that it would get the node's text by using <xsl:value-of>. Am I doing sth. totally stupid here?

all the best and thank you for your time, K


Solution

  • Why can't you use a literal result element and attribute value templates

    <xsl:template match="tei:place">
      <span label="{@label}" data-boo-coordinates="{location/geo}">
        <xsl:apply-templates/>
      </span>
    </xsl:template>