我想使用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 > $api-version]/resource">
<resource>
<xsl:value-of select="description"></xsl:value-of>
</resource>
</xsl:template>
</xsl:stylesheet>
和时髦的剧本
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()
答案 0 :(得分:1)
您需要在类路径上放置一个类似Saxon 9的XSLT 2.0处理器以支持XSLT 2.0。如果你想比较字符串而不是数字,那么
<xsl:param name="api-version" select="'2.0'"/>
<xsl:template match="/apis/api[@version > $api-version]/resource">