将XML标记中的属性值移动到单独的节点

时间:2018-02-02 13:59:08

标签: xml xslt-1.0

我试图将XML元素中的属性值移动到具有不同名称作为属性的新子元素。然而,该值保持不变,元素中的其余文本也是如此。原始XML行(完整XML消息的摘录)是:

<vanaf a:entiteittype="NPS" a:sleutelOntvangend="xxxxxx1608" xmlns:a="http://www.egem.nl/StUF/StUF0301"></vanaf>

我需要创建的XML输出是:

<vanaf a:entiteittype="NPS" xmlns:a="http://www.egem.nl/StUF/StUF0301">
    <inp.a-nummer>xxxxxx1608</inp.a-nummer>
</vanaf>

除了这些行之外,原始XML消息的其余部分可以按原样复制。到目前为止,我构建了以下代码,但没有成功:

<?xml version="1.0" encoding="utf-16"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:package="info:srw/extension/13/package-v1.0" xmlns:StUF="http://www.egem.nl/StUF/StUF0301" xmlns:BG="http://www.egem.nl/StUF/sector/bg/0310">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:template match="BG:vanaf">
    <vanaf a:entiteittype="NPS" xmlns:a="http://www.egem.nl/StUF/StUF0301">
    <inp.a-nummer> <xsl:value-of select = "@a:SleutelOntvangend"/> </inp.a-nummer>
    </vanaf>
</xsl:template>

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

任何帮助我指明正确方向的人都将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

Use this :

<?xml version="1.0" encoding="utf-16"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:package="info:srw/extension/13/package-v1.0" 
    xmlns:StUF="http://www.egem.nl/StUF/StUF0301" 
    xmlns:BG="http://www.egem.nl/StUF/sector/bg/0310"
    exclude-result-prefixes="BG package StUF">

    <xsl:output omit-xml-declaration="yes" indent="yes" />

    <xsl:template match="vanaf">
        <vanaf a:entiteittype="NPS" xmlns:a="http://www.egem.nl/StUF/StUF0301">
            <inp.a-nummer> <xsl:value-of select = "@a:sleutelOntvangend"/> </inp.a-nummer>
        </vanaf>
    </xsl:template>

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

</xsl:stylesheet>

NOTE: 1. template match having unknown namespace 'BG' 2. Need to exclude extra namespaces which not required in output. 3. In value-of attribute name is wrong: @a:sleutelOntvangend