XSLT 在输出中保留命名空间

XSLT keep namespace on output

我需要帮助在 XSLT 转换中保留命名空间。我看到其他线程有解决方案,但我不明白。当我使用下面的 XSLT 转换时,所有名称空间都消失了。以下是我要完成的工作。任何帮助深表感谢!谢谢!

变换前XML:

<?xml version='1.0' encoding='UTF-8'?>
<Worker
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:this="this.file/eib"
    xmlns:is="java:com.workday.esb.intsys.xpath.ParsedIntegrationSystemFunctions"
    xmlns:tv="java:com.workday.esb.intsys.TypedValue">
    <Detail>
        <EmployeeID>123456</EmployeeID>
        <PayCode>Earning</PayCode>
        <Amount>4243.20</Amount>
    </Detail>
    <Detail>
        <EmployeeID>123456</EmployeeID>
        <PayCode>Deduction</PayCode>
        <Amount>2265.60</Amount>
    </Detail>
</Worker>

我的 XSLT 转换:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" exclude-result-prefixes="xsl wd is xsd this env"
    xmlns:wd="urn:com.workday/bsvc" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:is="java:com.workday.esb.intsys.xpath.ParsedIntegrationSystemFunctions"
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:this="urn:this-stylesheet">

    <xsl:output indent="yes" method="xml"/>

    <xsl:template match="/">
        <xsl:copy>
            <Worker>
                <xsl:for-each select="Worker" >
                    <Detail>
                        <EmployeeID><xsl:value-of select="Detail[(PayCode ='Earning')]/EmployeeID"/></EmployeeID>
                        <PayCode><xsl:value-of select="Detail[(PayCode ='Earning')]/PayCode"/></PayCode>
                        <Amount><xsl:value-of select="Detail[(PayCode ='Earning') and (string-length(Amount) > 0)]/Amount"/></Amount>
                    </Detail>
                </xsl:for-each>
            </Worker>
        </xsl:copy>
    </xsl:template>  
</xsl:stylesheet>

期望的输出:

<?xml version="1.0" encoding="UTF-8"?>
<Worker
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:this="this.file/eib"
    xmlns:is="java:com.workday.esb.intsys.xpath.ParsedIntegrationSystemFunctions"
    xmlns:tv="java:com.workday.esb.intsys.TypedValue">
   <Detail>
      <EmployeeID>123456</EmployeeID>
      <PayCode>Earning</PayCode>
      <Amount>4243.20</Amount>
   </Detail>
</Worker>

我看不出保留输出中未使用的名称空间声明的充分理由。但是如果你真的想要它们,为什么不简单地复制它们(作为它们父元素的一部分)——例如:

XSLT

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

<xsl:template match="/Worker">
    <xsl:copy>
        <xsl:copy-of select="Detail[PayCode ='Earning']"/>
    </xsl:copy>
</xsl:template> 

</xsl:stylesheet>

将此应用于您的输入示例时,结果将是:

<?xml version="1.0" encoding="UTF-8"?>
<Worker xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:this="this.file/eib"
        xmlns:is="java:com.workday.esb.intsys.xpath.ParsedIntegrationSystemFunctions"
        xmlns:tv="java:com.workday.esb.intsys.TypedValue">
   <Detail>
      <EmployeeID>123456</EmployeeID>
      <PayCode>Earning</PayCode>
      <Amount>4243.20</Amount>
   </Detail>
</Worker>

演示:http://xsltransform.net/gVhD8Qt

至少在 XSLT 2.0(您使用的版本)中,命名空间消失了,因为您使用了 exclude-result-prefixes 属性。

只需删除它,XSLT sheet 中包含的所有名称空间,xsl 除外,将按出现顺序显示在输出中。

但是如果您决定省略所有命名空间,那么写 exclude-result-prefixes="#all" 比重新写它们更容易。