xslt 1.0 - Passing values from one template to another -


i trying passing value 1 template template. although found couple of example in forum i'm unable make it.

below xml:

<pi:paygroup xmlns:pi="urn:com.sample/file">     <pi:header>         <pi:version>17</pi:version>         <pi:payroll_company_name>abcd</pi:payroll_company_name>         <pi:pay_group_id>0307</pi:pay_group_id>     </pi:header>     <pi:employee>         <pi:summary>             <pi:employee_id>12345678</pi:employee_id>             <pi:name>test employee</pi:name>         </pi:summary>         <pi:position>             <pi:business_title>lead learning specialist</pi:business_title>             <pi:worker_type>p</pi:worker_type>             <pi:position_time_type>full_time</pi:position_time_type>             <pi:compensation_effective_date>2017-07-01</pi:compensation_effective_date>             <pi:base_pay_currency>eur</pi:base_pay_currency>             <pi:base_pay_frequency>4-weekly</pi:base_pay_frequency>             <pi:organization_one>0307_3075999496</pi:organization_one>             <pi:organization_two>0307</pi:organization_two>             <pi:supervisor_id>00295975</pi:supervisor_id>             <pi:supervisor_name>simba sang (98765432)</pi:supervisor_name>         </pi:position>         <pi:earnings>             <pi:start_date>2017-02-06</pi:start_date>             <pi:currency>eur</pi:currency>         </pi:earnings>     </pi:employee> </pi:paygroup> 

below xslt in use long time:

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:pi="urn:com.workday/picof"     version="2.0">      <xsl:output omit-xml-declaration="yes" method="xml"/>      <xsl:template match="@*|node()">         <xsl:copy>             <xsl:apply-templates select="@*|node()"/>         </xsl:copy>       </xsl:template>      <!-- 00123456 ... 123456 -->     <xsl:template match="pi:employee_id">         <pi:employee_id>             <xsl:value-of select="substring(.,3)"/>         </pi:employee_id>     </xsl:template>      <xsl:template match="pi:supervisor_id">         <pi:supervisor_id>             <xsl:value-of select="substring(.,3)"/>         </pi:supervisor_id>     </xsl:template>      <xsl:template match="pi:supervisor_name">         <pi:supervisor_name>             <xsl:value-of select="normalize-space(substring-before(.,'('))"/>         </pi:supervisor_name>     </xsl:template>      <xsl:template match="pi:organization_one">         <pi:organization_one>             <xsl:value-of select="if(contains(.,'_'))                 then(normalize-space(substring-after(.,'_')))                 else(.)"/>         </pi:organization_one>     </xsl:template>  </xsl:stylesheet> 

ask here /pi:position/pi:compensation_effective_date value should passed under /pi:earnings_deductions/pi:start_date tag

pi:start_date should have same value pi:compensation_effective_date.

so modified xslt include 2 more template match 1 pi:compensation_effective_date , other 1 /pi:start_date shown below:

<xsl:template match="/pi:compensation_effective_date">     <xsl:param name="test"/>     <xsl:apply-templates>         <xsl:with-param name="test" select="."/>     </xsl:apply-templates>     <pi:compensation_effective_date><xsl:value-of select="$test"/></pi:compensation_effective_date>         </xsl:template>  <xsl:template match="pi:start_date">     <xsl:param name="test"/>     <pi:start_date><xsl:value-of select="$test"/></pi:start_date> </xsl:template> 

had param value in pi:compensation_effective_date template , passed param pi:start_date template. pi:start_date blank; how can compensation_effective_date value in start_date well?

expected output:

<pi:position>     <pi:business_title>...</pi:business_title>     <pi:worker_type>..</pi:worker_type>     <pi:position_time_type>..</pi:position_time_type>     <pi:compensation_effective_date>2017-07-01</pi:compensation_effective_date>     <pi:base_pay_currency>..</pi:base_pay_currency>     <pi:base_pay_frequency>...</pi:base_pay_frequency>     <pi:organization_one>....</pi:organization_one>     <pi:organization_two>....</pi:organization_two>     <pi:supervisor_id>....</pi:supervisor_id>     <pi:supervisor_name>.....</pi:supervisor_name> </pi:position> <pi:earnings>     <pi:start_date>2017-07-01</pi:start_date>     <pi:currency>...</pi:currency> </pi:earnings> 

"in absence of select attribute, xsl:apply-templates instruction processes of children of current node [...]" (w3c). means template matching <start_date> cannot work in context of <compensation_effective_date> because there no <start_date> child element in it. instead, <start_date> template - other templates - matches content , applied without parameters, why <start_date> empty in output.

i couldn't work way intended, might have few hints going. following code-snippets, note removed pi: namespace because caused trouble in editor.

1.) easy solution

it possible refer desired source element if outside of current context. produce desired output:

<xsl:template match="start_date">     <start_date><xsl:value-of select="../../position/compensation_effective_date"/></start_date> </xsl:template> 

be careful using however, e.g. in case employee can have more 1 position-element.

2.) using <xls:call-template> , parameter

i adjusted "call-template-syntax" this answer fit example.

<xsl:template match="employee">     <xsl:copy>         <xsl:apply-templates select="@*|node()"/>     </xsl:copy>      <xsl:call-template name="t">         <xsl:with-param name="p" select="position/compensation_effective_date"/>     </xsl:call-template> </xsl:template>  <xsl:template name="t">     <xsl:param name="p"/>     <start_date><xsl:value-of select="$p"/></start_date> </xsl:template> 

the parameter transferred template "t" , appears in output, not in right place. problem getting right context , inserting tag there. maybe can build on , provide working solution using parameters.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -