接受或拒绝XML中的更改跟踪

时间:2016-08-03 06:32:58

标签: xml xslt xsd xml-parsing xslt-2.0

我有多个XML文件,其中包含更改跟踪属性<atict:add><atict:del>

目标:

  • 如果XML文件包含元素CT="ACCEPT",则使用<atict:add>接受/打印所有代码并忽略<atict:del>
  • 如果XML文件包含元素CT="REJECT",则使用<atict:del>接受/打印所有代码并忽略<atict:accept>

示例XML:

<?xml version="1.0" encoding="UTF-8"?>
<DM xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:atict="http://www.arbortext.com/namespace/atict" **CT="ACCEPT"**>
<PARA>abcd <atict:del>efghi</atict:del><atict:add>1456790
</atict:add></PARA>

<?xml version="1.0" encoding="UTF-8"?>
<DM xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:atict="http://www.arbortext.com/namespace/atict" **CT="ACCEPT"**>
<PARA>abcd <atict:del>efghi</atict:del><atict:add>1456790
</atict:add></PARA>

处理后输出XML:

<?xml version="1.0" encoding="UTF-8"?>
<DM xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:atict="http://www.arbortext.com/namespace/atict" **CT="ACCEPT"**>
<PARA>abcd <atict:add>1456790
</atict:add></PARA>

<?xml version="1.0" encoding="UTF-8"?>
<DM xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:atict="http://www.arbortext.com/namespace/atict" **CT="ACCEPT"**>
<PARA>abcd <atict:add>1456790
</atict:add></PARA>

如何在XSLT中添加具有if条件的CT以满足条件?

2 个答案:

答案 0 :(得分:1)

或者简单地说:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:atict="http://www.arbortext.com/namespace/atict">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="atict:del[ancestor::DM/@CT='ACCEPT']"/>
<xsl:template match="atict:add[ancestor::DM/@CT='REJECT']"/>

</xsl:stylesheet>

答案 1 :(得分:0)

下面的样本样式表完成了这项工作。请参阅注释以获得解释。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:atict="http://www.arbortext.com/namespace/atict"
    version="2.0">

    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>

    <xsl:variable name="CT_stat">
        <xsl:choose>
            <xsl:when test="DM/@CT = 'ACCEPT'">1</xsl:when>
            <xsl:when test="DM/@CT = 'REJECT'">0</xsl:when>
            <xsl:otherwise>2</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

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

    <!-- template matching for atict:del and atict:add, retaining
             them or deleting them based on $CT_stat variable -->    
    <xsl:template match="atict:del">
        <xsl:choose>
            <xsl:when test="$CT_stat=1"/>
            <xsl:when test="$CT_stat=0">
                <xsl:copy-of select="."/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy-of select="."/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="atict:add">
        <xsl:choose>
            <xsl:when test="$CT_stat=1">
                <xsl:copy-of select="."/>
            </xsl:when>
            <xsl:when test="$CT_stat=0"/>
            <xsl:otherwise>
                <xsl:copy-of select="."/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>