XSLT结合了多种功能

时间:2018-01-19 16:38:08

标签: xml xslt

将多个XSLT函数组合成一行是个好主意吗?

<xsl:template match="title" >
        <xsl:copy>
            <xsl:value-of select="normalize-space(replace(replace(replace(.,'OK',''),'^.\d+Something',''),'((Special10)\s+)[0-9]+\s+(.*)','$1$3'))"/>
        </xsl:copy>
</xsl:template>

是否有比此更易读的解决方案?

1 个答案:

答案 0 :(得分:3)

在带有XPath 3.1的XSLT 3中,您可以使用箭头操作符=>https://www.w3.org/TR/xpath-31/#id-arrow-operator)来编写例如

normalize-space(replace(replace(replace(.,'OK',''),'^.\d+Something',''),'((Special10)\s+)[0-9]+\s+(.*)','$1$3'))

作为

. => replace('OK', '') => replace('^.\d+Something','') => replace('((Special10)\s+)[0-9]+\s+(.*)','$1$3') => normalize-space()

您可以在http://xsltfiddle.liberty-development.net/pPgCcoq

比较两个版本

哪个

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>

    <xsl:template match="title" >
            <xsl:copy>
                <xsl:value-of select="normalize-space(replace(replace(replace(.,'OK',''),'^.\d+Something',''),'((Special10)\s+)[0-9]+\s+(.*)','$1$3'))"/>
            </xsl:copy>
    </xsl:template>

    <xsl:template match="title2" expand-text="yes">
        <xsl:copy>{. => replace('OK', '') => replace('^.\d+Something','') => replace('((Special10)\s+)[0-9]+\s+(.*)','$1$3') => normalize-space()}</xsl:copy>
    </xsl:template>

</xsl:stylesheet>