JPM
JPM

Reputation: 2066

Create xsl key by "joining" elements

<t>
  <rendition xml:id="b">color: blue</rendition>
  <rendition xml:id="r">color: red</rendition>

  <tagUsage gi="p" render="b" />
  <tagUsage gi="emph" render="r" />
</t>

How would I create an XSL 1.0 key into rendition elements based on @gi in the tagUsage element, joining rendition/@xml:id to tagUsage/@render? Something like

<xsl:key name="rendition-by-tagName" 
         match="rendition" 
         use="//tagUsage[@xml:id of rendition = @render of tagUsage]/@gi" />

so that given "p", the key would return the blue rendition; given "emph", the key would return the red rendition.

Upvotes: 4

Views: 301

Answers (2)

Klortho
Klortho

Reputation: 823

I found that the following, which uses a second key(), works with xsltproc, so, if that's your target processor, this should help. It doesn't work with Saxon, though.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:key name='kTagUsage' match='tagUsage' use='@render'/>
  <xsl:key name="kRendByUsageGi" match="rendition"
    use="key('kTagUsage', @xml:id)/@gi"/>


  <xsl:template match="/">
    <xsl:copy-of select="key('kRendByUsageGi', 'p')/text()"/>
    ========
    <xsl:copy-of select="key('kRendByUsageGi', 'emph')/text()"/>
  </xsl:template>
</xsl:stylesheet>

Upvotes: 1

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243469

Use:

 <xsl:key name="kRendByUsageGi" match="rendition"
  use="../tagUsage[@render=current()/@xml:id]/@gi"/>

Here is a complete verification:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="kRendByUsageGi" match="rendition"
  use="../tagUsage[@render=current()/@xml:id]/@gi"/>

 <xsl:template match="/">
  <xsl:copy-of select="key('kRendByUsageGi', 'p')/text()"/>
========
  <xsl:copy-of select="key('kRendByUsageGi', 'emph')/text()"/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided XML document:

<t>
  <rendition xml:id="b">color: blue</rendition>
  <rendition xml:id="r">color: red</rendition>

  <tagUsage gi="p" render="b" />
  <tagUsage gi="emph" render="r" />
</t>

the wanted, correct result is produced:

color: blue
========
  color: red

Upvotes: 4

Related Questions