如何使用模板匹配 uri-collection 结果
How to match uri-collection results using templates
我有文件 URI 集合的变量。
<xsl:variable name="swiftFilesPath" select="concat($inputPath, '?select=*.swift;recurse=yes;on-error=warning')"/>
<xsl:variable name="swiftFiles" select="uri-collection($swiftFilesPath)"/>
我想使用应用模板来处理所有 URI。
现在我使用 for-each 来获取文件,然后逐行处理。
<xsl:for-each select="$swiftFiles">
[...]
<xsl:variable name="filePath" select="."/>
<xsl:variable name="fileContent" select="unparsed-text($filePath, $encoding)"/>
<xsl:for-each select="tokenize($fileContent, '\n')">
[...]
</xsl:for-each>
</xsl:for-each>
我正在考虑将它改成这样:
<xsl:apply-templates select="$swiftFiles" mode="swiftFiles"/>
[...]
<xsl:template match="*" mode="swiftFiles">
[...]
</xsl:template/>
- 这是处理文件的更好方法吗?我的意思是
apply-templates
比 for-each
好。
- 有没有办法在模板匹配中避免
"*"
?也许像“*[.castable as xs:anyURI]”?
首先,我认为除非正在进行某种动态发送,否则使用应用模板不会有任何好处。例如,如果您同时拥有 .txt URI 和 .xml URI,那么您可以执行
<xsl:apply-templates select="uri-collection(....)" mode="dereference"/>
<xsl:template match=".[ends-with(., '.txt')]" mode="dereference">
--- process unparsed text file ----
</xsl:template>
<xsl:template match=".[ends-with(., '.xml')]" mode="dereference">
--- process XML file ----
</xsl:template>
<xsl:template match="." mode="dereference"/>
但是,如果它们都以相同的方式处理,那么 xsl:for- 每个都能很好地完成工作。
我已经用“.”回答了你的第二个问题。作为匹配所有内容的模式(包括原子值)。模式“*”将只匹配元素节点。
我有文件 URI 集合的变量。
<xsl:variable name="swiftFilesPath" select="concat($inputPath, '?select=*.swift;recurse=yes;on-error=warning')"/>
<xsl:variable name="swiftFiles" select="uri-collection($swiftFilesPath)"/>
我想使用应用模板来处理所有 URI。
现在我使用 for-each 来获取文件,然后逐行处理。
<xsl:for-each select="$swiftFiles">
[...]
<xsl:variable name="filePath" select="."/>
<xsl:variable name="fileContent" select="unparsed-text($filePath, $encoding)"/>
<xsl:for-each select="tokenize($fileContent, '\n')">
[...]
</xsl:for-each>
</xsl:for-each>
我正在考虑将它改成这样:
<xsl:apply-templates select="$swiftFiles" mode="swiftFiles"/>
[...]
<xsl:template match="*" mode="swiftFiles">
[...]
</xsl:template/>
- 这是处理文件的更好方法吗?我的意思是
apply-templates
比for-each
好。 - 有没有办法在模板匹配中避免
"*"
?也许像“*[.castable as xs:anyURI]”?
首先,我认为除非正在进行某种动态发送,否则使用应用模板不会有任何好处。例如,如果您同时拥有 .txt URI 和 .xml URI,那么您可以执行
<xsl:apply-templates select="uri-collection(....)" mode="dereference"/>
<xsl:template match=".[ends-with(., '.txt')]" mode="dereference">
--- process unparsed text file ----
</xsl:template>
<xsl:template match=".[ends-with(., '.xml')]" mode="dereference">
--- process XML file ----
</xsl:template>
<xsl:template match="." mode="dereference"/>
但是,如果它们都以相同的方式处理,那么 xsl:for- 每个都能很好地完成工作。
我已经用“.”回答了你的第二个问题。作为匹配所有内容的模式(包括原子值)。模式“*”将只匹配元素节点。