0

The variable $authors is declared as follows:

<listAuthor>
    <author catalogNumber="26A.18.1" fullName="Pjotr Domanski"/>
    <author catalogNumber="26A.19.1" fullName="Hermine Rex"/>
    <author catalogNumber="26A.19.2" fullName="Christoferus Hohenzell"/>
</listAuthor>

Further, the following XML input is given:

<h1:Block Type="obj">
  <h1:Field Type="5000" Value="77772001" />
  <h1:Field Type="5209" Value="26A.19.1 : Lazy Tennis"/>
</h1:Block>

The expected output is:

<h1:Block Type="obj">
  <h1:Field Type="5000" Value="77772001" />
  <h1:Field Type="5209" Value="26A.19.1 : Lazy Tennis"/>
  <h1:Field Type="9904" Value="Hermine Rex"/>
</h1:Block>

Here is a snippet of my transformation:

<xsl:template match="h1:Block">
    <h1:Block Type="obj">
        <xsl:copy-of select="child::*"/>
        <h1:Field Type="9904">
            <xsl:attribute name="Value"
               select="$authors/listAuthor/author[contains (h1:Field[@Type='5209']/@Value, @catalogNumber)]/@fullName"/>
        </h1:Field>
    </h1:Block>
</xsl:template>

The name of the author should be put in h1:Field[@Type='9904']/@Value, when @catalogNumber in $authors/author is contained in h1:Field[@Type='5209']. Unfortunately, the attribute @Value remains empty.

I found the following workaround by introducing a variable:

<xsl:template match="h1:Block">
    <h1:Block Type="obj">
        <xsl:copy-of select="child::*"/>
        <xsl:variable name="value5209" select="h1:Field[@Type='5209']/@Value"/>
        <h1:Field Type="9904">
            <xsl:attribute name="Value"
               select="$authors/listAuthor/author[contains ($value5209, @catalogNumber)]/@fullName"/>
        </h1:Field>
    </h1:Block>
</xsl:template>

In this case, it works fine. Does anyone have any idea why it doesn't work in the first case? I use Saxon Saxon-HE 9.6.

cassiomolin
  • 124,154
  • 35
  • 280
  • 359

1 Answers1

0

Inside the predicate the context node is the author element which does not have a h1:Field, so change $authors/listAuthor/author[contains (h1:Field[@Type='5209']/@Value, @catalogNumber)]/@fullName to $authors/listAuthor/author[contains (current()/h1:Field[@Type='5209']/@Value, @catalogNumber)]/@fullName to select the context node of the template.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • **It works**. Thanks so much! The difference between context node and current node is explained in [link]http://stackoverflow.com/questions/1022345/current-node-vs-context-node-in-xslt-xpath – Michael Freiberg Oct 23 '15 at 11:50