2016-12-02 69 views
0

感謝Stack Overflow和@ michael.hor257k我能夠在XML文檔中成功更新名稱空間。使用XSLT從XML中的選擇性節點中刪除選擇性附加名稱空間

現在,只有一個問題。
我的SAP系統無法理解來自XSD的幾個字段,因此添加了自己的名稱空間以適應XML模式。

這可以通過SAP解決,但我們尚未安裝該軟件(SPROXY)。

所以,我必須使用XSLT 1.0來實現這一點。

我原來的XML是:

<?xml version="1.0" encoding="UTF-8"?> 

<n0:eCPR xmlns:prx="urn:sap.com:proxy:DV4:/1SAI/TAS1F59A417878D36573F1D:700:2013/05/24" xmlns:n0="http://www.dir.ca.gov/dlse/CPR-Prod-Test/CPR.xsd"> 

<n0:employees> 
    <n0:employee> 
    <n0:name id="20019768">Paul John</n0:name> 
</n0:employee> 
</n0:employees> 
</n0:eCPR> 

,並用於實現低於要求輸出的XSLT:

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:CPR="http://www.dir.ca.gov/dlse/CPR-Prod-Test/CPR.xsd"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

    <xsl:template match="*"> 
    <xsl:element name="CPR:{local-name()}"> 
    <xsl:copy-of select="@*"/> 
    <xsl:apply-templates/> 
    </xsl:element> 
    </xsl:template> 

與當前的命名空間來更新我的XML後(其中錯誤是)看起來像

<?xml version="1.0" encoding="UTF-8"?> 
<CPR:eCPR xmlns:CPR="http://www.dir.ca.gov/dlse/CPR-Prod-Test/CPR.xsd"> 
<CPR:projectInfo> 
    <CPR:awardingBody xmlns:asx="http://www.sap.com/abapxml" asx:root=""/> 
    <CPR:contractAgencyID xmlns:asx="http://www.sap.com/abapxml" asx:root=""/> 
    <CPR:contractAgency/> 
    <CPR:projectName xmlns:asx="http://www.sap.com/abapxml" asx:root=""/> 
    <CPR:projectID/> 
    <CPR:awardingBodyID/> 
    <CPR:projectNum/> 
    <CPR:contractID/> 

因此,節點awardingBodycontractAgencyIDProject Name是無法由系統得到了正確轉換等領域。

我需要刪除這些命名空間(與該命名空間xmlns:asx節點)。是否有可能通過XSLT。條件:僅刪除其名稱空間是http://www.sap.com/abapxml或者我可以提供節點的名稱(因爲它們是固定的),這些條目的命名空間

理想的結構應該是什麼:

<?xml version="1.0" encoding="UTF-8"?> 
<CPR:eCPR xmlns:CPR="http://www.dir.ca.gov/dlse/CPR-Prod-Test/CPR.xsd"> 
<CPR:projectInfo> 
    <CPR:awardingBody/> 
    <CPR:contractAgencyID /> 
    <CPR:contractAgency/> 
    <CPR:projectName/> 
    <CPR:projectID/> 
    <CPR:awardingBodyID/> 
    <CPR:projectNum/> 
    <CPR:contractID/> 

感謝

+0

請指定XSLT 1.0或2.0。 –

+0

@MichaelKay XSLT 1.0 –

+0

這很混亂。第一個XML是轉換的輸入嗎?根據你以前的2個問題,我懷疑它是輸出。請顯示輸入或澄清。 –

回答

1

我猜你原來的XML實際上看起來是這樣的(!):

XML

<n0:eCPR xmlns:n0="http://www.dir.ca.gov/dlse/CPR-Prod-Test/CPR.xsd" xmlns:asx="http://www.sap.com/abapxml"> 
    <n0:projectInfo> 
     <n0:awardingBody asx:root=""/> 
     <n0:contractAgencyID asx:root=""/> 
     <n0:contractAgency/> 
     <n0:projectName asx:root=""/> 
     <n0:projectID/> 
     <n0:awardingBodyID/> 
     <n0:projectNum/> 
     <n0:contractID/> 
    </n0:projectInfo> 
</n0:eCPR> 

爲了得到所要求的輸出 - 即不復制asx:root屬性 - 你需要做的:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:CPR="http://www.dir.ca.gov/dlse/CPR-Prod-Test/CPR.xsd"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="*"> 
    <xsl:element name="CPR:{local-name()}"> 
    <xsl:copy-of select="@*[not(namespace-uri()='http://www.sap.com/abapxml')]"/> 
     <xsl:apply-templates/> 
    </xsl:element> 
</xsl:template> 

</xsl:stylesheet> 

或許這樣做:

<xsl:copy-of select="@*[not(namespace-uri())]"/> 

刪除任何屬性,這些屬性不在無名字空間中。

+0

@micheal完美..整個問題是固定的。感謝您的回覆和澄清。 –