我想重复XProc子流水线给定的次数。(在我的用例中,子管道由一个执行步骤组成,它在之前创建的.tex文件上运行LaTeX )
我的代码的简化版本如下所示,到目前为止还没有给出结果:
<p:declare-step version="1.0"
xmlns:p="http://www.w3.org/ns/xproc"
xmlns:c="http://www.w3.org/ns/xproc-step">
<p:option name="latex-exec" select="'uri/latex'"/>
<p:option name="latex-repeat" select="3"/>
<p:option name="tmp-path" select="'uri/tmp/'"/>
<p:option name="tmp-file" select="'tmp'"/>
<!-- pre-processing -->
<p:for-each>
<p:iteration-source select="(1 to $latex-repeat)"/>
<p:exec result-is-xml="false">
<p:with-option name="command" select="$latex-exec"/>
<p:with-option name="args" select="string-join(('-interaction=nonstopmode','-halt-on-error','-output-format=pdf',concat('-output-directory=',$tmp-path),concat($tmp-path,$tmp-file,'.tex')),' ')"/>
<p:input port="source">
<p:empty/>
</p:input>
</p:exec>
</p:for-each>
我不知道p:iteration-source元素中的XPath-2.0表达式是否有问题。但是,下面的代码是有效的,并给出了正确的结果"Message: 3":
<cx:message>
<p:with-option name="message" select="count((1 to $latex-repeat))"/>
<p:input port="source">
<p:empty/>
</p:input>
</cx:message>我的exec-step在for-each循环外部进行了测试,并正常工作。我在氧气16.0下和卡拉巴什一起工作。
发布于 2015-08-25 19:26:04
这种方法的问题在于,在XML1.0中只能遍历XProc文档。由于p:iteration-source中的select表达式生成的是一个数字序列,而不是文档,因此管道将失败并显示一个动态错误(最有可能是err:XD0001: It is a dynamic error if a non-XML resource is produced on a step output or arrives on a step input.)
(使用cx:message的示例之所以有效,是因为p:with-option构造将一个值绑定到一个选项,然后将select XPath表达式的结果视为xs:untypedAtomic。)
要执行n次循环,您必须使用递归,如下面的示例所示(未经测试,但您应该明白其中的含义):
<p:declare-step version="1.0"
xmlns:p="http://www.w3.org/ns/xproc"
xmlns:c="http://www.w3.org/ns/xproc-step"
xmlns:my="http://example.org/latex-process"
type="my:latex-process">
<p:option name="latex-exec" select="'uri/latex'"/>
<p:option name="latex-repeat" select="3"/>
<p:option name="tmp-path" select="'uri/tmp/'"/>
<p:option name="tmp-file" select="'tmp'"/>
<p:choose>
<p:when test="$latex-repeat = 0">
<p:sink>
<p:input port="source">
<p:empty/>
</p:input>
</p:sink>
</p:when>
<p:otherwise>
<p:exec result-is-xml="false">
<p:with-option name="command" select="$latex-exec"/>
<p:with-option name="args" select="string-join(('-interaction=nonstopmode','-halt-on-error','-output-format=pdf',concat('-output-directory=',$tmp-path),concat($tmp-path,$tmp-file,'.tex')),' ')"/>
<p:input port="source">
<p:empty/>
</p:input>
</p:exec>
<my:latex-exec>
<p:with-option name="latex-exec" select="$latex-exec"/>
<p:with-option name="latex-repeat" select="$latex-repeat - 1"/>
<p:with-option name="tmp-path" select="$tmp-path"/>
<p:with-option name="tmp-file" select="$tmp-file"/>
</my:latex-exec>
</p:otherwise>
</p:choose>
</p:declare-step>发布于 2015-08-28 17:13:26
正如Vojt ch Toman所提到的,p:ě-each循环不能迭代原子值[Vojt]。由于此限制不适用于XSLT2.0循环等效项,因此我们可以定义自己的p:xslt步骤,以便根据所需的迭代次数创建一系列XML节点。然后,此序列可用作p: for -each的输入。
迭代器步骤将如下所示:
<p:declare-step version="1.0"
xmlns:p="http://www.w3.org/ns/xproc"
xmlns:c="http://www.w3.org/ns/xproc-step"
xmlns:my="http://example.org/iterate"
type="my:iterator">
<p:option name="iterate" required="true"/>
<p:input port="source"/>
<p:output port="result" sequence="true"/>
<p:xslt name="iterator" template-name="iterator">
<p:with-param name="iterator" select="$iterate"/>
<p:input port="stylesheet">
<p:inline>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
<xsl:param name="iterator"/>
<xsl:template name="iterator">
<xsl:for-each select="(1 to xs:integer($iterator))">
<xsl:result-document href="{position()}">
<iterate/>
</xsl:result-document>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
</p:inline>
</p:input>
<p:input port="source">
<p:empty/>
</p:input>
</p:xslt>
<p:sink/>
<p:identity>
<p:input port="source">
<p:pipe port="secondary" step="iterator"/>
</p:input>
</p:identity>
</p:declare-step>使用新迭代器步骤的示例如下所示:
<p:declare-step version="1.0"
xmlns:p="http://www.w3.org/ns/xproc"
xmlns:c="http://www.w3.org/ns/xproc-step"
xmlns:my="http://example.org/iterate">
<p:import href="iterate.xpl"/>
<!-- pre-processing -->
<my:iterator>
<p:with-option name="iterate" select="3"/>
</my:iterator>
<p:for-each>
<!-- sub-pipeline -->
</p:for-each>
</p:declare-step>https://stackoverflow.com/questions/31277642
复制相似问题