2015-05-29 196 views
0

我有这样的XML代码:XSLT删除命名空间

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
<soapenv:Body> 
    <GetBillList xmlns="old_uri"> 
    <Case> 
     <CaseID>699677</CaseID> 
     <BillGroup> 
      <BillGroupID>1</BillGroupID> 
     </BillGroup> 
    </Case> 
    <StartDate>2014-06-12</StartDate> 
    <GetBillListResponse> 
     <Case> 
      <CaseID>699677</CaseID> 
      <BillGroup> 
       <BillGroupID>1</BillGroupID> 
       <Bill> 
       <BillNumber>380368215926</BillNumber> 
       <BillTypeAbbreviation>List Bill</BillTypeAbbreviation> 
        <BillPaymentStatusCode> 
        <BillPaymentStatusCode>UNPAID</BillPaymentStatusCode> 
        <tc>2</tc> 
        </BillPaymentStatusCode> 
       <BillCycleCode> 
        <BillCycleCode>Monthly Bill Cycle</BillCycleCode> 
        <tc>MTHL</tc> 
       </BillCycleCode>     
       <PaymentErrorCode/> 
       <FromDate>2014-12-01</FromDate> 
       </Bill> 
       </BillGroup> 
     </Case> 
    </GetBillListResponse> 
    </GetBillList> 
    </soapenv:Body> 
    </soapenv:Envelope> 

我想这XSL代码:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ouaf="old_uri" exclude-result-prefixes="ouaf"> 

    <xsl:output method="xml" version="2.0" omit-xml-declaration="yes" encoding="UTF-8" indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="/"> 
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soapenv:Body> 
     <xsl:apply-templates select="soapenv:Envelope/soapenv:Body/ouaf:GetBillList/ouaf:GetBillListResponse"/> 
    </soapenv:Body> 
</soapenv:Envelope> 
</xsl:template> 

<xsl:template match="*[ouaf:tc]"> 
<xsl:copy> 
<xsl:for-each select="ouaf:tc"> 
<xsl:attribute name="{local-name()}"> 
<xsl:value-of select="."/> 
</xsl:attribute> 
</xsl:for-each> 
<xsl:apply-templates select="node()[not(self::ouaf:tc)]"/> 
</xsl:copy> 
</xsl:template> 

<xsl:template match="ouaf:*"> 
<xsl:element name="{local-name()}" namespace="new_uri"> 
<xsl:apply-templates/> 
</xsl:element> 
</xsl:template> 

<xsl:template match="*[name(.) = name(..)]"> 
<xsl:apply-templates select="node()|@*"/> 
</xsl:template> 
</xsl:stylesheet> 

,并得到这样的输出:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soapenv:Body> 
    <GetBillListResponse xmlns="new_uri"> 
     <Case> 
      <CaseID>699677</CaseID> 
      <BillGroup> 
       <BillGroupID>1</BillGroupID> 
       <Bill> 
        <BillNumber>380368215926</BillNumber> 
        <BillTypeAbbreviation>List Bill</BillTypeAbbreviation> 
        <BillPaymentStatusCode xmlns="old_uri" tc="2">UNPAID</BillPaymentStatusCode> 
        <BillCycleCode xmlns="old_uri" tc="MTHL">Monthly Bill Cycle</BillCycleCode> 
        <PaymentErrorCode/> 
        <FromDate>2014-12-01</FromDate> 
       </Bill> 
      </BillGroup> 
     </Case> 
    </GetBillListResponse> 
</soapenv:Body> 

要求输出:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soapenv:Body> 
    <GetBillListResponse xmlns="new_uri"> 
     <Case> 
      <CaseID>699677</CaseID> 
      <BillGroup> 
       <BillGroupID>1</BillGroupID> 
       <Bill> 
        <BillNumber>380368215926</BillNumber> 
        <BillTypeAbbreviation>List Bill</BillTypeAbbreviation> 
        <BillPaymentStatusCode tc="2">UNPAID</BillPaymentStatusCode> 
        <BillCycleCode tc="MTHL">Monthly Bill Cycle</BillCycleCode> 
        <PaymentErrorCode/> 
        <FromDate>2014-12-01</FromDate> 
       </Bill> 
      </BillGroup> 
     </Case> 
    </GetBillListResponse> 
</soapenv:Body> 
</soapenv:Envelope> 

一切都很好,除了old_uri命名空间即将在那些有tc作为属性的元素。我想从我的输出XML中删除该命名空间。 希望对此有所帮助!

回答

1

当你匹配tc作为一个孩子的元素,您可以通过简单地复制它开始:

但要复制的元素是在旧的命名空间,所以复制的元素会也在旧的命名空间。

与其模仿的元素,你可以创建一个新的,有xsl:element,指定新的名字空间

<xsl:template match="*[ouaf:tc]"> 
    <xsl:element name="{local-name()}" namespace="new_uri"> 
    <xsl:for-each select="ouaf:tc"> 
     <xsl:attribute name="{local-name()}"> 
     <xsl:value-of select="."/> 
     </xsl:attribute> 
    </xsl:for-each> 
    <xsl:apply-templates select="node()[not(self::ouaf:tc)]"/> 
    </xsl:element> 
</xsl:template> 
+0

不过还是我得到的命名空间。我不想要tc属性存在的名称空间。只有new_uri会出现在根节点中。 –

+0

请明确您现在正在获取什么输出以及它与所需输出的不同之处。 –

+0

我现在编辑了我的代码。 –