You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否使用XSLT 3.0实现输入日期与节假日列表的匹配判断并输出布尔标识?

Absolutely! XSLT 3.0 is a great fit for this task—its built-in support for loading external documents, sequence operations, and clean node processing makes this straightforward. Let’s walk through a complete solution tailored to your requirements:

Solution Overview
  • First, we’ll load the holiday list XML and store its dates in a sequence for fast lookups.
  • Next, we’ll process each entry from your input data XML, checking if the Input_date exists in the holiday sequence.
  • Finally, we’ll generate the output XML with the original date and a corresponding boolean flag.

Complete XSLT 3.0 Code

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- Store all holiday dates in a sequence for efficient membership checks -->
    <xsl:variable name="holiday-dates" as="xs:string*">
        <xsl:sequence select="document('holidays.xml')/root/row/Date/text()"/>
    </xsl:variable>

    <!-- Match the root of the input data XML -->
    <xsl:template match="/Report_data">
        <Data>
            <!-- Process each Report_entry in the input -->
            <xsl:for-each select="Report_entry">
                <output>
                    <!-- Copy the original Input_date element -->
                    <xsl:copy-of select="Input_date"/>
                    <!-- Add the Flag element with the holiday check result -->
                    <Flag>
                        <xsl:value-of select="if (Input_date/text() = $holiday-dates) then 'True' else 'False'"/>
                    </Flag>
                </output>
            </xsl:for-each>
        </Data>
    </xsl:template>
</xsl:stylesheet>

Key Details Explained

  1. Loading the Holiday List

    • The document('holidays.xml') function loads your external holiday XML file. Ensure the filename/path matches where you’ve stored your holiday list relative to the XSLT processor’s working directory.
    • We store all holiday dates in the $holiday-dates sequence, which acts like a set for near-instant membership checks.
  2. Date Matching Logic

    • The expression Input_date/text() = $holiday-dates checks if the input date exists in the holiday sequence. We wrap this in an if/else to output capitalized True/False as specified in your requirement.
    • If you prefer lowercase true/false (the default XSLT boolean output), you can simplify this to <xsl:value-of select="Input_date/text() = $holiday-dates"/>.
  3. Output Structure

    • We mirror your expected output format exactly: wrapping each result in an <output> element, retaining the original <Input_date>, and adding the <Flag> element with the check result.

How to Use

  1. Save your holiday list as holidays.xml (adjust the filename in the document() function if needed).
  2. Save the XSLT above as holiday-check.xsl.
  3. Run the transformation using an XSLT 3.0-compliant processor (like Saxon HE/PE/EE, or Altova XMLSpy). For example, with Saxon from the command line:
    java -jar saxon-he-12.4.jar -s:input-data.xml -xsl:holiday-check.xsl -o:output.xml
    

Example Input/Output

Input Data XML (input-data.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>
</Report_data>

Holiday XML (holidays.xml):

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

Output XML (output.xml):

<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>
</Data>

内容的提问来源于stack exchange,提问作者Adee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 07:17:43