能否使用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_dateexists 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
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-datessequence, which acts like a set for near-instant membership checks.
- The
Date Matching Logic
- The expression
Input_date/text() = $holiday-dateschecks if the input date exists in the holiday sequence. We wrap this in anif/elseto output capitalizedTrue/Falseas 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"/>.
- The expression
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.
- We mirror your expected output format exactly: wrapping each result in an
How to Use
- Save your holiday list as
holidays.xml(adjust the filename in thedocument()function if needed). - Save the XSLT above as
holiday-check.xsl. - 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
相关产品推荐
相关产品推荐

