0

I got the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<batch>
  <batchnummer>782</batchnummer>
  <continueonerror>true</continueonerror>
  <metafileversion>1.0</metafileversion>
<documentset>
  <naam></naam>
  <type></type>
  <subsets>
<subset>
  <staple>true</staple>
  <subdocuments>
    <subdocument>
      <document>thr6UhEw5bER6Cjt8uKOCg</document>
      <stamp></stamp>
      <mediatype>Briefpapier</mediatype>
      <duplex>false</duplex>
    </subdocument>
    <subdocument>
      <document>thr6UhEw5bER6Cjt8uRUiA</document>
      <stamp></stamp>
      <mediatype>Briefpapier</mediatype>
      <duplex>false</duplex>
    </subdocument>
  </subdocuments>
</subset>
<subset>
  <staple>true</staple>
  <subdocuments>
    <subdocument>
      <document>thr6UhEw5bER6Cjt8uSxgA</document>
      <stamp></stamp>
      <mediatype>Blanco</mediatype>
      <duplex>false</duplex>
    </subdocument>
    <subdocument>
      <document>thr6UhEw5bER6Cjt8uSCCg</document>
      <stamp></stamp>
      <mediatype>Blanco</mediatype>
      <duplex>false</duplex>
    </subdocument>
    <subdocument>
      <document>thr6UhEw5bER6Cjt8uKOCg</document>
      <stamp></stamp>
      <mediatype>Briefpapier</mediatype>
      <duplex>false</duplex>
    </subdocument>
    <subdocument>
      <document>thr6UhEw5bER6Cjt8uUH-A</document>
      <stamp></stamp>
      <mediatype>Briefpapier</mediatype>
      <duplex>false</duplex>
    </subdocument>
  </subdocuments>
</subset>
</subsets>
  <documenten>
    <document>
      <naam>00000782_000001.rtf</naam>
      <code>thr6UhEw5bER6Cjt8uKOCg</code>
      <duplex>false</duplex>
      <type>RTF</type>
    </document>
    <document>
      <naam>00000782_000002.rtf</naam>
      <code>thr6UhEw5bER6Cjt8uRUiA</code>
      <duplex>false</duplex>
      <type>RTF</type>
    </document>
    <document>
      <naam>00000782_000003.rtf</naam>
      <code>thr6UhEw5bER6Cjt8uSCCg</code>
      <duplex>false</duplex>
      <type>RTF</type>
    </document>
    <document>
      <naam>00000782_000004.rtf</naam>
      <code>thr6UhEw5bER6Cjt8uSxgA</code>
      <duplex>false</duplex>
      <type>RTF</type>
    </document>
    <document>
      <naam>00000782_000005.rtf</naam>
      <code>thr6UhEw5bER6Cjt8uUH-A</code>
      <duplex>false</duplex>
      <type>RTF</type>
    </document>
  </documenten>
</documentset>
  <batchcontrole>
    <cntset>29</cntset>
    <cntdoc>75</cntdoc>
    <cntsub>58</cntsub>
  </batchcontrole>
</batch>

and the following XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
    <xsl:for-each select="//subsets/subset">
        <subset>
            <xsl:for-each select="subdocuments/subdocument">
                <documentname><xsl:value-of select="./document"/></documentname>
                <documentcode><xsl:value-of select="//document/naam[../code='./document']/text()"/></documentcode>
            </xsl:for-each>
        </subset>
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Somehow I cant dynamicly select the "naam" node. when i do a specific xpath select example

//document/naam[../code='thr6UhEw5bER6Cjt8uSCCg']/text()

It works fine but as soon as I replace it with current()/document or ./document it doenst retrieve anything anymore...

when I use a static xpath it just works fine and retrieve the information for every foreach loop.

How can I make the xpath dynamicly inside de xslt?

Currently trying to figure out xslt although I cant really get it to work properly...

Tim C
  • 70,053
  • 14
  • 74
  • 93
Patrick Rennings
  • 191
  • 1
  • 5
  • 19

2 Answers2

1

Inside the <xsl:for-each select="subdocuments/subdocument">, you need to store the value of document in a variable

<xsl:variable name="doc" select="document" />

Use the value of this variable to compare with the value of code and extract the required value.

<xsl:value-of select="//document[code=$doc]/naam" />

Another option to extract the value is using ancestor axis.

<xsl:value-of select="ancestor::documentset/documenten/document[code=$doc]/naam" />

Below is the updated template

<xsl:template match="/">
    <xsl:for-each select="//subsets/subset">
        <subset>
            <xsl:for-each select="subdocuments/subdocument">
                <xsl:variable name="doc" select="document" />
                <documentname>
                    <xsl:value-of select="$doc" />
                </documentname>
                <documentcode>
                    <xsl:value-of select="//document[code=$doc]/naam" />
                </documentcode>
            </xsl:for-each>
        </subset>
    </xsl:for-each>
</xsl:template>

This gives the following output

<subset>
    <documentname>thr6UhEw5bER6Cjt8uKOCg</documentname>
    <documentcode>00000782_000001.rtf</documentcode>
    <documentname>thr6UhEw5bER6Cjt8uRUiA</documentname>
    <documentcode>00000782_000002.rtf</documentcode>
</subset>
<subset>
    <documentname>thr6UhEw5bER6Cjt8uSxgA</documentname>
    <documentcode>00000782_000004.rtf</documentcode>
    <documentname>thr6UhEw5bER6Cjt8uSCCg</documentname>
    <documentcode>00000782_000003.rtf</documentcode>
    <documentname>thr6UhEw5bER6Cjt8uKOCg</documentname>
    <documentcode>00000782_000001.rtf</documentcode>
    <documentname>thr6UhEw5bER6Cjt8uUH-A</documentname>
    <documentcode>00000782_000005.rtf</documentcode>
</subset>
Aniket V
  • 3,183
  • 2
  • 15
  • 27
1

The problem with your current expression is that you have enclosed the ./document in apostrophes, which makes it a string literal, rather than an expression.

It should be this (and you do need to use current() too. . represents the context node, current() the current node. See Current node vs. Context node in XSLT/XPath?)

<xsl:value-of select="//document/naam[../code=current()/document]/text()"/>

Or, slightly simpler, this...

<xsl:value-of select="//document[code=current()/document]/naam"/>

Better still, use a key to look up the documents.

<xsl:key name="docs" match="document[code]" use="code" />

Then the expression becomes this....

<xsl:value-of select="key('docs', document)/naam"/>

Try this XSLT

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

<xsl:key name="docs" match="document[code]" use="code" />

<xsl:template match="/">
    <xsl:for-each select="//subsets/subset">
        <subset>
            <xsl:for-each select="subdocuments/subdocument">
                <documentname><xsl:value-of select="document"/></documentname>
                <documentcode><xsl:value-of select="key('docs', document)/naam"/></documentcode>
            </xsl:for-each>
        </subset>
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Tim C
  • 70,053
  • 14
  • 74
  • 93