2012-09-15 252 views
0

我有以下輸入XML:XML屬性元素

<?xml version='1.0' encoding='UTF-8'?> 
    <fde-request xmlns="http://xml-schemas.xxx.com/bb/xxx.xsd" 
     xmlns:cbe="http://xml-schemas.xxx.com/bb/xxx.xsd" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://xml-schemas.xxx.com/bb/xxx.xsd xxx.xsd"> 

     <cbe:request-header 
     user-id="mde" 
     session-token="433" 
     audit-id="9999" 
     pearl-code="ca" 
     interface-id="mf" 
     system-name="sr" 
     function-code="image" 
     /> 

    <fde-parms 
     function-code='b' 
     sccf-serial='042463452400' 
     type-process='H'> 
    </fde-parms> 
    </fde-request> 

我需要複製屬性的新元素值,以得到下面的輸出XML:

<?xml version='1.0' encoding='UTF-8'?> 
    <fde-request xmlns="http://xml-schemas.xxx.com/bb/xxx.xsd" 
     xmlns:cbe="http://xml-schemas.xxx.com/bb/xxx.xsd" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://xml-schemas.xxx.com/bb/xxx.xsd xxx.xsd"> 

     <REQUEST-HEADER> 
     <REQUEST-ID>mde</REQUEST-ID> 
     <REQUEST-PEACODE>ca</REQUEST-PEACODE> 
     </REQUEST-HEADER> 

    <fde-parms 
     function-code='b' 
     sccf-serial='042463452400' 
     type-process='H'> 
    </fde-parms> 
    </fde-request> 

但是:我得到這個輸出XML與下面的XSLT:

<xsl:template match="@*|node()"> 
     <xsl:copy> 
     <xsl:apply-templates /> 
     </xsl:copy> 
    </xsl:template> 
     <xsl:template match="*[local-name(.)='request-header']"> 
     <xsl:variable name="sysname" select="@system-name"/> 
     <xsl:variable name="peacode" select="@pearl-code"/> 

    <REQUEST-HEADER> 
    <REQUEST-ID><xsl:value-of select="$sysname"/></REQUEST-ID> 
    <REQUEST-PEACODE><xsl:value-of select="$peacode"/></REQUEST-PEACODE> 
    </REQUEST-HEADER> 
     </xsl:template> 
    </xsl:stylesheet> 

產生錯誤的輸出:xmlns=""被填充,其不是我想要的。

<?xml version='1.0' encoding='UTF-8'?> 
    <fde-request xmlns="http://xml-schemas.xxx.com/bb/xxx.xsd" 
     xmlns:cbe="http://xml-schemas.xxx.com/bb/xxx.xsd" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://xml-schemas.xxx.com/bb/xxx.xsd xxx.xsd"> 

     <REQUEST-HEADER **xmlns=""**> 
     <REQUEST-ID>mde</REQUEST-ID> 
     <REQUEST-PEACODE>ca</REQUEST-PEACODE> 
     </REQUEST-HEADER> 

    <fde-parms 
     function-code='b' 
     sccf-serial='042463452400' 
     type-process='H'> 
    </fde-parms> 
    </fde-request> 

我需要刪除這個不必要的空名稱空間。
你將如何修改xslt來產生正確的輸出?

回答

1

在XSLT 1.0中(我假定您正在使用;示例樣式表缺少開始標記),xsl:copy總是copies也是所有的命名空間節點。

如果您想避免複製命名空間,則需要使用xsl:element重新創建該元素。其namespace屬性是可選的。