Question

I was helping another user when we ran into this problem.

I have this piece of XSLT where I try to create two keys, one with the first occurrence (in the same parent) of an element containing a given value on a descendant and a second one with all other occurrences for the same value on the descendant. (sorry about bad english)

This is the first key, for which the goal is to create a set with "The first of the siblings", for a given Record/ID, indexed by its generate-id() value:

  <xsl:key name ="key1" match="DataPage[not(
      preceding-sibling::DataPage/Record/ID = Record/ID
    )]"
    use="generate-id()"/>

In the second key I try to get all DapaPage elements which are "NOT the first of the siblings", for a given Record/ID, indexed by generate-id() of "The first of the siblings" with the same Record/ID:

  <xsl:key name="key2" match="DataPage[
      preceding-sibling::DataPage/Record/ID = Record/ID
    ]"
    use="generate-id(preceding-sibling::DataPage[
      Record/ID = current()/Record/ID
    ][last()])" />

And the templates

  <xsl:template match="/*">
      <xsl:copy>
        <xsl:apply-templates select="DataPage"/>
      </xsl:copy>
  </xsl:template>
  <xsl:template match="DataPage">
    <xsl:copy>
      <xsl:for-each select="key('key1',generate-id())">
        <Key1>
          <xsl:copy-of select="."/>
        </Key1>
      </xsl:for-each>
      <xsl:for-each select="key('key2',generate-id())">
        <Key2>
          <xsl:copy-of select="."/>
        </Key2>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

I tested the XSLT with this XML:

<?xml version="1.0" encoding="UTF-8"?>
<Page>
    <DataPage>
        <Record>
            <ID>0</ID>
            <DESC>AAA</DESC>
            <AMOUNT>11</AMOUNT>
        </Record>
    </DataPage>
    <DataPage>
        <Record>
            <ID>0</ID>
            <DESC>BBB</DESC>
            <AMOUNT>22</AMOUNT>
        </Record>
    </DataPage>
    <DataPage>
        <Record>
            <ID>0</ID>
            <DESC>CCC</DESC>
            <AMOUNT>333</AMOUNT>
        </Record>
    </DataPage>
</Page>

Transforming it with Xalan I get, as expected, the following result:

<Page>
  <DataPage>
    <Key1>
      <DataPage>
        <Record>
            <ID>0</ID>
            <DESC>AAA</DESC>
            <AMOUNT>11</AMOUNT>
        </Record>
      </DataPage>
    </Key1>
    <Key2>
      <DataPage>
        <Record>
            <ID>0</ID>
            <DESC>BBB</DESC>
            <AMOUNT>22</AMOUNT>
        </Record>
      </DataPage>
    </Key2>
    <Key2>
      <DataPage>
        <Record>
            <ID>0</ID>
            <DESC>CCC</DESC>
            <AMOUNT>333</AMOUNT>
        </Record>
      </DataPage>
    </Key2>
  </DataPage>
  <DataPage/>
  <DataPage/>
</Page>

However when I use the libXML xsltproc I get only this:

<Page>
  <DataPage>
    <Key1>
      <DataPage>
        <Record>
            <ID>0</ID>
            <DESC>AAA</DESC>
            <AMOUNT>11</AMOUNT>
        </Record>
      </DataPage>
    </Key1>
  </DataPage>
  <DataPage/>
  <DataPage/>
</Page>

What is wrong with the construction of Key2, or is this a xslproc bug?

Was it helpful?

Solution

Hard to say for certain, but yes, this looks like an error in xsltproc. At least, I don't see anything wrong in your key constructor and Saxon 9.4.0.3 agrees with Xalan and not with xsltproc on the output.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top