命名空间问题

时间:2019-04-25 10:13:24

标签: xml xslt

我需要在要发送给第三方的邮件标题中输入xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
还是有一种方法可以删除xsi:release属性,但又保留xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance

这是我的xslt:

<xsl:choose>
    <xsl:when test="$msgtype='Nomination_Document'">
        <xsl:element name="Nomination_Document" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" namespace="{$msgns}">
            <xsl:attribute name="xsi:release" xmlns="http://www.w3.org/2001/XMLSchema-instance">
            </xsl:attribute> 
            <xsl:attribute name="release" xmlns="http://www.w3.org/2001/XMLSchema-instance">
                <xsl:value-of select="'3'"/>
            </xsl:attribute> 

这是我的输入内容

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TSMessage xmlns="http://ECG.NOMSInhouse/V1.1.0.0">
    <Header>
        <MsgNumber Revision="1" MsgNo="840711"/>
        <GenerationDate Date="2019-04-16T11:55:22+02:00"/>
        <MsgSender Role="ZSH" Label="" CodeAgency="" ID=""/>
        <MsgReceiver Role="ZSO" Label="" CodeAgency="" ID=""/>
        <MsgType TypeCode="TN"/>
        <Subject Text=""/>
        <MsgPeriod TimeTo="2019-04-18T06:00:00+02:00" TimeFrom="2019-04-17T06:00:00+02:00"/>
        <Contract ContractID=""/>

这就是我现在得到的:

<?xml version="1.0" encoding="UTF-8"?><Nomination_Document 
 xmlns="urn:easeegas.eu:edigas:nominationandmatching:nominationdocument:5:1" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:release="" 
 release="3">

我需要消息的标题如下:

<?xml version="1.0" encoding="UTF-8"?><Nomination_Document 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="urn:easeegas.eu:edigas:nominationandmatching:nominationdocument:5:1 " release="3">

2 个答案:

答案 0 :(得分:1)

动态计算元素/属性名称时,请勿使用<xsl:element><xsl:attribute> 。如果它们是固定的(例如在这种情况下),则只需直接写下元素/属性。

此样式表:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ecg="http://ECG.NOMSInhouse/V1.1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="urn:easeegas.eu:edigas:nominationandmatching:nominationdocument:5:1"
    exclude-result-prefixes="ecg xsi"
>
    <xsl:output indent="yes" encoding="utf-8" />

    <xsl:template match="ecg:TSMessage[ecg:Header/ecg:MsgType/@TypeCode = 'TN']">
        <Nomination_Document release="3">
            <xsl:comment> ... more processing ... </xsl:comment>
        </Nomination_Document>
    </xsl:template>
</xsl:stylesheet>

应用于此XML:

<TSMessage xmlns="http://ECG.NOMSInhouse/V1.1.0.0">
    <Header>
        <MsgNumber Revision="1" MsgNo="840711"/>
        <GenerationDate Date="2019-04-16T11:55:22+02:00"/>
        <MsgSender Role="ZSH" Label="" CodeAgency="" ID=""/>
        <MsgReceiver Role="ZSO" Label="" CodeAgency="" ID=""/>
        <MsgType TypeCode="TN"/>
        <Subject Text=""/>
        <MsgPeriod TimeTo="2019-04-18T06:00:00+02:00" TimeFrom="2019-04-17T06:00:00+02:00"/>
        <Contract ContractID=""/>
    </Header>
</TSMessage>

产生以下结果:

<?xml version="1.0" encoding="utf-8"?>
<Nomination_Document release="3" xmlns="urn:easeegas.eu:edigas:nominationandmatching:nominationdocument:5:1">
    <!-- ... more processing ... -->
</Nomination_Document>

注意:

  • 我建议您优先使用多个<xsl:template match="something specific">,而不是单个<xsl:coose>,因为您的代码嵌套的深度较小。
  • 声明您打算在<xsl:stylesheet>级别使用的所有名称空间。
  • 您可以自由选择名称空间前缀-我为ecg名称空间选择了http://ECG.NOMSInhouse/V1.1.0.0,可以选择更喜欢的名称。
  • 使用exclude-result-prefixes防止不必要的名称空间声明显示在输出中。

答案 1 :(得分:0)

您可以删除xsi:release并添加以下行<xsl:namespace name="xsi" select="'http://www.w3.org/2001/XMLSchema-instance'"/>

以下示例:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">

<xsl:output method="xml" indent="yes"/>

<xsl:variable name="msgtype" select="'Nomination_Document'" />
<xsl:variable name="msgns" select="'urn:easeegas.eu:edigas:nominationandmatching:nominationdocument:5:1'" />

<xsl:template match="/">
    <xsl:choose>
        <xsl:when test="$msgtype='Nomination_Document'">
            <xsl:element name="Nomination_Document" namespace="{$msgns}">
                <xsl:namespace name="xsi" select="'http://www.w3.org/2001/XMLSchema-instance'"/>
                <xsl:attribute name="release">
                    <xsl:value-of select="'3'" />
                </xsl:attribute>
            </xsl:element>
        </xsl:when>
    </xsl:choose>
</xsl:template>

https://xsltfiddle.liberty-development.net/jyRYYiB