xslt 删除结果前缀

xslt removnig result prefixes

我有一个这样的文件:

<?xml version='1.0' encoding='UTF-8'?>
<wd:Report_Data xmlns:wd="urn:com.workday.report/Business_Process_Transactions_-_Canceled__Rescinded_or_Corrected">
   <wd:Report_Entry>
       <wd:Worker wd:Descriptor="**** *****">
           <wd:ID wd:type="WID">2b994449ed3b10ba7bc5d65f971ba4d4</wd:ID>
           <wd:ID wd:type="Employee_ID">00083646</wd:ID>
       </wd:Worker>
   </wd:Report_Entry>
</wd:Report_Data>

我用这个xslt改造了一下:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"             
        xmlns:wd="urn:com.workday.report/Business_Process_Transactions_-_Canceled__Rescinded_or_Corrected"
                    exclude-result-prefixes="wd">

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


        <xsl:template match ="/">
            <xsl:apply-templates select="wd:Report_Data/wd:Report_Entry/wd:Worker[@wd:Descriptor!='']" />
        </xsl:template>


        <xsl:template match="wd:Report_Data/wd:Report_Entry/wd:Worker[@wd:Descriptor!='']">
            <wd:Instance_Reference>
                <wd:ID>
                    <xsl:attribute name="wd:type">
                        <xsl:value-of select="wd:ID[@wd:type='Contingent_Worker_ID' or @wd:type='Employee_ID']/@wd:type"/>
                    </xsl:attribute>

                    <xsl:value-of select="wd:ID[@wd:type='Contingent_Worker_ID' or @wd:type='Employee_ID']"></xsl:value-of>
                </wd:ID>
            </wd:Instance_Reference>
        </xsl:template>

    </xsl:stylesheet>

它给了我:

<wd:Instance_Reference xmlns:wd="urn:com.workday.report/Business_Process_Transactions_-_Canceled__Rescinded_or_Corrected">
    <wd:ID wd:type="Employee_ID">00083646</wd:ID>
</wd:Instance_Reference>

我想删除这个元素:

xmlns:wd="urn:com.workday.report/Business_Process_Transactions_-_Canceled__Rescinded_or_Corrected"

没有名称空间声明就不能有前缀。 但是,如果您删除命名空间并留下这样的输出...

<Instance_Reference>
    <ID type="Employee_ID">00083646</ID>
</Instance_Reference>

那么您需要做的就是创建不排除前缀的输出元素。在您的示例中,只需将最终模板更改为此..

<xsl:template match="wd:Report_Data/wd:Report_Entry/wd:Worker[@wd:Descriptor!='']">
        <Instance_Reference>
            <ID>
                <xsl:attribute name="type">
                    <xsl:value-of select="wd:ID[@wd:type='Contingent_Worker_ID' or @wd:type='Employee_ID']/@wd:type"/>
                </xsl:attribute>

                <xsl:value-of select="wd:ID[@wd:type='Contingent_Worker_ID' or @wd:type='Employee_ID']"></xsl:value-of>
            </ID>
        </Instance_Reference>
    </xsl:template>