【问题标题】:XSLT Error - A sequence of more than one item is not allowed as the first argument of Date formatXSLT 错误 - 不允许将多个项目的序列作为日期格式的第一个参数
【发布时间】:2021-03-08 06:00:05
【问题描述】:

我正在尝试使用 XSLT 代码来读取“公司”字段值

XML 有 2 个单元,分别是(5000 个有 3 个记录,2 个活动 1 个不活动)和(6000 个有 3 个记录,2 个活动和 1 个不活动)

只有当 6000“StartDate”中的一个单位与单位 5000“StartDate”字段的日期范围匹配时,我才必须阅读“company”字段值表单单元 6000

现场公司价值的预期 XSLT 输出是“2010”,单位 6000,因为该记录日期与 5000 单位记录之一匹配

日期范围可以完全相同,也可以在开始和结束日期范围之间

我写的下面的代码抛出错误,任何建议都有帮助

XML

    <?xml version="1.0" encoding="UTF-8"?>
<AllRecords>
   <Records>
      <Record>
         <Items>
            <EndDate>2021-03-31T00:00:00.000</EndDate>
            <PlantDetails>
               <PlantDetailsUS>
                  <company>1000</company>
                  <Newplant>TEST1</Newplant>
               </PlantDetailsUS>
            </PlantDetails>
            <StartDate>2021-01-01T00:00:00.000</StartDate>
            <Unit>5000</Unit>
            <Status>A</Status>
         </Items>
         <Items>
            <EndDate>2020-12-31T00:00:00.000</EndDate>
            <PlantDetails>
               <PlantDetailsUS>
                  <company>1000</company>
                  <Newplant>TEST2</Newplant>
               </PlantDetailsUS>
            </PlantDetails>
            <StartDate>2020-05-01T00:00:00.000</StartDate>
            <Unit>5000</Unit>
            <Status>A</Status>
         </Items>
         <Items>
            <EndDate>2021-03-31T00:00:00.000</EndDate>
            <PlantDetails>
               <PlantDetailsUS>
                  <company>1000</company>
                  <Newplant>TEST3</Newplant>
               </PlantDetailsUS>
            </PlantDetails>
            <StartDate>2021-01-01T00:00:00.000</StartDate>
            <Unit>5000</Unit>
            <Status>I</Status>
         </Items>
         <Items>
            <EndDate>2020-12-31T00:00:00.000</EndDate>
            <PlantDetails>
               <PlantDetailsUS>
                  <company>2000</company>
                  <Newplant>TEST4</Newplant>
               </PlantDetailsUS>
            </PlantDetails>
            <StartDate>2020-01-01T00:00:00.000</StartDate>
            <Unit>6000</Unit>
            <Status>A</Status>
         </Items>
         <Items>
            <EndDate>2021-03-31T00:00:00.000</EndDate>
            <PlantDetails>
               <PlantDetailsUS>
                  <company>2010</company>
                  <Newplant>TEST5</Newplant>
               </PlantDetailsUS>
            </PlantDetails>
            <StartDate>2021-01-01T00:00:00.000</StartDate>
            <Unit>6000</Unit>
            <Status>A</Status>
         </Items>
  <Items>
            <EndDate>2021-03-31T00:00:00.000</EndDate>
            <PlantDetails>
               <PlantDetailsUS>
                  <company>2020</company>
                  <Newplant>TEST5</Newplant>
               </PlantDetailsUS>
            </PlantDetails>
            <StartDate>2021-01-01T00:00:00.000</StartDate>
            <Unit>6000</Unit>
            <Status>I</Status>
         </Items>
      </Record>
   </Records>
</AllRecords>

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" version="2.0">
   <xsl:template match="/">
      <Root>
         <xsl:apply-templates />
      </Root>
   </xsl:template>
   <xsl:template match="AllRecords/Records/Record">
      <xsl:if test="Record/Items[unit=5000]/unit =5000 or Record/Items[unit=6000]/unit = 6000">
         <xsl:choose>
            <xsl:when test="Record/Items[unit=5000][Status='A']">
               <xsl:for-each select="Record/Items[unit='5000'][Status='A']">
                  <row>
                     <xsl:if test="substring(//Items[Status = 'A' and unit=5000]/StartDate,1,10) &gt;= substring(//Items[Status = 'A' and ( unit=6000) ]/StartDate,1,10)">
                        &gt;
                        <xsl:value-of select="//company" />
                     </xsl:if>
                  </row>
               </xsl:for-each>
            </xsl:when>
         </xsl:choose>
      </xsl:if>
   </xsl:template>
</xsl:stylesheet>

【问题讨论】:

  • 您问题的样本数据中是否有值为“2010”的记录?

标签: xslt xslt-1.0 xslt-2.0


【解决方案1】:

替换

substring(//Items[Status = 'A' and unit=5000]/StartDate,1,10)

通过

//Items[Status = 'A' and unit=5000]/StartDate ! substring(., 1, 10)

在 3.0 中,或通过

for $x in //Items[Status = 'A' and unit=5000]/StartDate return substring(., 1, 10)

//Items[Status = 'A' and unit=5000]/StartDate / substring(., 1, 10)

在 2.0 中

substring() 和许多其他这样的函数,期望一个字符串作为操作数,如果你想将该函数应用于多个字符串,你必须明确地这样做。

【讨论】:

  • 谢谢,我试过了,但没有成功,子字符串不是问题,问题是循环遍历多条记录并获得准确的值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-02
  • 1970-01-01
  • 1970-01-01
  • 2016-02-06
  • 2016-12-23
  • 2010-11-29
  • 2018-07-05
相关资源
最近更新 更多