Home xstl apply-template (call tempalate) with variable and use in xsl:evaluate
Reply: 2

xstl apply-template (call tempalate) with variable and use in xsl:evaluate

pshemek
1#
pshemek Published in 2017-11-13 10:59:33Z

I have the following xml:

<pi:Payroll_Extract_Employees xmlns:pi="urn:com.workday/picof">
   <pi:company>
      <pi:Employee>
         <pi:name>John Andrews</pi:name>
         <pi:age>23</pi:age>
         <pi:salary>4000</pi:salary>
         <pi:division>Accounting</pi:division>
      </pi:Employee>
   </pi:company>
</pi:Payroll_Extract_Employees>

I am using this transformation:

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:pi="urn:com.workday/picof">
    <xsl:output method="text"/>
    <xsl:template match="pi:Payroll_Extract_Employees/pi:company">
        <xsl:variable name="tabsNames">
            <entry>
                <spreadSheetName>Payroll Data for Taiwan</spreadSheetName>
                <values>
                    <value>
                        <header>Chinese Name</header>
                        <data>/pi:Name</data>
                    </value>
                </values>
            </entry>
        </xsl:variable>
        <xsl:variable name="currentNode" select="."/>
        <xsl:for-each select="$tabsNames/entry">
            <xsl:variable name="dataValues" select="values/value/data"/>

            <xsl:call-template name="employeeRows">
                <xsl:with-param name="rows" select="$dataValues" as="element()*"/>
                <xsl:with-param name="currentNode" select="$currentNode" as="element()"/>
            </xsl:call-template>

        </xsl:for-each>
    </xsl:template>
    <xsl:template name="employeeRows">
        <xsl:param name="rows" select="."/>
        <xsl:param name="currentNode" select="."/>

        <xsl:for-each select="$currentNode/pi:Employee">
            <xsl:variable name="employee" select="." as="element()"/>
            <xsl:for-each select="$rows">
                <xsl:variable name="path" select="."/>
                <xsl:value-of>
                    <xsl:evaluate xpath="$employee || $path" context-item="."/>
                    <!--<xsl:value-of select="$employee/pi:name"/>-->
                </xsl:value-of>
            </xsl:for-each>
        </xsl:for-each>

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

Here I am creating a map $tabNames and the would like to pass map together with current node to employeeRows template. Instead of data values for "rows" parameter I get entire map and instead of current node (pi:Payroll_Extract_Employees/pi:company) for currentNode parameter I receive the same map as for the "rows" parameter. The second problem is that xsl:evaluates changes $employee to a value I would like following to give me the same output

<xsl:evaluate xpath="$employee || $path" context-item="."/>

as this

<xsl:value-of select="$employee/pi:Name"/>

What I have so far throws an error here:

<xsl:evaluate xpath="$employee || $path" context-item="."/>

Error message:

System ID: C:\Users\przemekb\AppData\Local\Temp\Untitled4.xsl Severity: fatal Description: Static error in XPath expression supplied to xsl:evaluate: Unexpected token name "Andrews" beyond end of expression. Expression: { John Andrews 23 4000 Accounting pi:Name} Start location: 36:42 Length: 1

Michael Kay
2#
Michael Kay Reply to 2017-11-13 12:33:37Z
  1. I suspect that in this element

    <data>pi:Summary/pi:Name</data>

the content is intended to be an XPath expression, and that you expect this XPath expression to be evaluated. That's not a correct expectation; use an xsl:value-of instruction.

  1. I suspect that

    <xsl:variable name="dataValues" select="values/value/data"/>

should be

<xsl:variable name="dataValues" select="$tabsName/entry/values/value/data"/>

though I can't account for the output you said you got.

  1. In this:

<xsl:with-param name="currentNode" select="." as="node()*"/>

there's something amiss because "." will always be a singleton, so why does the "as" attribute suggest it might be a sequence?

Michael Kay
3#
Michael Kay Reply to 2017-11-16 18:52:41Z

Perhaps your

<xsl:evaluate xpath="$employee || $path" context-item="."/>

should be either

<xsl:evaluate xpath="'$employee' || $path">
  <xsl:with-param name="employee" select="$employee"/>
</xsl:evaluate>

or simply

<xsl:evaluate xpath="$path" context-item="$employee"/>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.276057 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO