2017-07-27 194 views
0

我试图将一个模板的值传递给另一个模板。尽管我在这个论坛中发现了一些例子,但我无法做到。将值从一个模板传递到另一个模板

下面是我的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> 

下面是一个在使用很长一段时间的XSLT:

<?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> 

    <!-- 0... 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> 

问这里是/ PI:位置/ PI:Compensation_Effective_Date值应也可以通过/ pi下:Earnings_Deductions/pi:Start_Date标记

pi:Start_Date应该与pi:Compensation_Effective_Date具有相同的值。

所以我修改了XSLT为包括两个多个模板匹配一个用于PI:Compensation_Effective_Date和另一个用于/ PI:START_DATE,如下所示:

<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> 

已在PI一个PARAM值:compensation_effective_date模板并通过该参数为pi:start_date模板。但是pi:Start_Date总是空白;我怎样才能在start_date中获得compensation_effective_Date值?

预期输出:

<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> 

+0

我建议您将示例降低到演示问题所需的最小值 - 请参阅:[mcve]。 –

回答

0

在没有选择的属性,所述xsl:apply-templates指令处理所有当前节点的子节点的[...]”(W3C) 。 这意味着您的模板匹配<Start_Date>无法在<Compensation_Effective_Date>的上下文中工作,因为其中没有<Start_Date>子元素。 相反,<Start_Date>模板(与其他模板一样)只是匹配内容并且没有参数,因此<Start_Date>在输出中为空。

我无法按照你想要的方式工作,但我可能有一些提示让你走。 对于下面的代码片段,请注意我删除了pi:命名空间,因为它在我的编辑器中造成了麻烦。

1)易于溶液

它可以简单地是指,即使它是当前上下文之外所需的源元件。这将产生所需的输出:

<xsl:template match="Start_Date"> 
    <Start_Date><xsl:value-of select="../../Position/Compensation_Effective_Date"/></Start_Date> 
</xsl:template> 

然而,小心使用它,例如,如果员工可以有多个职位元素。

2)使用<xls:call-template>和参数

我调整了“呼叫模板语法”从this answer,以适应你的榜样。

<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> 

该参数成功传输到模板“t”并出现在输出中,但不在正确的位置。 现在问题是进入正确的上下文并在那里插入标签。 也许有人可以在此基础上提供使用参数的工作解决方案。

+1

W3Schools **不** ** W3C和(不出所料)你引用他们的句子是不正确的。 'xsl:apply-templates'不会**将模板应用到当前元素(除非您明确选择它)。如果确实如此,你将陷入无限循环。 - 我没有评论你的答案的其余部分,因为我没有读过这个问题(太长)。 –

+0

感谢您的提示,我的坏!我改变了报价和来源。 – everdream

相关问题