2017-02-15 108 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 *文件*。只有在进程结束时,结果树才会被序列化以创建输出文件。 –