我想根據基於子元素的派生值對元素進行排序。無法使用XPath(sum
,concat
等)計算派生值,但可以使用XSL(xsl:choose
,xsl:if
等)進行計算。XSLT使用派生/計算值進行排序而不使用EXSLT函數
我會使用EXSLT功能擴展,但它不可用。該環境是XSLT 1.0,Xalan-C++版本1.10,帶有EXSLT公共和集合擴展。
編輯更改了示例以強調我需要組合的派生值不能用簡單的node/xpath函數在xsl:sort
語句中計算。
我的目標是列出當前藥物在活動之前,按降序開始日期排序。確定藥物是否是當前藥物的邏輯取決於它是否被取消,尚未過期以及其他一些業務邏輯。
鑑於這種XML:
<?xml version="1.0"?>
<medications>
<medication>
<name>med1</name>
<status>canceled</status>
<startTime>2012-02-01T00:00:00Z</startTime>
<endTime>2012-12-31T00:00:00Z</endTime>
<!-- other elements omitted -->
</medication>
<medication>
<name>med2</name>
<status />
<startTime>2012-01-01T00:00:00Z</startTime>
<endTime>2012-01-07T00:00:00Z</endTime>
<!-- other elements omitted -->
</medication>
<medication>
<name>med3</name>
<status />
<startTime>2012-01-01T00:00:00Z</startTime>
<!-- other element omitted -->
</medication>
</medications>
樣式表會產生藥物,包括從例如數據刪去信息的排序列表從父節點(訂貨醫生,藥房位置等)和數據(患者地址,初級保健醫生等)。在這個例子中,我只製作一個簡單的排序列表,顯示了藥物節點可以運行:
<?xml version="1.0" encoding="utf-8"?>
<medications>
<medication isCurrent="1" name="med3" />
<medication isCurrent="0" name="med1" />
<medication isCurrent="0" name="med2" />
</medications>
我能想出最好是預先計算得到的值轉換成EXSLT節點集(連同所需的排序其他值),並使用一鍵生成由-ID查找藥品單體:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common"
exclude-result-prefixes="exsl">
<xsl:output encoding="utf-8" method="xml" indent="yes" />
<xsl:key name="medications-by-id" match="medication" use="generate-id()" />
<xsl:variable name="medication-sorter">
<xsl:for-each select="//medication">
<item id="{generate-id(.)}">
<xsl:attribute name="isCurrent">
<xsl:apply-templates mode="isCurrentMedication" select="." />
</xsl:attribute>
<xsl:attribute name="startTime">
<xsl:value-of select="startTime/text()" />
</xsl:attribute>
</item>
</xsl:for-each>
</xsl:variable>
<xsl:template match="medications">
<!-- hardcoded key lookup works -->
<hardcoded><xsl:value-of select="key('medications-by-id',generate-id(medication[2]))/name/text()"/></hardcoded>
<!-- but key lookup from the sort helper does not -->
<medications>
<xsl:for-each select="exsl:node-set($medication-sorter)/item">
<xsl:sort select="@isCurrent" order="descending" />
<xsl:sort select="@startTime" order="descending" />
<medication>
<xsl:attribute name="isCurrent">
<xsl:value-of select="@isCurrent" />
</xsl:attribute>
<xsl:attribute name="name">
<xsl:value-of select="key('medications-by-id',@id)/name/text()" />
</xsl:attribute>
</medication>
</xsl:for-each>
</medications>
</xsl:template>
<xsl:template mode="isCurrentMedication" match="medication">
<xsl:choose>
<xsl:when test="(status/text()='canceled') or (status/text()='discontinued') or (status/text()='inactive')">0</xsl:when>
<xsl:otherwise>
<!-- omitted date checking logic not relevent to this question, so just hardcoded -->
<xsl:choose>
<xsl:when test="name/text()='med2'">0</xsl:when>
<xsl:when test="name/text()='med3'">1</xsl:when>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
然而,這並沒有達到預期效果。使用generate-id(medication [2])查找關鍵字時,節點有效並輸出名稱,但在使用節點集中的@id調用時無效,即使值看起來完全相同:
<?xml version="1.0" encoding="utf-8"?>
<medications>
<hardcoded>med2</hardcoded>
<medication isCurrent="1" name="" />
<medication isCurrent="0" name="" />
<medication isCurrent="0" name="" />
</medications>
還用Xalan for Java 2.7.1測試過,結果相同。
我可以通過在$ medication-sorter節點集中包含一個copy-of
藥物元素來解決這個問題,但是父上下文會丟失,並且有時候我的樣式表需要它。
是否有另一種方法來對需要使用xsl:template
計算的值進行排序/分組?