在XSL中有没有办法更新全局变量?
我想检查一下我已经改变了哪些元素并采取相应的行动.这将要求我以某种方式将元素的名称添加到某种列表中,并在每次转换新元素时更新它.
但是,由于xsl:variable人们没有预期的"变量",所以一旦定义,我就无法添加任何东西.
我有多个包含的数据文件,因此使用仅知道当前节点集的xsl函数将无济于事.
==编辑==
这就是我现在的转型.但它将包括每次在不同子文件中重复引用的文件.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- include the contents of referenced files -->
<xsl:template match="reference">
<xsl:apply-templates select="document(@url)/data/node()" />
</xsl:template>
</xsl:transform>
Run Code Online (Sandbox Code Playgroud)
数据文件看起来像这样:
<data>
<reference url="another_data_file.xml"/>
... other stuff ...
</data>
Run Code Online (Sandbox Code Playgroud)
XSLT是一种函数式语言,不允许更新变量.如果需要通过多个步骤聚合结果,通常的方法是使用递归模板.例:
<xsl:template name="transform-elements">
<xsl:param name="elements-to-process" select="/.."/>
<xsl:param name="processed-elements" select="/.."/>
<xsl:if test="$elements-to-process">
<xsl:variable name="element" select="$elements-to-process[1]"/>
<!-- ... Do stuff with $element ...-->
<!-- Recursively invoke template for remaining elements -->
<xsl:call-template name="transform-elements">
<xsl:with-param name="elements-to-process"
select="$elements-to-process[position() != 1]"/>
<xsl:with-param name="processed-elements"
select="$processed-elements|$element"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)