2013-06-04 55 views
0

我想使用xslt從給定的xml中獲取輸出xml。輸出xml將在給定的xml中添加一些元素。但我沒有獲得所需的輸出。使用XSLT在XML中進行修改

我有一個輸入XML,如:

<Response xmlns="http://xyz.abc/max/" xmlns:ns1="http://xyz.abc/max/"> 
    <out xmlns=""> 
     <Number xmlns="http://xyz.abc/max/">Desc1</Number> 
     <Address xmlns="http://xyz.abc/max">Desc2</Address> 
     <Records xmlns="http://xyz.abc/max">Desc3</Records> 
    </out> 
</Response> 

我想要一個XML輸出,如:

<?xml version="1.0" encoding="UTF-8"?> 
<abc:Reqeust xmlns:abc="http://www.nnn.com/bnm" 
xmlns:ns1="http://xyz.abc/max/" xmlns="http://xyz.abc/max/" > 
    <abc:Tray> 
     <abc:Remote> 
      <abc:ID>ID1</abc:ID> 
      <abc:Distance>Always</abc:Distance> 
     </abc:Remote> 
     <abc:Time> 
      1100-01-01T01:01:01+05:30 
     </abc:Time> 
     <abc:AreaMap /> 
    </abc:Tray> 
    <abc:Area> 
     <abc:Get> 
      <abc:Fault>Token1</abc:Fault> 
     </abc:Get> 
     <Response xmlns="http://xyz.abc/max/" xmlns:ns1="http://xyz.abc/max/"> 
      <out xmlns=""> 
       <Number xmlns="http://xyz.abc/max/">Desc1</Number> 
       <Address xmlns="http://xyz.abc/max">Desc2</Address> 
       <Records xmlns="http://xyz.abc/max">Desc3</Records> 
      </out> 
     </Response> 
    </abc:Area> 
</abc:Reqeust> 

我使用XSLT:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:abc="http://www.nnn.com/bnm" 
xmlns:ns1="http://xyz.abc/max/" xmlns="http://xyz.abc/max/" > 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="Response"> 
     <abc:Request> 
      <abc:Tray> 
       <abc:Remote> 
        <abc:ID>ID1</abc:ID> 
        <abc:Distance>Always</abc:Distance> 
       </abc:Remote> 
       <abc:Time> 
        1100-01-01T01:01:01+05:30 
       </abc:Time> 
       <abc:AreaMap /> 
      </abc:Tray> 
      <abc:Area> 
       <abc:Get> 
        <abc:Fault>Token1</abc:Fault> 
       </abc:Get> 
       <Response> 
        <xsl:apply-templates select="@*|node()"/> 
       </Response> 
      </abc:Area> 
     </abc:Request> 
    </xsl:template> 
</xsl:stylesheet> 

,但我沒有得到所需的輸出。 我應該在xslt中做出什麼改變才能獲得所需的輸出xml?

回答

0

在xslt-1.0中,您始終使用命名空間前綴來訪問屬於命名空間的名稱。
迴應您輸入的XML元素屬於默認命名空間xmlns="http://xyz.abc/max/"
您已將此名稱空間添加爲xmlns:ns1="http://xyz.abc/max/

因此改變你的模板響應形式:

<xsl:template match="Response"> 

到:

<xsl:template match="ns1:Response"> 

這將產生以下輸出:

<?xml version="1.0"?> 
<abc:Request xmlns:abc="http://www.nnn.com/bnm" xmlns:ns1="http://xyz.abc/max/" xmlns="http://xyz.abc/max/"> 
    <abc:Tray> 
     <abc:Remote> 
      <abc:ID>ID1</abc:ID> 
      <abc:Distance>Always</abc:Distance> 
     </abc:Remote> 
     <abc:Time> 
      1100-01-01T01:01:01+05:30 
     </abc:Time> 
     <abc:AreaMap/> 
    </abc:Tray> 
    <abc:Area> 
     <abc:Get> 
      <abc:Fault>Token1</abc:Fault> 
     </abc:Get> 
     <Response> 
      <out xmlns=""> 
       <Number xmlns="http://xyz.abc/max/">Desc1</Number> 
       <Address xmlns="http://xyz.abc/max">Desc2</Address> 
       <Records xmlns="http://xyz.abc/max">Desc3</Records> 
      </out> 
     </Response> 
    </abc:Area> 
</abc:Request>