syomini
syomini

Reputation: 1

How to declare a variable which tie an attribute to another one, in another node?

I need to declare a variable. I would like to link/tie 2 attributes in this variable.

<xsl:variable name="politiciens" select="Trans/Speakers/Speaker/@id='Trans/Episode/Section/Turn/@speaker'"/>`

    XSL =
    <xsl:for-each select="Trans/Speakers/Speaker[@check='yes']">
    <!-- declare a variable which contain @id, and whom will link/tie @speaker from Turn-->
    <xsl:variable name="politiciens" select="@id=//Turn/@Speaker" />
     <tr>
      <td><xsl:value-of select="@name"/></td>
       td><xsl:value-of select="count(Trans/Episode/Section/Turn[@Speaker=$politiciens]/tour/motBDL[@lexeme='JE'])" /></td>
    </tr>
    </xsl:for-each>

XML =

<Trans scribe="ComputerName" audio_filename="Debat Hollande Sarkozy 1998" video_filename="" version="8" version_date="181221">
     <Speakers>
        <Speaker id="spk1" name="Nicolas Sarkozy" check="yes"/>
    </Speakers>
    <Episode>
        <Section type="report" startTime="0" endTime="1408.652">
          <Turn startTime="0" endTime="0.152">
            <Sync time="0"/>
          </Turn>
          <Turn speaker="spk1" startTime="0.152" endTime="3.038">
            <Sync time="0.152"/>
            <tour nbmots="14" id="000000">
              <motBDL lexeme="POUR">pour</motBDL>
    </tour>
    </Turn>
    </Section>
    </Episode>
    </Trans>

Expected = The two attributes to be linked together <xsl:variable name="politiciens" select="@id='@speaker'"/>

Upvotes: 0

Views: 23

Answers (1)

Tim C
Tim C

Reputation: 70638

I think it sounds like you need to read up on keys. If you want to look-up the Turn elements for the current Speaker you can define a key like so

<xsl:key name="turns" match="Turn" use="@speaker" />

Then, within your xsl:for-each that gets the speakers, you can use the key to count the number of turn elements like so:

<xsl:value-of select="count(key('turns', @id)/tour/motBDL)" />

(I've omitted the check on @lexeme='JE' because there isn't any matching value in your XML)

Try this XSLT as a starting point:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="html" indent="yes" />

  <xsl:key name="turns" match="Turn" use="@speaker" />

  <xsl:template match="/">
    <table>
      <xsl:for-each select="Trans/Speakers/Speaker[@check='yes']">
        <tr>
          <td><xsl:value-of select="@name"/></td>
          <td><xsl:value-of select="count(key('turns', @id)/tour/motBDL)" /></td>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>
</xsl:stylesheet>

And, just as an example, you could also define a key to look up the Speaker elements themselves, if you want to list out the individual turns.

For example....

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="html" indent="yes" />

  <xsl:key name="speakers" match="Speaker" use="@id" />

  <xsl:template match="/">
    <table>
      <xsl:for-each select="Trans/Episode/Section/Turn[@speaker != '']">
        <tr>
          <td><xsl:value-of select="key('speakers', @speaker)/@name"/></td>
          <td><xsl:value-of select="@startTime" /></td>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>
</xsl:stylesheet>

Upvotes: 1

Related Questions