XSL 转换 - 带空格的文件路径
XSL Transform - Filepath with spaces
我正在尝试执行 XSL 转换以使用 Java 和 Saxon 将 CSV 文件转换为 XML。
<xsl:when test="unparsed-text-available($pathToCSV)">
我这样调用命令:
C:\Program Files (x86)\Java\jdk1.8.0_191\bin>java.exe -jar c:\saxon\saxon9he.jar -o:"E:\Temp\Folder Has Spaces\test.xml" -it:main -xsl:E:\Temp\csvxml_TEST.xsl pathToCSV="E:\Temp\Folder Has Spaces\test.csv"
如果他们输入的路径不包含空格,脚本可以正常工作。
当输入路径中有空格时,有没有办法让这个脚本工作?
完整的 XSL 代码:
<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="fn"
exclude-result-prefixes="xs fn">
<xsl:output indent="yes" encoding="UTF-8"/>
<xsl:param name="pathToCSV" />
<xsl:function name="fn:getTokens" as="xs:string+">
<xsl:param name="str" as="xs:string"/>
<xsl:analyze-string select="concat($str, ',')" regex='(("[^"]*")+|[^,]*),'>
<xsl:matching-substring>
<xsl:sequence select='replace(regex-group(1), "^""|""$|("")""", "")'/>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:function>
<xsl:template match="/" name="main">
<xsl:choose>
<xsl:when test="unparsed-text-available($pathToCSV)">
<xsl:variable name="csv" select="unparsed-text(pathToCSV)"/>
<xsl:variable name="lines" select="tokenize($csv, '
')" as="xs:string+"/>
<xsl:variable name="elemNames" select="fn:getTokens($lines[1])" as="xs:string+"/>
<root>
<xsl:for-each select="$lines[position() > 1]">
<row>
<xsl:variable name="lineItems" select="fn:getTokens(.)" as="xs:string+"/>
<xsl:for-each select="$elemNames">
<xsl:variable name="pos" select="position()"/>
<elem name="{.}">
<xsl:value-of select="normalize-space($lineItems[$pos])"/>
</elem>
</xsl:for-each>
</row>
</xsl:for-each>
</root>
</xsl:when>
<xsl:otherwise>
<xsl:text>Cannot locate : </xsl:text><xsl:value-of select="$pathToCSV"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
大多数 XPath 和 XSLT 函数使用 URI 而不是 system/os 依赖文件路径,因此您可以尝试将 Windows 文件路径转换为 URI,例如
unparsed-text(concat('file:///', replace(replace($pathToCSV, '\', '/'), ' ', '%20')))
或
unparsed-text(iri-to-uri(concat('file:///', replace($pathToCSV, '\', '/'))))
请注意,Saxon PE 和 EE 支持的 EXPath 文件模块 (http://saxonica.com/html/documentation9.9/functions/expath-file/path-to-uri.html) has method file:path-to-uri
: http://expath.org/spec/file#fn.path-to-uri。
我正在尝试执行 XSL 转换以使用 Java 和 Saxon 将 CSV 文件转换为 XML。
<xsl:when test="unparsed-text-available($pathToCSV)">
我这样调用命令: C:\Program Files (x86)\Java\jdk1.8.0_191\bin>java.exe -jar c:\saxon\saxon9he.jar -o:"E:\Temp\Folder Has Spaces\test.xml" -it:main -xsl:E:\Temp\csvxml_TEST.xsl pathToCSV="E:\Temp\Folder Has Spaces\test.csv"
如果他们输入的路径不包含空格,脚本可以正常工作。 当输入路径中有空格时,有没有办法让这个脚本工作?
完整的 XSL 代码:
<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="fn"
exclude-result-prefixes="xs fn">
<xsl:output indent="yes" encoding="UTF-8"/>
<xsl:param name="pathToCSV" />
<xsl:function name="fn:getTokens" as="xs:string+">
<xsl:param name="str" as="xs:string"/>
<xsl:analyze-string select="concat($str, ',')" regex='(("[^"]*")+|[^,]*),'>
<xsl:matching-substring>
<xsl:sequence select='replace(regex-group(1), "^""|""$|("")""", "")'/>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:function>
<xsl:template match="/" name="main">
<xsl:choose>
<xsl:when test="unparsed-text-available($pathToCSV)">
<xsl:variable name="csv" select="unparsed-text(pathToCSV)"/>
<xsl:variable name="lines" select="tokenize($csv, '
')" as="xs:string+"/>
<xsl:variable name="elemNames" select="fn:getTokens($lines[1])" as="xs:string+"/>
<root>
<xsl:for-each select="$lines[position() > 1]">
<row>
<xsl:variable name="lineItems" select="fn:getTokens(.)" as="xs:string+"/>
<xsl:for-each select="$elemNames">
<xsl:variable name="pos" select="position()"/>
<elem name="{.}">
<xsl:value-of select="normalize-space($lineItems[$pos])"/>
</elem>
</xsl:for-each>
</row>
</xsl:for-each>
</root>
</xsl:when>
<xsl:otherwise>
<xsl:text>Cannot locate : </xsl:text><xsl:value-of select="$pathToCSV"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
大多数 XPath 和 XSLT 函数使用 URI 而不是 system/os 依赖文件路径,因此您可以尝试将 Windows 文件路径转换为 URI,例如
unparsed-text(concat('file:///', replace(replace($pathToCSV, '\', '/'), ' ', '%20')))
或
unparsed-text(iri-to-uri(concat('file:///', replace($pathToCSV, '\', '/'))))
请注意,Saxon PE 和 EE 支持的 EXPath 文件模块 (http://saxonica.com/html/documentation9.9/functions/expath-file/path-to-uri.html) has method file:path-to-uri
: http://expath.org/spec/file#fn.path-to-uri。