使用 xslt 动态添加数组中的元素

add elements in array dynamically using xslt

我是 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>

所需的输出是我想要的 dynamicall,我可以使用 xslt 的任何其他功能来存储错误并立即显示:-

 <ErrorList>
<Error>'name is invalid'</Error>
<Error>'salary is invalid greater than 1'</Error>
</ErrorList>

我不太确定你想要完成什么,但这里有一些东西可以帮助你开始:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">

  <xsl:output method="xml" indent="yes" />

  <xsl:template match="/">
    <ErrorList>
      <xsl:apply-templates select="Employees/data"/>
    </ErrorList>
  </xsl:template>
  
  <xsl:template match="data">
    <EmployeeName>
      <xsl:if test="not(name='rocky')">
        <Error>Name is invalid</Error>
      </xsl:if>
      <xsl:if test="not(sal='10')">
        <Error>Salary is invalid, greater than 1</Error>
      </xsl:if>
    </EmployeeName>
  </xsl:template>
  
</xsl:stylesheet>

看到它在这里工作:https://xsltfiddle.liberty-development.net/eieFA11

类似这样的内容:已编辑

<?xml version="1.0" encoding="UTF-8"?>
<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' as="element()*">
      <xsl:if test="not($empname=('rocky'))">
        <error type="nameIsInvalid"/>
      </xsl:if>
      <xsl:if test="not($sal=('10'))">
        <error type="salaryIsInvalid"/>
      </xsl:if>
    </xsl:variable>

    <Details>
      <EmployeeName>
        <xsl:choose>
          <xsl:when test="count($errorList) gt 0">
            <xsl:apply-templates select="$errorList"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates select="data/name"/>
          </xsl:otherwise>
        </xsl:choose>
      </EmployeeName>
    </Details>
  </xsl:template>
  
  <xsl:template match="/Employee/data/name">
    <!-- Do something with the name -->
  </xsl:template>
    
  <xsl:template match="error">
    <!-- Do something with the error -->
    <!-- i.e. just copy the error-->
    <xsl:copy-of select="."/>
  </xsl:template>

  
</xsl:stylesheet>