与我在这里给您的解决方案相比,这是一种不同风格的解决方案。
我认为一步一步走是值得的。我假设@action
s 以逻辑顺序出现 -create
首先、change
下一个和remove
最后一个。可以多次出现相同的情况,@action
但不会是随机的。现在我们准备看看主要逻辑:
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
我们声明身份转换,然后在几个地方截取它。@id
我们只在具有相同、 parent@id
和的节点的唯一出现处停止@action
:
<xsl:template match="node/*/*[a:is-primary(.)]" priority="1">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="attribute" mode="consolidate-most-recent"/>
</xsl:copy>
</xsl:template>
我们忽略“重复”:
<xsl:template match="node/*/*[not(a:is-primary(.))]"/>
并且还忽略create
s后跟 achange
以及所有create
s 和change
后跟 a remove
。
<xsl:template match="node/*/*[@action = 'change'][a:preceded-by(., 'create')]" priority="2"/>
<xsl:template match="node/*/*[@action = 'create' or action='change'][a:followed-by(., 'remove')]" priority="2"/>
当唯一@action
不跟随另一个@action
会让我们忽略它的唯一被捕获时,我们做一件简单的事情 - 收集具有相同@id
忽略的元素的所有属性@action
并使用它们最近的“最近”值(在文档顺序中最后出现的那些) )。
<xsl:template match="attribute" mode="consolidate-most-recent">
<xsl:copy>
<xsl:for-each-group
select="/root/node/*/*[a:matches(current()/parent::*, ., 'any')]/attribute/*"
group-by="local-name()">
<!-- take the last in the document order -->
<xsl:apply-templates select="current-group()[last()]"/>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
就是这样。现在让我们看看使它工作的函数:
我们有一个key
来简化查找
<xsl:key name="entity" match="/root/node/*/*" use="concat(parent::*/@id, '_', @id, '_', @action)"/>
一个检查它是否是唯一出现的节点的函数(我们可以将它直接添加到模板match
谓词中,但由于我们从函数开始,所以让我们保持不变):
<xsl:function name="a:is-primary" as="xs:boolean">
<xsl:param name="ctx"/>
<!-- need to establish "focus"(context) for the key() function to work -->
<xsl:for-each select="$ctx">
<xsl:sequence select="generate-id($ctx) = generate-id(key('entity', concat($ctx/parent::*/@id, '_', $ctx/@id, '_', $ctx/@action))[1])"/>
</xsl:for-each>
</xsl:function>
一个matches
可以为我们做各种比较的函数(同样,可以把它全部放在谓词中,但是这样我们可以在真实的模板中保持它的整洁):
<xsl:function name="a:matches" as="xs:boolean">
<xsl:param name="src"/>
<xsl:param name="target"/>
<!-- can be one of the following:
'any' - only match the @id(s) and ignore @action
'same' - match by @id(s) and expect $src/@action to match $target/@action
a certain value - match by @id(s) and expect @action to match this value
-->
<xsl:param name="action"/>
<xsl:value-of select="
($src/local-name() = $target/local-name()) and
($src/parent::*/@id = $target/parent::*/@id) and
($src/@id = $target/@id) and
(if ($action = 'any')
then true()
else if ($action = 'same')
then ($target/@action = $src/@action)
else ($target/@action = $action))"/>
</xsl:function>
以及“原始”函数之上的preceded-by
and语法糖:followed-by
matches
<xsl:function name="a:preceded-by" as="xs:boolean">
<xsl:param name="ctx"/>
<xsl:param name="action"/>
<xsl:value-of select="count($ctx/preceding::*[a:matches($ctx, ., $action)]) > 0"/>
</xsl:function>
<xsl:function name="a:followed-by" as="xs:boolean">
<xsl:param name="ctx"/>
<xsl:param name="action"/>
<xsl:value-of select="count($ctx/following::*[a:matches($ctx, ., $action)]) > 0"/>
</xsl:function>
概括
这是一个完整的转换:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:a="http://a.com">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="entity" match="/root/node/*/*" use="concat(parent::*/@id, '_', @id, '_', @action)"/>
<xsl:function name="a:is-primary" as="xs:boolean">
<xsl:param name="ctx"/>
<!-- need to establish "focus"(context) for the key() function to work -->
<xsl:for-each select="$ctx">
<xsl:sequence select="generate-id($ctx) = generate-id(key('entity', concat($ctx/parent::*/@id, '_', $ctx/@id, '_', $ctx/@action))[1])"/>
</xsl:for-each>
</xsl:function>
<xsl:function name="a:preceded-by" as="xs:boolean">
<xsl:param name="ctx"/>
<xsl:param name="action"/>
<xsl:value-of select="count($ctx/preceding::*[a:matches($ctx, ., $action)]) > 0"/>
</xsl:function>
<xsl:function name="a:followed-by" as="xs:boolean">
<xsl:param name="ctx"/>
<xsl:param name="action"/>
<xsl:value-of select="count($ctx/following::*[a:matches($ctx, ., $action)]) > 0"/>
</xsl:function>
<xsl:function name="a:matches" as="xs:boolean">
<xsl:param name="src"/>
<xsl:param name="target"/>
<!-- can be one of the following:
'any' - only match the @id(s) and ignore @action
'same' - match by @id(s) and expect $src/@action to match $target/@action
a certain value - match by @id(s) and expect @action to match this value
-->
<xsl:param name="action"/>
<xsl:value-of select="
($src/local-name() = $target/local-name()) and
($src/parent::*/@id = $target/parent::*/@id) and
($src/@id = $target/@id) and
(if ($action = 'any')
then true()
else if ($action = 'same')
then ($target/@action = $src/@action)
else ($target/@action = $action))"/>
</xsl:function>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node/*/*[a:is-primary(.)]" priority="1">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="attribute" mode="consolidate-most-recent"/>
</xsl:copy>
</xsl:template>
<xsl:template match="attribute" mode="consolidate-most-recent">
<xsl:copy>
<xsl:for-each-group
select="/root/node/*/*[a:matches(current()/parent::*, ., 'any')]/attribute/*"
group-by="local-name()">
<!-- take the last in the document order -->
<xsl:apply-templates select="current-group()[last()]"/>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<xsl:template match="node/*/*[not(a:is-primary(.))]"/>
<!-- assume a remove is never followed by a change or create -->
<xsl:template match="node/*/*[@action = 'change'][a:preceded-by(., 'create')]" priority="2"/>
<xsl:template match="node/*/*[@action = 'create' or action='change'][a:followed-by(., 'remove')]" priority="2"/>
</xsl:stylesheet>
应用于文档时:
<root>
<node id="N1">
<fruit id="1" action="aaa">
<orange id="x" action="create">
<attribute>
<color>Orange</color>
<year>2012</year>
</attribute>
</orange>
<orange id="x" action="change">
<attribute>
<color>Red</color>
<something>!!</something>
</attribute>
</orange>
<orange id="x" action="change">
<attribute>
<color>Blue</color>
<condition>good</condition>
</attribute>
</orange>
<orange id="x" action="remove">
<attribute>
<condition>awesome</condition>
</attribute>
</orange>
</fruit>
</node>
</root>
产生以下结果:
<root>
<node id="N1">
<fruit id="1" action="aaa">
<orange id="x" action="remove">
<attribute>
<color>Blue</color>
<year>2012</year>
<something>!!</something>
<condition>awesome</condition>
</attribute>
</orange>
</fruit>
</node>
</root>
我希望这很清楚。您可以扩展此概念并为自己构建一个很好的可重用函数库,然后将其用作简单的谓词,以一种或另一种方式合并您的节点。不太可能是完成工作的最有效方式,但至少是表达解决方案的一种简洁方式。