在 XML 中接受或拒绝更改跟踪
Accept or reject change tracking in XML
我有多个 XML 文件,其中包含更改跟踪属性 <atict:add>
或 <atict:del>
。
Objective:
- 如果 XML 文件包含一个元素
CT="ACCEPT"
那么 accept/print 所有带有 <atict:add>
的标签并忽略 <atict:del>
- 如果 XML 文件包含一个元素
CT="REJECT"
那么 accept/print 所有带有 <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>
如何使用 if 条件在 XSLT 中添加 CT 以满足条件?
下面的示例样式表可以完成这项工作。解释见评论。
<?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>
或者简单地说:
<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>
我有多个 XML 文件,其中包含更改跟踪属性 <atict:add>
或 <atict:del>
。
Objective:
- 如果 XML 文件包含一个元素
CT="ACCEPT"
那么 accept/print 所有带有<atict:add>
的标签并忽略<atict:del>
- 如果 XML 文件包含一个元素
CT="REJECT"
那么 accept/print 所有带有<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>
如何使用 if 条件在 XSLT 中添加 CT 以满足条件?
下面的示例样式表可以完成这项工作。解释见评论。
<?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>
或者简单地说:
<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>