2012-07-19 88 views
-1

節點我有一個XML輸入如下複製兩名來自XML使用XSLT

輸入XML

<description-page> 
    <noted>12000 </noted> 
    <noted>15000</noted> 
    <noted>NOTE</noted> 
</description-page> 
<idescription-note> 
<noted>12000</noted> 
<noted>15000</noted> 
<noted>ENG CHG</noted> 
</idescription-note> 

我想我的輸出

<sample> 
<input> 
    <noted>12000</noted> 
    <noted>12000</noted> 
</input> 
<input> 
    <noted>15000</noted> 
    <noted>15000</noted> 
</input> 
<input> 
    <noted>NOTE</noted> 
    <noted>ENG CHG</noted> 
</input> 
</sample> 

所以這裏的每一個描述頁(注意)元素

我現在正在做的是在xslt

<xsl-template match="description-page | idescription-note> 

這是我如何嘗試我的xslt,但我沒有在如何匹配兩個節點掙扎。

請在這裏引導我。

問候 Karthic

回答

0

您可以使用position()同步的description-pageidescription-note節點的相對位置,就像這樣:

<xsl:template match="description-page"> 
    <sample> 
     <xsl:for-each select="noted"> 
      <input> 
       <xsl:variable name="position" select="position()" /> 
       <noted> 
        <xsl:value-of select="//description-page/noted[$position]/text()"/> 
       </noted> 
       <noted> 
        <xsl:value-of select="//idescription-note/noted[$position]/text()"/> 
       </noted> 
      </input> 
     </xsl:for-each> 
    </sample> 
</xsl:template> 

編輯下面也實現了這個不爲,每個

<xsl:template match="description-page"> 
    <sample> 
     <xsl:apply-templates select="noted"/> 
    </sample> 
</xsl:template> 

<xsl:template match="noted"> 
    <input> 
     <xsl:variable name="position" select="position()" /> 
     <noted> 
      <xsl:value-of select="./text()"/> 
     </noted> 
     <noted> 
      <xsl:value-of select="//idescription-note/noted[$position]/text()"/> 
     </noted> 
    </input> 
</xsl:template> 

<!--Suppress the description-page tree entirely--> 
<xsl:template match="idescription-note"> 
</xsl:template> 

I ha ven't試圖與大型XML,但與樣品

  • 的foreach:樣式表執行時間:94.91毫秒
  • 應用模板:樣式表執行時間:94.93毫秒

所以大同小異的?具有性能的一件事是用您真正的完整路徑替換此節點的路徑。

+0

喜@nonnb代替的for-each是否有任何的方法,因爲如果正在使用換每個爲XML我會有很多的數據我認爲這可能會減慢我的過程是,我們可以使用任何其他方法 – karthic 2012-07-19 13:01:23

+0

@karthic - 我已經更新,只使用* apply-templates * – StuartLC 2012-07-19 13:16:45

+0

非常感謝你 – karthic 2012-07-19 13:39:03

0

假設輸入是

<root> 
<description-page> 
    <noted>12000 </noted> 
    <noted>15000</noted> 
    <noted>NOTE</noted> 
</description-page> 
<idescription-note> 
<noted>12000</noted> 
<noted>15000</noted> 
<noted>ENG CHG</noted> 
</idescription-note> 
</root> 

然後樣式表

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

<xsl:output indent="yes"/> 

<xsl:variable name="id-notes" select="//idescription-note/noted"/> 

<xsl:template match="root"> 
    <sample> 
    <xsl:apply-templates select="description-page/noted"/> 
    </sample> 
</xsl:template> 

<xsl:template match="description-page/noted"> 
    <input> 
    <xsl:copy-of select="."/> 
    <xsl:variable name="pos" select="position()"/> 
    <xsl:copy-of select="$id-notes[$pos]"/> 
    </input> 
</xsl:template> 

</xsl:stylesheet> 

輸出

<sample> 
    <input> 
     <noted>12000 </noted> 
     <noted>12000</noted> 
    </input> 
    <input> 
     <noted>15000</noted> 
     <noted>15000</noted> 
    </input> 
    <input> 
     <noted>NOTE</noted> 
     <noted>ENG CHG</noted> 
    </input> 
</sample> 
+0

hi @martin非常感謝你 – karthic 2012-07-19 13:39:30