2012-05-09 32 views
2

我有一個XML,需要使用XSLT進行轉換。 XML有自定義標籤,這是沒有得到解決,我沒有得到結果。自定義標記在XSLT轉換中不起作用

以下是XML代碼:

<?xml version="1.0" encoding="UTF-16"?> 
<di:itg_dataImport xmlns:di="http://www.mercury.com/itg/data_import/1.0" 
        xmlns = "http://www.mercury.com/itg/dm/2.0/types" 
        xmlns:common = "http://www.mercury.com/itg/common/2.0/types" 
        xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://www.mercury.com/itg/data_import/1.0 data_import.xsd"> 
    <request> 
    <requestType>Project Issue</requestType> 
    <identifier>3</identifier> 
    <common:simpleField> 
     <common:token>REQ.ASSIGNED_TO_USER_ID</common:token> 
     <common:stringValue>Admin User (DEV)</common:stringValue> 
    </common:simpleField> 
    <common:simpleField> 
     <common:token>REQ.DESCRIPTION</common:token> 
     <common:stringValue>Test - Pls Ignore</common:stringValue> 
    </common:simpleField> 
    <common:simpleField> 
     <common:token>REQ.KNTA_ESCALATION_LEVEL</common:token> 
     <common:stringValue>Project</common:stringValue> 
    </common:simpleField> 
    <common:simpleField> 
     <common:token>REQ.KNTA_MASTER_PROJ_REF</common:token> 
     <common:stringValue>P0912002 IPTV Residential Phase 1</common:stringValue> 
    </common:simpleField> 
    <common:simpleField> 
     <common:token>REQ.PRIORITY_CODE</common:token> 
     <common:stringValue>Normal</common:stringValue> 
    </common:simpleField> 
    <common:simpleField> 
     <common:token>REQ.WORKFLOW_ID</common:token> 
     <common:stringValue>Issue Management Process</common:stringValue> 
    </common:simpleField> 
    </request> 

</di:itg_dataImport> 

以下被稱爲XSLT:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:di="http://www.mercury.com/itg/data_import/1.0" 
        xmlns = "http://www.mercury.com/itg/dm/2.0/types" 
        xmlns:common = "http://www.mercury.com/itg/common/2.0/types" 
        xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://www.mercury.com/itg/data_import/1.0 data_import.xsd" > 

<xsl:output method="xml" indent="yes"/> 
<xsl:template match="/"> 
<requests> 
    <request> 
    <requestType> 
    <xsl:copy-of select="di:itg_dataImport/request/requestType"/> 
    </requestType> 

    </request> 
</requests> 

</xsl:template> 
</xsl:stylesheet> 

所需的輸出是:

項目事件

任何人都可以請幫助在解決和指出我出錯的地方。 感謝

回答

1

XSL文件:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:di="http://www.mercury.com/itg/data_import/1.0" 
    xmlns = "http://www.mercury.com/itg/dm/2.0/types" 
    xmlns:common = "http://www.mercury.com/itg/common/2.0/types" 
    xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.mercury.com/itg/data_import/1.0 data_import.xsd" > 

    <xsl:output method="xml" indent="yes"/> 
    <xsl:template match="/"> 
     <requests> 
      <request> 
       <xsl:for-each select="descendant::*[local-name() = 'requestType']"> 
        <requestType> 
         <xsl:value-of select="text()"/> 
        </requestType> 
       </xsl:for-each> 
      </request> 
     </requests> 
    </xsl:template> 
</xsl:stylesheet> 

結果,文檔

<?xml version="1.0" encoding="utf-8"?> 
<requests xmlns="http://www.mercury.com/itg/dm/2.0/types" xmlns:di="http://www.mercury.com/itg/data_import/1.0" xmlns:common="http://www.mercury.com/itg/common/2.0/types" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <request> 
     <requestType>Project Issue</requestType> 
    </request> 
</requests> 
+1

