2017-08-29 15 views
0

以下是我的要求 我有訂單輸入包含項目,每個項目包含子項目,但標籤名稱是相同的「項目」。如果父母和孩子的xml標籤在xslt中具有相同的名稱,如何保持從源到目標的相同結構

<OrderInput> 
    <Item> 
     <ItemId> 
     <ItemName> 
     <ItemDesc> 
     <Item> 
      <ItemId> 
      <ItemName> 
      <ItemDesc> 
      <Item> 
       <ItemId> 
       <ItemName> 
       <ItemDesc> 
      </Item> 
     </Item> 
    </Item> 
    <Item> 
     <ItemId> 
     <ItemName> 
     <ItemDesc> 
     <Item> 
      <ItemId> 
      <ItemName> 
      <ItemDesc> 
     </Item> 
    </Item> 
</OrderInput> 

使用xslt進行轉換後,它應該如下所示。父和chil節點具有相同的名稱「線」,如源「項」

<OrderOutput> 
    <OrderLine> 
    <Line> 
     <LineId> 
     <LineName> 
     <LineDesc> 
     <Line> 
      <LineId> 
      <LineName> 
      <LineDesc> 
      <Line> 
       <LineId> 
       <LineName> 
       <LineDesc> 
      </Line> 
     </Line> 
    </Line> 
    <Line> 
     <LineId> 
     <LineName> 
     <LineDesc> 
     <Line> 
      <LineId> 
      <LineName> 
      <LineDesc> 
     </Line> 
    </Line> 
</OrderOutput> 

請幫我解決這個問題。

+1

在這情況下'OrderLine'輸出來自和它爲什麼關閉? –

+0

對不起..我錯過了它..請假設 –

回答

1

您可以使用元素名稱替換而轉化

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

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

    <xsl:template match="*[starts-with(name(.), 'Item')]"> 
     <xsl:element name="{replace(name(.), '^Item', 'Line')}"> 
      <xsl:apply-templates/> 
     </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 
+0

您好Rupesh ..感謝您的快速回復...我的要求是不是字符串的替換......只是爲了說明目的,我給了輸出格式。 –

+0

@VamsiKrishna因爲你可以簡單地將元素轉換爲元素映射,對於元素嵌套如果你不改變文檔的映射比它不會影響輸出。就像上面代碼的第一個模板一樣。 – Rupesh

相關問題