2011-08-07 64 views
1

我期待將XML1轉換爲XML2(基本上添加其他名稱空間& )。XML消息的XSLT轉換 - 初學者

我被告知這可以通過XSLT完成。這是可以使用XSLT實現的嗎?它有多簡單(或複雜)?

XML1

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE Update_WCS_OrderStatus SYSTEM "Update_WCS_OrderStatus_20.dtd" > 
<Update_WCS_OrderStatus> 
<ControlArea> 
     <Verb value="Constant">test</Verb> 
     <Noun value="Constant">Constant</Noun> 
     <Credentials> 
     <LogonId>a</LogonId> 
     <Password>b</Password> 
     </Credentials> 
    </ControlArea> 
    <DataArea> 
    <OrderStatus> 
     <OrderStatusHeader> 
      <OrderNumber/> 
       </OrderStatusHeader> 
       <OrderStatusItem> 
       <ItemNumber/> 
       </OrderStatusItem> 
      </OrderStatus> 
     </DataArea> 
</Update_WCS_OrderStatus>' 

XML2 
'<?xml version="1.0" encoding="UTF-8"?> 
<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge"> 
    <ns0:Message1> 
    <!DOCTYPE Update_WCS_OrderStatus SYSTEM "Update_WCS_OrderStatus_20.dtd" > 
     <Update_WCS_OrderStatus> 
     <ControlArea> 
      <Verb value="">Constant</Verb> 
      <Noun value="">Constant</Noun> 
      <Credentials> 
       <LogonId>test</LogonId> 
       <Password>test</Password> 
      </Credentials> 
     </ControlArea> 
     <DataArea> 
      <OrderStatus> 
       <OrderStatusHeader> 
        <OrderNumber/> 
       </OrderStatusHeader> 
       <OrderStatusItem> 
        <ItemNumber/> 
       </OrderStatusItem> 
      </OrderStatus> 
     </DataArea> 
     </Update_WCS_OrderStatus> 
    </ns0:Message1> 
</ns0:Messages> 

回答

1

你不能有一個元素節點內部的DOCTYPE節點。 除此之外,您當然可以通過XSLT完成任務:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" 
    xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge"> 

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

<xsl:template match="/"> 
    <ns0:Messages> 
    <ns0:Message1> 
     <xsl:apply-templates/> 
    </ns0:Message1> 
    </ns0:Messages> 
</xsl:template> 

<xsl:template match="ControlArea/Verb[@value = 'Constant'] | ControlArea/Noun[@value = 'Constant']"> 
    <Verb value="">Constant</Verb> 
</xsl:template> 

</xsl:stylesheet>