接受或拒绝 XML 中的更改跟踪 [英] Accept or reject change tracking in XML

查看:23
本文介绍了接受或拒绝 XML 中的更改跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个 XML 文件,其中包含更改跟踪属性 .

I have multiple XML files which consists of change tracking attribute <atict:add> or <atict:del>.

目标:

  • 如果 XML 文件包含一个元素 CT="ACCEPT" 然后接受/打印带有 的所有标签并忽略
  • 如果 XML 文件包含元素 CT="REJECT" 然后接受/打印带有 的所有标签并忽略
  • if XML file consists of an element CT="ACCEPT" then accept/print all tags with <atict:add> and ignore <atict:del>
  • if XML file consits of an element CT="REJECT" then accept/print all tags with <atict:del> and ignore <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 以满足条件?

How can I add the CT in the XSLT with an if condition to satisfy the criteria?

推荐答案

下面的示例样式表可以完成这项工作.请参阅注释以获取解释.

The sample stylesheet below does the job. See the comments for explanations.

<?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>

这篇关于接受或拒绝 XML 中的更改跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