将多个 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>
还有比这更可读的解决方案吗?
最佳答案
在带有 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>
关于xml - XSLT 结合多种功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48345668/