sboti8m
sboti8m

Reputation: 55

XSL xsl:evaluate multiple nodes

How can I make sure, that xsl:evaluate returns a sequence of nodes, in case the xPath matches multiple nodes.

Suppose the following input.xml

<numlist key="K1">
 <numlitm key="K2">C1</numlitm>
 <numlitm key="K2">C2</numlitm>
 <numlitm key="K3">C3</numlitm>                            
</numlist>

And the following XSL

<?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" xmlns:xst="http://www.w3.org/1999/XSL/Transform"
                exclude-result-prefixes="xs"
                version="3.0">

    <xsl:variable name="main-doc" select="/"/>

    <xsl:template match="/">
        <xsl:variable name="var">//*[@key='K2']</xsl:variable>
        <xsl:variable name="eval">
            <xsl:evaluate xpath="$var" context-item="$main-doc" />
        </xsl:variable>
        <xsl:copy-of select="$eval"/>
    </xsl:template>

</xsl:stylesheet>

The result is

<numlitm key="K2">C1</numlitm>
<numlitm key="K2">C2</numlitm>

I would like the result, stored in eval, to be a sequence of two elements, so that I can iterate over the two numlitm via a for-each loop (I need this to construct a key). The issue is that the result seems not to be a node sequence, thus a for-each iteration will yield both numlitm rows, instead of each row in one seperate iteration

Upvotes: 3

Views: 401

Answers (3)

michael.hor257k
michael.hor257k

Reputation: 117073

The issue is that the result seems not to be a node sequence, thus a for-each iteration will yield both numlitm rows, instead of each row in one seperate iteration

Your assumption is false. It is probably based on incorrect implementation of the xsl:for-each instruction - which your question neglects to show. If you do:

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

<xsl:variable name="main-doc" select="/"/>
 
<xsl:template match="/">
    <xsl:variable name="var">//*[@key='K2']</xsl:variable>
    <xsl:variable name="eval">
        <xsl:evaluate xpath="$var" context-item="$main-doc" />
    </xsl:variable>
    <output>
        <xsl:for-each select="$eval/numlitm">
            <item>
                <xsl:value-of select="." />
            </item>
        </xsl:for-each>
    </output>
</xsl:template>

</xsl:stylesheet>

you will get the expected result of:

<?xml version="1.0" encoding="UTF-8"?>
<output>
   <item>C1</item>
   <item>C2</item>
</output>

Demo: https://xsltfiddle.liberty-development.net/eiorv1f

Upvotes: 0

Andreas Hauser
Andreas Hauser

Reputation: 92

If I understand you correctly, you want a serial string for the same key. → Than you could try something like this:

<root>
 <xsl:for-each-group select="//numlitm" group-by="@key">
 <xsl:variable name="currentKey" select="@key"/>
 <SameKey>
  <!-- Serialize Nodestrings -->
  <xsl:variable name="nodestrings">
    <xsl:apply-templates select="//numlitm[@key=$currentKey]/text()" mode="serialize"/> <!-- get all tag-values for current group-key --> 
  </xsl:variable>
  <key><xsl:value-of select="$currentKey"/></key>
  <keyValues><xsl:value-of select="$nodestrings"/></keyValues>
 </SameKey>
 </xsl:for-each-group>
</root>

For this you will receive this output:

<root>
 <SameKey>
  <key>K2</key>
  <keyValues>C1C2</keyValues>
 </SameKey>
 <SameKey>
  <key>K3</key>
  <keyValues>C3</keyValues>
 </SameKey>
</root>

Upvotes: 1

Mads Hansen
Mads Hansen

Reputation: 66783

The default type for a variable is a document-node. So, type your variable as a sequence of element(), or at least a sequence of item().

<xsl:variable name="eval" as="element()*">

or

<xsl:variable name="eval" as="item()*">

and then the $eval will be a sequence of the selected items from the evaluated XPath.

Upvotes: 4

Related Questions