2015-11-05 88 views
0

我是XSL的新手,我需要一些細節來實現這個過程。 我想用xsl實現一個模板以獲得父節點上的多個outputbased,請你幫助我。xsl如何將一個節點拆分爲多個

This is the input required now : 

<parent> 
<company> 
<liv dt="2015-10-22" > 
<Qty type ="NET"> 55</Qty> 
<Qty type ="FAR"> 558</Qty> 
<Qty type ="MAE"> 1222</Qty> 
<HR amt ="NET"> 55</HR> 
<Risk> adsm </Risk> 
</liv> 

<company> 
</parent> 

this is the output desired:I need to seprate the companies by Type, the other records should always appear as they were, Thanks a lot for the help: 

<parent> 

<company> 
<liv dt="2015-10-22" ><Qty type ="NET"> 55</Qty> 
<HR amt ="NET"> 55</HR> 
<Risk> adsm </Risk> 
</liv> 
</company> 

<company> 
<liv dt="2015-11-1" ><Qty type ="FAR"> 558</Qty> 
<HR amt ="NET"> 55</HR> 
<Risk> adsm </Risk> 
</liv> 
</company> 

<company> 
<liv dt="2015-10-28" ><Qty type ="MAE"> 1222</Qty> 
<HR amt ="NET"> 55</HR> 
<Risk> adsm </Risk> 
</liv> 
</company> 

</parent> 
+0

你的問題不清楚。爲什麼FAR和MAE在一起,但NET是分開的? 「新價值:」的意義是什麼?請顯示您期望的**精確**輸出。同時指出是否使用XSLT 1.0或2.0。 –

+0

對不起,沒有任何新的價值 – user3145787

回答

0

寫出company元素的模板處理孩子,然後在模板liv與父把它包:在http://xsltransform.net/3NJ38YT

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="company"> 
     <xsl:apply-templates/> 
    </xsl:template> 

    <xsl:template match="liv"> 
     <xsl:element name="{name(..)}"> 
      <xsl:copy-of select="."/> 
     </xsl:element> 
    </xsl:template> 
</xsl:transform> 

在線。

對於編輯輸入和輸出樣本是需要的,這裏更代碼是XSLT:

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

    <xsl:strip-space elements="*"/> 
    <xsl:output indent="yes"/> 

    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="@*|node()" name="identity" mode="sub"> 
     <xsl:param name="anchor"/> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()" mode="sub"> 
       <xsl:with-param name="anchor" select="$anchor"/> 
      </xsl:apply-templates> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="company"> 
     <xsl:apply-templates select="liv/Qty" mode="split"/> 
    </xsl:template> 

    <xsl:template match="Qty" mode="split"> 
     <xsl:apply-templates select="ancestor::company" mode="sub"> 
      <xsl:with-param name="anchor" select="current()"/> 
     </xsl:apply-templates> 
    </xsl:template> 

    <xsl:template match="Qty" mode="sub"> 
     <xsl:param name="anchor"/> 
     <xsl:if test="generate-id() = generate-id($anchor)"> 
      <xsl:call-template name="identity"/> 
     </xsl:if> 
    </xsl:template> 
</xsl:transform> 

在線在http://xsltransform.net/3NJ38YT/1

+0

非常感謝您的幫助,我想知道一些事情,對於您沒有在路徑中提及它的節點父節點,它不是必需的嗎? – user3145787

+0

樣式表中的第一個模板處理需要複製的任何節點(如'parent'元素),然後爲那些需要轉換的節點添加更多特定的模板。 –

+0

請編輯您的問題與任何額外的代碼示例,格式良好,請,以便我們可以推斷你想要達到什麼。 –