首先,名爲「equals」的關係不能有這個名字。
「等於」意味着該關係是等價關係。根據定義任何等價關係~
必須是:
自反:x ~ x
。
對稱:如果x ~ y
然後y ~ x
傳遞:如果x ~ y
和y ~ z
然後x ~ z
。
下面是一個例子,示出了所提出的 「等於」 關係不傳遞的:
x
是:
<a>
<c type="type-one" id="5675"/>
<c type="type-two" id="3423"/>
<c type="type-three" id="9088"/>
</a>
y
是:
<b>
<c type="type-one" id="5675"/>
<c type="type-two" id="3423"/>
<c type="type-four" id="1234"/>
</b>
z
是:
<b>
<c type="type-three" id="3333"/>
<c type="type-four" id="1234"/>
</b>
現在,我們可以看到,x ~ y
和y ~ z
。但是,顯然這不成立:x ~ z
這就是說,我稱之爲關係「匹配」,它是放鬆,而不是「等於」。
下面是該問題的解決方案,在上述調整:
注意,這不能用單一的XPath表達式來表達,因爲XPath的1。0(在XSLT 1.0轉換中使用)沒有範圍變量。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<xsl:call-template name="matches">
<xsl:with-param name="pElem1" select="a"/>
<xsl:with-param name="pElem2" select="b"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="matches">
<xsl:param name="pElem1" select="/.."/>
<xsl:param name="pElem2" select="/.."/>
<xsl:variable name="vMisMatch">
<xsl:for-each select="$pElem1/c[@type = $pElem2/c/@type]">
<xsl:if test=
"$pElem2/c[@type = current()/@type and not(@id = current()/@id)]">1</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:copy-of select="not(string($vMisMatch))"/>
</xsl:template>
</xsl:stylesheet>
當這種轉化是在下面的XML文檔應用:
<t>
<a>
<c type="type-one" id="5675"/>
<c type="type-two" id="3423"/>
<c type="type-three" id="9088"/>
</a>
<b>
<c type="type-one" id="5675"/>
<c type="type-two" id="3423"/>
</b>
</t>
有用,正確的結果產生:
true
當相同的變換是應用於此XML文檔:
<t>
<a>
<c type="type-one" id="5675"/>
<c type="type-two" id="3423"/>
<c type="type-three" id="9088"/>
</a>
<b>
<c type="type-one" id="5675"/>
<c type="type-two" id="9876"/>
</b>
</t>
再次正確的結果產生:
false
第一個例子中'a'和'b'如果'a'有一個子元素' '有一個相應的孩子'B'?請編輯問題並更正。目前的例子與「平等」的概念相矛盾。 –
沒有平等的單一定義...例如,具有相同的內部文本節點在某種意義上是平等的,但是這不是我感興趣的,或者在現實生活中的差異,也許是兩個人有相同的口碑堆棧溢出,但不平等的工資,等等,等等等等... –
user1447002,平等被嚴格定義爲一種等價關係 - 如果是〜平等的,它必須是1自反,對稱2和3傳遞。你沒有這些 - 你沒有定義你稱之爲「平等」的關係的所有屬性。 –