如果您只想根据位置选择元素,请使用
<xsl:param name="error-url" select="'ErrorList.xml'"/>
<xsl:variable name="errors" select="doc($error-url)/ErrorList/error"/>
<xsl:template match="Validations">
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="Validations/*">
<xsl:variable name="pos" select="position()"/>
<xsl:if test="saxon:evaluate(.)">
<xsl:copy-of select="$errors[$pos]"/>
</xsl:if>
</xsl:template>
这是一个更完整的示例。假设我们的主要输入为
<a>
<c>
<UPDATE-IND>A</UPDATE-IND>
<DUP-OVERRIDE>B</DUP-OVERRIDE>
<TYPE-RECORD>R</TYPE-RECORD>
</c>
</a>
并且test2013110502.xml
作为
<Validations>
<Condition-1>UPDATE-IND!='A'</Condition-1>
<Condition-2>DUP-OVERRIDE='B'</Condition-2>
<Condition-3>TYPE-RECORD!='R'</Condition-3>
</Validations>
(注意您的示例的更改,右侧有字符串文字)并且文件test2013110503.xml
为
<ErrorList>
<error>
<errorcode>1</errorcode>
<errordescription>UPDATE INDICATOR.</errordescription>
</error>
<error>
<errorcode>2</errorcode>
<errordescription>SHOULD NOT BE BLANK.</errordescription>
</error>
<error>
<errorcode>3</errorcode>
<errordescription>NOT IN LIST</errordescription>
</error>
<error>
<errorcode>4</errorcode>
<errordescription>PARSE ERROR.</errordescription>
</error>
</ErrorList>
样式表
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:saxon="http://saxon.sf.net/"
exclude-result-prefixes="xs saxon">
<xsl:param name="conditions-url" as="xs:string" select="'test2013110502.xml'"/>
<xsl:param name="conditions" as="document-node()" select="doc($conditions-url)"/>
<xsl:param name="error-url" as="xs:string" select="'test2013110503.xml'"/>
<xsl:param name="errors" as="document-node()" select="doc($error-url)"/>
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:template match="c">
<errors>
<xsl:apply-templates select="$conditions/Validations/*">
<xsl:with-param name="context" select="current()"/>
</xsl:apply-templates>
</errors>
</xsl:template>
<xsl:template match="Validations/*">
<xsl:param name="context"/>
<xsl:variable name="pos" select="position()"/>
<xsl:apply-templates select="$context" mode="eval">
<xsl:with-param name="expression" select="string(current())"/>
<xsl:with-param name="pos" select="$pos"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="c" mode="eval">
<xsl:param name="expression"/>
<xsl:param name="pos"/>
<xsl:if test="saxon:evaluate($expression)">
<xsl:copy-of select="$errors/ErrorList/error[$pos]"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
输出
<errors>
<error>
<errorcode>2</errorcode>
<errordescription>SHOULD NOT BE BLANK.</errordescription>
</error>
</errors>
dyn:evaluate
支持 EXSLT功能的 XSLT 1.0 处理器也应该可以做到这一点:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:dyn="http://exslt.org/dynamic"
exclude-result-prefixes="dyn">
<xsl:param name="conditions-url" select="'test2013110502.xml'"/>
<xsl:param name="conditions" select="document($conditions-url)"/>
<xsl:param name="error-url" select="'test2013110503.xml'"/>
<xsl:param name="errors" select="document($error-url)"/>
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:template match="c">
<errors>
<xsl:apply-templates select="$conditions/Validations/*">
<xsl:with-param name="context" select="current()"/>
</xsl:apply-templates>
</errors>
</xsl:template>
<xsl:template match="Validations/*">
<xsl:param name="context"/>
<xsl:variable name="pos" select="position()"/>
<xsl:apply-templates select="$context" mode="eval">
<xsl:with-param name="expression" select="string(current())"/>
<xsl:with-param name="pos" select="$pos"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="c" mode="eval">
<xsl:param name="expression"/>
<xsl:param name="pos"/>
<xsl:if test="dyn:evaluate($expression)">
<xsl:copy-of select="$errors/ErrorList/error[$pos]"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
我得到结果
<?xml version="1.0"?>
<errors>
<error>
<errorcode>2</errorcode>
<errordescription>SHOULD NOT BE BLANK.</errordescription>
</error>
</errors>
和xsltproc
Using libxml 20708, libxslt 10126 and libexslt 815
xsltproc was compiled against libxml 20706, libxslt 10126 and libexslt 815
libxslt 10126 was compiled against libxml 20706
libexslt 815 was compiled against libxml 20706
在 Windows 上。