2017-07-27 131 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

相關問題