词汇比较的问题

Problems with lexical comparision

我想使用 XSL 和 groovy 转换一些 XML。我为此使用 javax.xml.transform.TransformerFactory。

但是我的 XLS 文件中的比较并不像我想象的那样有效。

它不能告诉我 2.0.1 大于 2.0。为什么?我认为应该是因为 xsl:stylesheet version="2.0"。我做错了什么?

这是我的文件:

XML

<?xml version="1.0" encoding="UTF-8"?>
<apis>
    <api version="2.0.1">
        <resource>
            <description>doc for API 2.0.1</description>
        </resource>
    </api>
</apis>

XSL

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

<xsl:param name="api-version" select="2.0"/>
<xsl:template match="/apis/api[@version &gt; $api-version]/resource">
    <resource>
        <xsl:value-of select="description"></xsl:value-of>
    </resource>
</xsl:template>

</xsl:stylesheet>

和groovy脚本

import javax.xml.transform.TransformerFactory
import javax.xml.transform.stream.StreamResult
import javax.xml.transform.stream.StreamSource

def workspacePath
def xslPath
def xslFileName
def xmlPath
def xmlFileName
def outputPath
def outputFileName
def xslt
def transformer
def xml
def output
def apiVersions

workspacePath = "C:/test/"
xslPath = "transformations/"
xslFileName = "test5.xsl"
xmlPath = "pendingFeature/"
xmlFileName = "test5.xml"
outputPath = "outputs/"

xslt = new File(workspacePath + xslPath + xslFileName).getText()
transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(new StringReader(xslt)))
xml = new File(workspacePath + xmlPath + xmlFileName).getText()
outputFileName = "doc.html"
output = new FileOutputStream(workspacePath + outputPath + outputFileName)
transformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(output))
output.close()

您需要将像 Saxon 9 这样的 XSLT 2.0 处理器放在 class 路径上以获得 XSLT 2.0 支持。如果你想比较字符串而不是数字,那么做

<xsl:param name="api-version" select="'2.0'"/>
<xsl:template match="/apis/api[@version &gt; $api-version]/resource">