雖然這個工作,我個人更喜歡告訴別人他們的代碼有什麼問題,而不是給他們一個可行的替代解決方案。這樣,當他們再次犯同樣的錯誤時,他們不太可能回來尋求更多的幫助。 –

1

這是頭號XSLT編碼錯誤:你的元素是一個(默認)命名空間,並您在選擇它們時未指定名稱空間。

+0

+1,我是這個領域的新手,並且正在收集閱讀你的書的知識。主席先生,非常感謝你展示根本原因。 – Cylian

1

爲了讓邁克爾·凱正確地指向更加明確,這裏是如何在一個默認命名空間的文檔工作 - 你的具體情況

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:di="http://www.mercury.com/itg/data_import/1.0" 
        xmlns:x = "http://www.mercury.com/itg/dm/2.0/types" 
        xmlns:common = "http://www.mercury.com/itg/common/2.0/types" 
        xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://www.mercury.com/itg/data_import/1.0 data_import.xsd" > 

<xsl:output method="xml" indent="yes"/> 
<xsl:template match="/"> 
<requests> 
    <request> 
    <requestType> 
    <xsl:copy-of select="di:itg_dataImport/x:request/x:requestType"/> 
    </requestType> 

    </request> 
</requests> 
</xsl:template> 
</xsl:stylesheet> 

我已經只有短短的發改變你原有的代碼

  1. XSLT樣式表不再有一個默認的命名空間 - 而不是它有一個前綴指定相同的命名空間。

  2. select屬性xsl:copy-of現在具有所有名稱的前綴。

當這種變換所提供的XML文檔應用:

<di:itg_dataImport xmlns:di="http://www.mercury.com/itg/data_import/1.0" 
        xmlns = "http://www.mercury.com/itg/dm/2.0/types" 
        xmlns:common = "http://www.mercury.com/itg/common/2.0/types" 
        xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://www.mercury.com/itg/data_import/1.0 data_import.xsd"> 
    <request> 
    <requestType>Project Issue</requestType> 
    <identifier>3</identifier> 
    <common:simpleField> 
     <common:token>REQ.ASSIGNED_TO_USER_ID</common:token> 
     <common:stringValue>Admin User (DEV)</common:stringValue> 
    </common:simpleField> 
    <common:simpleField> 
     <common:token>REQ.DESCRIPTION</common:token> 
     <common:stringValue>Test - Pls Ignore</common:stringValue> 
    </common:simpleField> 
    <common:simpleField> 
     <common:token>REQ.KNTA_ESCALATION_LEVEL</common:token> 
     <common:stringValue>Project</common:stringValue> 
    </common:simpleField> 
    <common:simpleField> 
     <common:token>REQ.KNTA_MASTER_PROJ_REF</common:token> 
     <common:stringValue>P0912002 IPTV Residential Phase 1</common:stringValue> 
    </common:simpleField> 
    <common:simpleField> 
     <common:token>REQ.PRIORITY_CODE</common:token> 
     <common:stringValue>Normal</common:stringValue> 
    </common:simpleField> 
    <common:simpleField> 
     <common:token>REQ.WORKFLOW_ID</common:token> 
     <common:stringValue>Issue Management Process</common:stringValue> 
    </common:simpleField> 
    </request> 

</di:itg_dataImport> 

想要的,正確的結果產生:

<requests xmlns:di="http://www.mercury.com/itg/data_import/1.0" xmlns:x="http://www.mercury.com/itg/dm/2.0/types" xmlns:common="http://www.mercury.com/itg/common/2.0/types" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <request> 
     <requestType> 
      <requestType xmlns="http://www.mercury.com/itg/dm/2.0/types">Project Issue</requestType></requestType> 
    </request> 
</requests> 

的規則要記住 :XPath將前綴名稱視爲屬於「無名稱」空間」。爲了解決這個問題,請在您的XSLT代碼中定義此名稱空間,但使用前綴 - 然後爲此名稱前綴加上前綴。

+0

+1,不可取消! – Cylian

+0

@Cylian:不客氣。 –