【问题标题】:add elements in array dynamically using xslt使用 xslt 在数组中动态添加元素
【发布时间】: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


【解决方案1】:

类似这样的内容:已编辑

<?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>

【讨论】:

  • 您的意思是当我们使用 xsl:apply-templates select="$errorList"/> 应用模板时,控制转到错误模板中?如果是这种情况,那么我该如何访问 errorList ?或错误模板中的nameIsInvalid 和salaryIsInvalid
  • 是的,通过 errorList 中的每个错误都将在
  • 我正在尝试在响应中显示两个错误,请让我知道我在这里缺少什么。
  • 查看模板中的示例
【解决方案2】:

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

<?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

【讨论】:

    猜你喜欢
    • 2010-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 2012-04-18
    • 2013-08-04
    相关资源
    最近更新 更多