【发布时间】:2015-03-31 22:06:36
【问题描述】:
我正在尝试实现一个小的整数求值器。事实上,它处理的 xml 文档有一个表达式,以及一个 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(sum) 和 mult n-ary 运算符。也就是说,像这样:
<suma>
<cons>1</cons>
<cons>2</cons>
<cons>3</cons>
</suma>
应该能够被评估。为了使它工作,我必须修改suma xsl:template,但我不太确定如何去做。我已经尝试了几件事,但事实上我必须在将孩子相加之前以某种方式对其进行评估,这让我很难找到解决方案。
您能建议如何实现这一目标吗?
请注意,我希望sum 和mult 操作数都以这种方式工作,因此基于xpath 函数sum() 的解决方案不适用于mult。
【问题讨论】:
标签: xslt integer xslt-2.0 evaluation