0
逃生輸入我有以下XSLT,其通過分組給定陣列上的鍵格式化輸出:在XSLT
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="xs mf">
<xsl:param name="values" select="'Sniper 50 Red', 'Xiper 10 Red'"/>
<xsl:output indent="yes"/>
<xsl:function name="mf:match" as="xs:string">
<xsl:param name="input" as="xs:string"/>
<xsl:param name="values" as="xs:string*"/>
<xsl:sequence
select="if ($values[matches($input, concat('^', .))])
then $values[matches($input, concat('^', .))]
else $input"/>
</xsl:function>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="results">
<xsl:copy>
<xsl:for-each-group select="pageFunctionResult" group-by="mf:match(product_name, $values)">
<xsl:choose>
<xsl:when test="product_name ne current-grouping-key()">
<xsl:copy>
<position>MAINPRODUCT</position>
<variantname>
<xsl:value-of select="product_name"/>
</variantname>
<product_namea>
<xsl:value-of select="current-grouping-key()"/>
</product_namea>
<xsl:copy-of select="category, product_id, quantity, manufacturer,image"/>
<xsl:apply-templates select="current-group() except ."/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="current-group()">
<xsl:copy>
<position>MAINPRODUCT</position>
<variantname>
<xsl:value-of select="current-grouping-key()"/>
</variantname>
<xsl:apply-templates select="*"/>
</xsl:copy>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<xsl:template match="pageFunctionResult">
<variant id="{position()}">
<xsl:apply-templates/>
</variant>
</xsl:template>
</xsl:transform>
在這裏可以看到it in action。它的工作原理很好,但我現在有括號鍵在其名稱:
<xsl:param name="values" select="'Sniper 50 Red (26)', 'Xiper 10 Red (27)'"/>
...這顯然剎車的代碼,因爲括號內的正則表達式被保留。所以括號應該逃脫。我得到了一個提示,使用functx:escape-for-regex,但我無法實現它的代碼。
的功能從http://www.xsltfunctions.com/xsl/functx_escape-for-regex.html拍攝,我沒有嘗試改變它的主要建議是使用圖書館功能,然後展示如何做,因爲海報說他不知道如何做。我同意你的版本看起來更加緊湊和高效。 –
我明白了。好的,這是一個小問題,我將刪除評論。 – Tomalak