如何在XSL中添加子节点的属性?

时间:2015-11-21 06:54:31

标签: xml xslt

我有像

这样的xml文件
<?xml version="1.0" encoding="UTF-8"?>
<_POSDW-POSTR_CREATEMULTIPLE04>
    <IDOC BEGIN="1">        
        <_POSDW-E1POSTR_CREATEMULTIP>
            <_POSDW-E1BPTRANSACTION>
                <RETAILSTOREID>C10</RETAILSTOREID>
                <BUSINESSDAYDATE>11182015</BUSINESSDAYDATE>
                <PARTNERQUALIFIER>1</PARTNERQUALIFIER>
                <TRANSACTIONTYPECODE>MS01</TRANSACTIONTYPECODE>
                <WORKSTATIONID>001</WORKSTATIONID>
                <TRANSACTIONSEQUENCENUMBER>001007071</TRANSACTIONSEQUENCENUMBER>
                <BEGINDATETIMESTAMP>11182015</BEGINDATETIMESTAMP>
                <ENDDATETIMESTAMP>11182015</ENDDATETIMESTAMP>
                <OPERATORQUALIFIER>1</OPERATORQUALIFIER>
                <OPERATORID>10006</OPERATORID>
                <TRANSACTIONCURRENCY>VND</TRANSACTIONCURRENCY>
            </_POSDW-E1BPTRANSACTION>           
        </_POSDW-E1POSTR_CREATEMULTIP>
    </IDOC>
</_POSDW-POSTR_CREATEMULTIPLE04>

我尝试为每个节点添加属性:SEGMENT =“1”

使用:

<xsl:copy>
                <xsl:attribute name="SEGMENT">1</xsl:attribute>
                <xsl:copy-of select="@*|node()"/>
            </xsl:copy>

仅结果:

<?xml version="1.0" encoding="UTF-8"?>
<_POSDW-POSTR_CREATEMULTIPLE04>
    <IDOC BEGIN="1">        
        <_POSDW-E1POSTR_CREATEMULTIP SEGMENT="1">
            <_POSDW-E1BPTRANSACTION>
                <RETAILSTOREID>C10</RETAILSTOREID>
                <BUSINESSDAYDATE>11182015</BUSINESSDAYDATE>
                <PARTNERQUALIFIER>1</PARTNERQUALIFIER>
                <TRANSACTIONTYPECODE>MS01</TRANSACTIONTYPECODE>
                <WORKSTATIONID>001</WORKSTATIONID>
                <TRANSACTIONSEQUENCENUMBER>001007071</TRANSACTIONSEQUENCENUMBER>
                <BEGINDATETIMESTAMP>11182015</BEGINDATETIMESTAMP>
                <ENDDATETIMESTAMP>11182015</ENDDATETIMESTAMP>
                <OPERATORQUALIFIER>1</OPERATORQUALIFIER>
                <OPERATORID>10006</OPERATORID>
                <TRANSACTIONCURRENCY>VND</TRANSACTIONCURRENCY>
            </_POSDW-E1BPTRANSACTION>           
        </_POSDW-E1POSTR_CREATEMULTIP>
    </IDOC>
</_POSDW-POSTR_CREATEMULTIPLE04>

节点缺少属性:_POSDW-E1BPTRANSACTION

请帮我添加属性:SEGMENT =“1”到_POSDW-E1BPTRANSACTION

谢谢, 的Cuong

1 个答案:

答案 0 :(得分:0)

您可以使用标识模板以及另外一个模板,该模板假定将SEGMENT="1"属性添加到特定的元素集,即_POSDW-E1POSTR_CREATEMULTIP_POSDW-E1BPTRANSACTION

<xsl:template match="_POSDW-E1POSTR_CREATEMULTIP|_POSDW-E1BPTRANSACTION">
    <xsl:copy>
        <xsl:attribute name="SEGMENT">1</xsl:attribute>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

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

<强> xsltransform demo