2017-02-15 110 views
0

在我的一個xml有效內容中我需要在下面一行之後添加一個名稱空間。使用XSLT添加名稱空間

<?xml version="1.0" encoding="UTF-8"?> 
<ep:Document xmlns:ep="namespace here" 
xmlns:gk="namespace here" 
xmlns:sh="namespace here" schemaVersion="" creationDate=""> 

在此之後我NEDD添加的命名空間的xmlns:XSI = 「http://www.w3.org/2001/XMLSchema-instance」>

預期的輸出應該

<?xml version="1.0" encoding="UTF-8"?> 
<ep:Document xmlns:ep="namespace here" 
xmlns:gk="namespace here" 
xmlns:sh="namespace here" schemaVersion="" creationDate=""xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 

請問您可以幫忙。 讓我詳細說明我的要求。在實際的輸入消息中,我正在獲取沒有任何名稱空間的有效內容。它看起來像下面。

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

在b/w文件中我們有剩餘的有效載荷。

之後,我已經使用XSLT代碼在有效負載中添加名稱空間和前綴。下面的 是我的XSLT代碼。

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:ep="namespace here" 
xmlns:sh="namespace here" 
xmlns:gk="namespace here"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 


    <!-- identity transform --> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

    <xsl:template match="Document"> 
    <ep:Document> 
    <xsl:apply-templates select="node()|@*"/> 
    </ep:Document> 
    </xsl:template> 

<xsl:template match="Extension|Extension//*"> 
<xsl:element name="gk:{name()}"> 
    <xsl:apply-templates select="node()|@*"/> 
    </xsl:element> 
    </xsl:template> 

<xsl:template match="Header|Header//*"> 
<xsl:element name="sh:{name()}"> 
    <xsl:apply-templates select="node()|@*"/> 
    </xsl:element> 

</xsl:stylesheet> 

使用此代碼我收到看起來像下面的輸出後。

<?xml version="1.0" encoding="UTF-8"?> 
<ep:Document xmlns:ep="namespace here" 
xmlns:gk="namespace here" 
xmlns:sh="namespace here" schemaVersion="" creationDate=""> 

和其餘payload.so我需要在現有的代碼中的架構版本和creationdate後面多一個命名空間,如後面提到的。

+0

我們需要查看您現有的XSLT - 至少是處理'ep:Document'元素的部分。請注意,這是(另一個)多餘的要求:如果您的輸出需要綁定xsi前綴,那麼您的處理器會自動包含它(或發生錯誤)。 –

+0

讓我詳細說明我的要求。在實際的輸入有效載荷中,我得到沒有任何命名空間的消息。 –

+0

我編輯了我的問題。請找到詳細信息。 –

回答

0

如果您所需的命名空間聲明添加到您的xsl:stylesheet元素,即:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:ep="namespace here" 
xmlns:sh="namespace here" 
xmlns:gk="namespace here" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 

可能顯示輸出的根ep:Document元素的開始標記中,像你期望的那樣。我說「可能」,因爲XSLT處理器不必包含冗餘名稱空間聲明。

您無法控制屬性和名稱空間聲明在開始標記中出現的順序。該命令從定義上來說不重要。

+0

所以模式版本和創建日期將在xmlns:xsi命名空間之後出現?不是在那之前? –

+0

這完全取決於您的XSLT處理器。大多數處理器將首先列出所有命名空間,然後列出屬性。請記住,XSLT處理器適用於XML *樹*,而不是實際的XML *文件*。只有在進程結束時,結果樹纔會被序列化以創建輸出文件。 –