2015-02-23 101 views
0

我試圖在同一個XML文檔中的兩個子元素之間進行排序的交集。比較一系列元素並通過XSLT/Xpath替換匹配

<links> 
<old> 
    <xref linkend="zzzzz">/chapter/subchapter[1]/section[2]/@id</xref> 
    <xref linkend="aaaaa">/chapter/section[1]/@id</xref> 
</old> 
<new> 
    <xref linkend="xxxxx">/chapter/subchapter[1]/section[2]/@id</xref> 
    <xref linkend="sssss">/chapter/@id</xref> 
</new> 
</links> 

每當有新的/外部參照老/外部參照的路徑字符串之間的匹配,我想加入的兩個元素,創造一個像這樣的輸出:

<matches> 
    <match old-linkend='zzzzz' new-linkend='xxxxx'>/chapter/subchapter[1]/section[2]/@id</match> 
</matches> 

這是我第一次嘗試比較序列,所以我有點迷路。我在撒克遜使用XSLT 2.0。

+0

您可以在old/xref上做一個選擇匹配,然後使用xsl:if如/ new/xref [position()]。你可能必須去父母,這取決於你如何做你的匹配,即../新/外部參照[位置()]。 – 2015-02-23 17:28:35

回答

2

從一個特定old/xref上下文開始,你可以檢查是否有匹配新的與

<xsl:variable name="matching" select="../../new/xref[. = current()]"/> 

在方括號,.是你正在測試的new/xrefcurrent()old/xref你開始。檢查這個序列是否爲空可以告訴你是否匹配。

<xsl:if test="$matching"> 
    <match old-linkend="{@linkend}" new-linkend="{$matching/@linkend}"> 
    <xsl:value-of select="." /> 
    </match> 
</xsl:if> 

如果多於一個new/xref匹配此old/xrefnew-linkend屬性值模板是受rules for constructing simple content,將從所有匹配new/xref元件一起串聯的linkend值,用空格分開。

+0

如果有多個比賽怎麼辦?我有點期待某種模板比賽。 – 2015-02-23 18:07:13

+1

@PhilipStuyck,因爲這是XSLT 2.0,你會得到所有匹配的'linkend'值空間分隔。如果它是XSLT 1.0,那麼你只能得到其中的一個(按文檔順序排列第一)。 – 2015-02-23 18:27:07