2016-09-16 54 views
0

基本上我在這裏嘗試的是使用XSLT通過引用XML中元素的值對多個XML進行合併和排序。根據另一個XML中的元素值對多個XML進行排序

> <xsl:variable name="refXml" 
> select="document(concat(replace($refXmlTemp,'^file:',''),'/ref.xml'))"/> 
> 
> 

> <xsl:for-each select="for $x in 
> collection(string-join(($inputDir,'select=*.xml;recurse=yes;on-error=fail'),'?')) 
> return 
>       (if (matches($refXml/root/descendant-or-self::issue/id[normalize-space(.)=normalize-space($x/art/item/id)]/number,'\w+') 
> and matches($x/art/item/title,'\w+')) then saxon:discard-document($x) 
>       else())"> 

> <xsl:sort select="$refXml/root/descendant-or-self::issue/id[normalize-space(.)=/art/item/id]/following-sibling::number"/> 

上面的代碼片段合併了所有的輸入XML,但它沒有排序。

看來XSLT xsl:sort函數只會在指向當前正在處理的XML內部的值時才起作用。

請指教我如何才能在排序中使用ref.xml作爲參考。

這裏ref.xml的一個樣本輸入端:

<root> 
<issue> 
    <id>wlu-101</id> 
    <number>1</number> 
</issue> 
<issue> 
    <id>wlu-143</id> 
    <number>2</number> 
</issue> 
<issue-group> 
    <issue> 
     <id>wlu-144</id> 
     <number>3</number> 
    </issue> 
    <issue-group> 
     <issue> 
     <id>wlu-185</id> 
     <number>4</number> 
     </issue> 
    </issue-group> 
</issue-group> 
</root> 

回答

1

限定

<xsl:key name="ref" match="issue" use="normalize-space(id)"/> 

作爲替代使用<xsl:sort select="$refXml//issue[normalize-space(id)=current()/art/item/id]/number"/>後,更換<xsl:sort select="$refXml/root/descendant-or-self::issue/id[normalize-space(.)=/art/item/id]/following-sibling::number"/>

<xsl:sort select="key('ref', /art/item/id, $refXml)/number"/> 

+0

謝謝Martin,但是當我嘗試你的建議代碼時,它沒有按照它應該的方式排序;它看起來在xml上沒有任何更改。 – Viin

+0

您是否可以嘗試將問題減少到至少三個或四個您要按排序順序處理的輸入文件,並向我們展示樣本,將其降至最低以展示問題?恐怕無法僅從您的XSLT代碼片斷中發現事情失敗的情況,我們需要查看一些示例數據來確定哪個鍵或路徑是錯誤的。 –

+0

woot!這似乎是我的錯,爲什麼它不適合我的第一次嘗試,對不起。然而,後代或自己不能與一起工作,我們如何使用它來獲得數字的價值?可能嗎? – Viin

相關問題