元素的xml属性

时间:2012-09-15 15:38:27

标签: xml xslt

我有以下输入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>

但是:我使用以下xslt获取此输出xml:

    <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 个答案:

答案 0 :(得分:1)

在XSLT 1.0中(我假设你正在使用;你的示例样式表缺少开始标记),xsl:copy总是copies所有命名空间节点。

如果要避免复制命名空间,则需要使用xsl:element重新创建元素。其namespace属性是可选的。

相关问题