【发布时间】:2021-06-09 13:55:50
【问题描述】:
我是 XSLT 的新手,我正在尝试根据某些条件声明数组并在数组中添加元素,然后检查数组是否有任何元素。
输入xml:-
<?xml version="1.0"?>
<Employees>
<data>
<name>rocky</name>
<sal>1</sal>
</data>
</Employees>
xslt:-
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/Employee">
<xsl:variable name="empname" select="./data/name"/>
<xsl:variable name="sal" select="./data/sal"/>
<xsl:variable name='errorList'/> -- here i am not sure, how to declare the array and is this errorList accessible in another template i have created. if not then i am planning to declare that outside both the templates
<Details>
<EmployeeName>
<xsl:choose>
<xsl:when test="not($empname=('rocky'))"/>
//here i have to populate errorlist with 'name is invalid'
</xsl:choose>
<xsl:choose>
<xsl:when test="not($sal=('10'))"/>
//here i have to populate errorlist with 'salary is invalid greater than 1'
</xsl:choose>
//here i have to check if errorlist is not empty, then if this condition is satisfied then appy the below template
<xsl:apply-templates select="/Employee/data/name"/>
</EmployeeName>
</Details>
</xsl:template>
<xsl:template match="/Employee/data/name">
here i will apply for loop on errorList
<xsl:for-each select="errorList">
<ErrorList>
<Error>
</Error>
</ErrorList>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
所需的输出是我想要的动态,我可以使用 xslt 的任何其他功能来存储错误并立即显示:-
<ErrorList>
<Error>'name is invalid'</Error>
<Error>'salary is invalid greater than 1'</Error>
</ErrorList>
【问题讨论】:
-
你能解释一下你需要的输出是什么吗?在 XSLT 中,您不需要“声明数组”。
-
我已经更新了我需要的输出,请告诉我如何实现@Sebastien
标签: xml xslt xslt-2.0 xslt-3.0