[英]XSLT integer evaluator, how to implement n-ary sum and multiplication?
我正在尝试实现一个小的整数评估器。 varDef
,它处理的xml文档有一个varDef
,以及一个带有可能变量值的varDef
列表。
XSLT将该XML文档转换为带有结果的另一个文档。
这是XML文档的XML模式:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:ej3="http://procesadores.ejemplo.com/Ej3"
targetNamespace="http://procesadores.ejemplo.com/Ej3"
elementFormDefault="qualified">
<element name="documento">
<complexType>
<sequence>
<element ref="ej3:expr"/>
<element ref="ej3:varDef" maxOccurs="unbounded" minOccurs="0"/>
</sequence>
</complexType>
</element>
<element name="expr" abstract="true"/>
<element name="suma" type="ej3:expBinaria" substitutionGroup="ej3:expr"/>
<element name="resta" type="ej3:expBinaria" substitutionGroup="ej3:expr"/>
<element name="mult" type="ej3:expBinaria" substitutionGroup="ej3:expr"/>
<element name="div" type="ej3:expBinaria" substitutionGroup="ej3:expr"/>
<element name="mod" type="ej3:expBinaria" substitutionGroup="ej3:expr"/>
<element name="opuesto" type="ej3:expUnaria" substitutionGroup="ej3:expr"/>
<element name="abs" type="ej3:expUnaria" substitutionGroup="ej3:expr"/>
<element name="var" type="ej3:tipoNombreVar" substitutionGroup="ej3:expr"/>
<element name="cons" type="integer" substitutionGroup="ej3:expr"/>
<element name="varDef">
<complexType>
<simpleContent>
<extension base="int">
<attribute name="nombre" type="ej3:tipoNombreVar"/>
</extension>
</simpleContent>
</complexType>
</element>
<complexType name="expUnaria">
<sequence>
<element ref="ej3:expr" minOccurs="1" maxOccurs="1"/>
</sequence>
</complexType>
<complexType name="expBinaria">
<sequence>
<element ref="ej3:expr" minOccurs="2" maxOccurs="2"/>
</sequence>
</complexType>
<complexType name="expNaria">
<sequence>
<element ref="ej3:expr" minOccurs="0" maxOccurs="unbounded"/>
</sequence>
</complexType>
<simpleType name="tipoNombreVar">
<restriction base="string">
<pattern value="[a-zA-Z][a-zA-Z0-9]*"/>
</restriction>
</simpleType>
</schema>
这是XSLT文档:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:ej3="http://procesadores.ejemplo.com/Ej3"
exclude-result-prefixes="xs"
version="2.0">
<xsl:strip-space elements="ej3:*"/>
<xsl:output
method="xml"
indent="yes"
encoding="utf-8"/>
<xsl:key name="defVariables" match="ej3:varDef" use="@nombre"/>
<xsl:template match="/ej3:documento">
<cons><xsl:apply-templates select="*[not(local-name()='varDef')]"/></cons>
</xsl:template>
<xsl:template match="ej3:suma">
<xsl:variable name="s1">
<xsl:apply-templates select="child::node()[1]"/>
</xsl:variable>
<xsl:variable name="s2">
<xsl:apply-templates select="child::node()[2]"/>
</xsl:variable>
<xsl:value-of select="$s1 + $s2"/>
</xsl:template>
<xsl:template match="ej3:resta">
<xsl:variable name="s1">
<xsl:apply-templates select="child::node()[1]"/>
</xsl:variable>
<xsl:variable name="s2">
<xsl:apply-templates select="child::node()[2]"/>
</xsl:variable>
<xsl:value-of select="$s1 - $s2"/>
</xsl:template>
<xsl:template match="ej3:mult">
<xsl:variable name="s1">
<xsl:apply-templates select="child::node()[1]"/>
</xsl:variable>
<xsl:variable name="s2">
<xsl:apply-templates select="child::node()[2]"/>
</xsl:variable>
<xsl:value-of select="$s1 * $s2"/>
</xsl:template>
<xsl:template match="ej3:div">
<xsl:variable name="s1">
<xsl:apply-templates select="child::node()[1]"/>
</xsl:variable>
<xsl:variable name="s2">
<xsl:apply-templates select="child::node()[2]"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="$s2 = 0">
<xsl:value-of select="$s1 div $s2"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="xs:integer($s1 div $s2)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="ej3:mod">
<xsl:variable name="s1">
<xsl:apply-templates select="child::node()[1]"/>
</xsl:variable>
<xsl:variable name="s2">
<xsl:apply-templates select="child::node()[2]"/>
</xsl:variable>
<xsl:value-of select="$s1 mod $s2"/>
</xsl:template>
<xsl:template match="ej3:opuesto">
<xsl:variable name="s1">
<xsl:apply-templates select="child::node()[1]"/>
</xsl:variable>
<xsl:value-of select="- $s1"/>
</xsl:template>
<xsl:template match="ej3:abs">
<xsl:variable name="s1">
<xsl:apply-templates select="child::node()[1]"/>
</xsl:variable>
<xsl:value-of select="abs($s1)"/>
</xsl:template>
<xsl:template match="ej3:var">
<xsl:value-of select="key('defVariables',.)"/>
</xsl:template>
<xsl:template match="ej3:cons">
<test><xsl:value-of select="."/></test>
</xsl:template>
</xsl:stylesheet>
这一切都按我的预期进行。 但是我想做suma
(求和)和mult
n元运算符。 也就是说,像这样:
<suma>
<cons>1</cons>
<cons>2</cons>
<cons>3</cons>
</suma>
应该能够被评估。 为了使它起作用,我必须修改suma
xsl:template,但是我不太确定该怎么做。 我尝试过一些服务器式的事情,但是我必须以某种方式评估这些孩子,然后再将它们加起来,这使我很难找到解决方案。
您能建议如何实现吗?
请注意,我希望sum
和mult
操作数都以这种方式工作,因此基于xpath函数sum()
的解决方案将不适用于mult
。
我将确保您的模板返回xs:integer
序列,例如change
<xsl:template match="ej3:cons">
<test><xsl:value-of select="."/></test>
</xsl:template>
至
<xsl:template match="ej3:cons">
<xsl:sequence select="xs:integer(.)"/>
</xsl:template>
那么你可以使用
<xsl:template match="suma">
<xsl:variable name="operands" as="xs:integer+">
<xsl:apply-templates select="*"/>
</xsl:variable>
<xsl:sequence select="sum($operands)"/>
</xsl:template>
对于乘法,可以使用一个函数(需要声明绑定到某些名称空间的前缀mf
)
<xsl:function name="mf:multiply" as="xs:integer">
<xsl:param name="operands" as="xs:integer+"/>
<xsl:sequence select="if (not(exists($operands[2])))
then $operands[1]
else $operands[1] * mf:multiply($operands[position() gt 1])"/>
</xsl:function>
然后用在
<xsl:template match="multa">
<xsl:variable name="operands" as="xs:integer+">
<xsl:apply-templates select="*"/>
</xsl:variable>
<xsl:sequence select="mf:multiply($operands)"/>
</xsl:template>
这是一个例子:
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="xs mf">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:function name="mf:multiply" as="xs:integer">
<xsl:param name="operands" as="xs:integer+"/>
<xsl:sequence select="if (not(exists($operands[2])))
then $operands[1]
else $operands[1] * mf:multiply($operands[position() gt 1])"/>
</xsl:function>
<xsl:template match="expression">
<result>
<xsl:apply-templates/>
</result>
</xsl:template>
<xsl:template match="cons">
<xsl:sequence select="xs:integer(.)"/>
</xsl:template>
<xsl:template match="suma">
<xsl:variable name="operands" as="xs:integer+">
<xsl:apply-templates select="*"/>
</xsl:variable>
<xsl:sequence select="sum($operands)"/>
</xsl:template>
<xsl:template match="multa">
<xsl:variable name="operands" as="xs:integer+">
<xsl:apply-templates select="*"/>
</xsl:variable>
<xsl:sequence select="mf:multiply($operands)"/>
</xsl:template>
</xsl:stylesheet>
输入示例
<expression>
<suma>
<cons>1</cons>
<cons>2</cons>
<cons>3</cons>
<multa>
<cons>1</cons>
<cons>2</cons>
<cons>3</cons>
</multa>
</suma>
</expression>
我得到输出<result>12</result>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.