平台:撒克逊9 - XSLT 2.0排序XML元素顺序XSLT基于在外部文档指定的顺序
我有需要定期编辑,更新和保存3000个的XML文档。
该过程的一部分包括在编辑之前从存储库中检出文档,并在编辑完成后定期发布它。
每个文档包含一系列单独命名的部分,例如,
<part>
<meta>
<place_id>12345</place_id>
<place_name>London</place_name>
<country_id>GB</country_id>
<country_name>United Kingdom</country_name>
</meta>
<text>
<docs>some blurb</docs>
<airport>some blurb LGW LHR</airport>
<trains>some blurb</trains>
<hotels>some blurb</hotels>
<health>some blurb</health>
<attractions>some blurb</attractions>
</text>
</part>
在文本元素有近100部,并与所有的编辑团队,他们改变主意的优先顺序上偶尔,但经常,基础。也许每年两次。
目前,我们向当前首选编辑中的编辑提供XML文档部分以进行编辑和发布。这个顺序被称为“stdhdg.xml”动态产生的外部文档中指定,并且出现这样的事:
<hdgs>
<hdg name="docs" newsort="10"/>
<hdg name="airport" newsort="30"/>
<hdg name="trains" newsort="20"/>
<hdg name="hotels" newsort="40"/>
<hdg name="health" newsort="60"/>
<hdg name="attractions" newsort="50"/>
</hdgs>
,其中,通过热浸镀锌/ @ newsort规定的优选排序次序。
于是我就用一个模板像这样以正确的顺序
<xsl:template match="text">
<xsl:variable name="thetext" select="."/>
<xsl:variable name="stdhead" select="document('stdhdg.xml')"/>
<text>
<xsl:for-each select="$stdhead//hdg">
<xsl:sort data-type="number" order="ascending" select="@newsort"/>
<xsl:variable name="tagname" select="@name"/>
<xsl:variable name="thisnode" select="$thetext/*[local-name() = $tagname]"/>
<xsl:apply-templates select="$thisnode"/>
</xsl:for-each>
</text>
</xsl:template>
到过程,但它似乎非常缓慢和繁琐的,我觉得我应该使用键加速这一过程。
是否有一个简单/整洁的方式来做这个排序操作。
(请不要问我的方式来改变编辑的编辑,这是比我的生命更有价值)
TIA
Feargal
好问题,+1。请参阅我的答案,以获得完全消除每次编辑之前重新排序的解决方案。 :) – 2010-12-16 15:43:21