使用 XSLT 在假期列表中查找日期(如果存在或不存在)

Lookup for a date in a list of holidays if present or not using XSLT

我有一个 xml,其中包含假期列表:

<root>
  <row>
    <Date>2021-01-01</Date>
  </row>
  <row>
    <Date>2021-02-05</Date>
  </row>

...

我有一个输入 xml,里面有一个日期字段:

<Report_data>
  <Report_entry>
    <Input_date>2021-01-01</Input_date>
  </Report_entry>
  <Report_entry>
    <Input_date>2021-01-15</Input_date>
  </Report_entry>

...

我想读取输入 xml 并根据假期列表中存在的 Input_date 输出一个 true/false 值。 预期输出:

<Data>
  <output>
    <Input_date>2021-01-01</Input_date>
    <Flag>True</Flag>
  </output>
  <output>
    <Input_date>2021-01-15</Input_date>
    <Flag>False</Flag>
  </output>

...

可以使用 XSLT 3.0 完成吗?

你没有提到你到目前为止做了什么。我仍然假设,您有一个需要更新的 XSL。

这里用一个变量来存储假期列表:

  <xsl:variable name="holidays" as="node()">
      <rows>
          <row>
            <Date>2021-01-01</Date>
          </row>
          <row>
            <Date>2021-02-05</Date>
          </row>
      </rows>
  </xsl:variable> 

然后您需要将这些值与您的输入进行比较:

<xsl:template match="Report_entry">
    <xsl:element name="output">
        <xsl:apply-templates/>
        <xsl:element name="Flag">
            <xsl:choose>
                <xsl:when test="$holidays//Date = Input_date">
                    <xsl:text>TRUE</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>False</xsl:text>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:element>
    </xsl:element>
  </xsl:template> 

测试:https://xsltfiddle.liberty-development.net/asoTKv

虽然发布的解决方案解决了问题,但在 XSLT 3 中有更优雅和紧凑的方法,在作为参数提供的辅助输入文档上使用键,使用模板匹配和文本值模板:

<?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"
    exclude-result-prefixes="#all"
    expand-text="yes"
    version="3.0">
    
  <xsl:param name="holidays">
      <rows>
          <row>
            <Date>2021-01-01</Date>
          </row>
          <row>
            <Date>2021-02-05</Date>
          </row>
      </rows>
  </xsl:param>
    
  <xsl:key name="date" match="rows/rows/Date" use="."/>

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:output method="xml" indent="yes"/>
  
  <xsl:template match="Report_entry/Input_date">
      <xsl:next-match/>
      <Flag>{exists([key('date', ., $holidays)])}</Flag>
  </xsl:template>
  
  <xsl:template match="Report_data">
      <Data>
          <xsl:apply-templates/>
       </Data>
  </xsl:template>
    
  <xsl:template match="Report_entry">
      <output>
          <xsl:apply-templates/>
      </output>
  </xsl:template>
  
</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/asoTKv/1