XSLT中format-date函数使用问题:日期格式化为指定样式
Hey there! Let's troubleshoot why your XSLT format-date function isn't outputting that nice "Thursday 26 April" format you need for your weather XML homework. I've run into similar snags before, so let's break down the most likely issues and fixes step by step.
1. First: Are You Using XSLT 2.0+?
The format-date function is only available in XSLT 2.0 and later—most default processors (like the ones built into browsers or Python's lxml library) stick to XSLT 1.0, which doesn't support this function at all. That's probably the biggest culprit here.
Fix:
- Make sure your XSL stylesheet explicitly declares version 2.0 at the top:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
- Swap to an XSLT 2.0-compatible processor. For Python, the easiest option is using the
saxonchelibrary (Saxon is the go-to for XSLT 2.0/3.0 support).
2. Double-Check Your format-date Pattern
Your target format is "Weekday Full Name + Day Number + Month Full Name"—the correct pattern string for that is:
format-date($your-date-value, '[FNn] [D1] [MNn]')
Let's break that down:
[FNn]: Outputs the full weekday name (e.g., Thursday)[D1]: Outputs the day number without leading zeros (e.g., 26 instead of 02)[MNn]: Outputs the full month name (e.g., April)
Also, make sure you're passing a valid xs:date type to the function. If your XML's date is a plain string (like 2024-04-26), convert it first:
format-date(xs:date(date), '[FNn] [D1] [MNn]')
If your XML uses a non-standard date format (like 04/26/2024), you'll need to reformat it to YYYY-MM-DD before converting to xs:date.
3. Test with a Working Example
Let's use a sample setup to verify things work. Suppose your weather XML looks like this:
<weatherData> <dailyRecord> <date>2024-04-26</date> <highTemp>22</highTemp> </dailyRecord> </weatherData>
Your XSLT 2.0 stylesheet should look like this:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xsl:output method="html" indent="yes"/> <xsl:template match="/"> <html> <body> <h2>Daily Weather Summary</h2> <xsl:for-each select="weatherData/dailyRecord"> <div class="record"> <p><strong>Date:</strong> <xsl:value-of select="format-date(xs:date(date), '[FNn] [D1] [MNn]')"/></p> <p><strong>High Temperature:</strong> <xsl:value-of select="highTemp"/>°C</p> </div> </xsl:for-each> </body> </html> </xsl:template> </xsl:stylesheet>
To process this with Python, use saxonche with this code:
from saxonche import PySaxonProcessor with PySaxonProcessor(license=False) as proc: xslt_processor = proc.new_xslt30_processor() compiled_stylesheet = xslt_processor.compile_stylesheet(stylesheet_file="weather-stylesheet.xsl") result = compiled_stylesheet.transform_to_string(source_file="weather.xml") print(result)
This should output exactly the formatted date you want.
4. Troubleshooting Your Current Setup
- If testing in a browser: Most browsers only support XSLT 1.0, so
format-datewon't work. Test with a server-side processor like Saxon instead. - Check for path errors: Make sure your XSL is selecting the correct date node from your XML (e.g., if your date is under
<weather/entry/date>instead of<date>, adjust yourselectpath). - Check for errors: Run your Python code with error handling to catch any processor warnings—this will tell you if the date is invalid or the function is unsupported.
内容的提问来源于stack exchange,提问作者Ovaflow

