Home > Net >  Use current-grouping-key assigned to variable in a predicate
Use current-grouping-key assigned to variable in a predicate

Time:11-22

I'm having trouble, converting an existing xslt1.0 stylesheet to 2.0.

I'm using Saxon as processor.

Problem is at this instruction :

<xsl:for-each-group select="$currentArt/MAPs/typeMAP[ERs/er/code= $ERval]" group-by="refCde">

where the variable "ERval" is set using the current-grouping-key function in another for-each-group.

ERval gets set to a xs:untypedAtomic, and I have the error "Required item type of first operand of '/' is node(), supplied value has item type xs:untypeAtomic."

I tried different things, but i'm missing something.

Can someone expalin, how to achieve that, and point me the documentation section explaining it ?

Thank you for your help.

XML input

<?xml version="1.0" encoding="ISO-8859-1"?>
<art>
    <MAPs>
        <typeMAP>
            <libArt>ANJOU TOLERIE</libArt>
            <ERs>
                <er>
                    <code>18</code>
                </er>
                <er>
                    <code>21</code>
                </er>
            </ERs>
            <refCde>1234</refCde>
        </typeMAP>
        <typeMAP>
            <libArt>ARTICLE HOTELIER</libArt>
        </typeMAP>
        <typeMAP>
        <libArt>BOOK</libArt>
            <ERs>
                <er>
                    <code>18</code>
                </er>
            </ERs>
            <refCde>5678</refCde>
        </typeMAP>
        <typeMAP>
            <libArt>ARTICLE HOTELIER</libArt>
        </typeMAP>
    </MAPs>
</art>

Stylesheet

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    
    <xsl:output method="text" encoding="ISO-8859-1"/>
        
    <xsl:template match="/">
        <xsl:apply-templates select="//art"/>
    </xsl:template>
    
    <xsl:template match="art">
        <xsl:if test="./MAPs/typeMAP/ERs/er/code">
            <xsl:for-each-group select="./MAPs/typeMAP" group-by="ERs/er/code">
                <!--            call template passing ERval-->
                <xsl:call-template name="template">
                    <xsl:with-param name="currentArt" select="."/>
                    <xsl:with-param name="ERval" select="current-grouping-key()"/>
                </xsl:call-template>
            </xsl:for-each-group>
        </xsl:if>
    </xsl:template>
    
    <xsl:template name="template">
        <xsl:param name="currentArt" select="''"/>
        <xsl:param name="ERval" select="()"/>
        <xsl:for-each-group select="$currentArt/MAPs/typeMAP[ERs/er/code = $ERval]" group-by="refCde">
            <xsl:value-of select="current-grouping-key()"/>
            <xsl:text>&#xA;</xsl:text>
        </xsl:for-each-group>

    </xsl:template>
    
</xsl:stylesheet>

CodePudding user response:

I think you want <xsl:for-each-group select="current-group()" group-by="refCde"> instead of <xsl:for-each-group select="$currentArt/MAPs/typeMAP[ERs/er/code = $ERval]" group-by="refCde">.

  • Related