如果XSLT中满足日期比较测试表达式,则在输出xml中显示错误消息

问题描述

每当修订日期大于发布日期时,我都必须在输出xml中显示一条错误消息。我必须将每个修订日期与相应的出版日期进行比较。输入xml中可以有多个“修订日期”,但是一本书只有一个发布日期。

在运行XSLT代码时,由于要在输出xml中未显示错误消息,因此我正在执行日期比较的测试表达式变为false。

下面是输入XML:

<catalog>
   <book id="bk101">
      <author>Gambardella,Matthew</author>
      <title>XML Developer's Guide</title>
      <publish_date>2020-10-01</publish_date>
      <revision_date>2019-09-02</revision_date>
      <revision_date>2021-12-02</revision_date>
   </book>
   <book id="bk102">
      <author>John,Doe</author>
      <title>XPath Developer's Guide</title>
      <publish_date>2020-12-01</publish_date>
      <revision_date>2018-11-02</revision_date>
      <revision_date>2022-12-02</revision_date>
      <revision_date>2023-12-02</revision_date>
   </book>
</catalog>

下面是XSLT代码段:

<xsl:for-each select="/catalog/book">
    <xsl:for-each select="revision_date">
        <xsl:if test="xs:date(revision_date) &gt; xs:date(publish_date)">
            <Error><xsl:text>Revision date should be lesser than publish date.</xsl:text></Error>
        </xsl:if>
    </xsl:for-each>
</xsl:for-each>

解决方法

这是一种解决方案:

<xsl:template match="revision_date[xs:date(.) gt xs:date(../publish_date)]">
  <Error>Revision date should be lesser than publish date.</Error>
</xsl:template>

现在没有<xsl:for-each>,只有<xsl:apply-templates select="revision_date"/>


这是一个完整的转换

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="revision_date[xs:date(.) gt xs:date(../publish_date)]">
    <Error>
    <xsl:value-of select="concat('Revision date ',.,' should be lesser than publish date ',../publish_date)"/>
    </Error>
  </xsl:template>
</xsl:stylesheet>

将其应用于提供的XML文档时

<catalog>
   <book id="bk101">
      <author>Gambardella,Matthew</author>
      <title>XML Developer's Guide</title>
      <publish_date>2020-10-01</publish_date>
      <revision_date>2019-09-02</revision_date>
      <revision_date>2021-12-02</revision_date>
   </book>
   <book id="bk102">
      <author>John,Doe</author>
      <title>XPath Developer's Guide</title>
      <publish_date>2020-12-01</publish_date>
      <revision_date>2018-11-02</revision_date>
      <revision_date>2022-12-02</revision_date>
      <revision_date>2023-12-02</revision_date>
   </book>
</catalog>

产生了所需的正确结果

<catalog>
   <book id="bk101">
      <author>Gambardella,Matthew</author>
      <title>XML Developer's Guide</title>
      <publish_date>2020-10-01</publish_date>
      <revision_date>2019-09-02</revision_date>
      <Error>Revision date 2021-12-02 should be lesser than publish date 2020-10-01</Error>
   </book>
   <book id="bk102">
      <author>John,Doe</author>
      <title>XPath Developer's Guide</title>
      <publish_date>2020-12-01</publish_date>
      <revision_date>2018-11-02</revision_date>
      <Error>Revision date 2022-12-02 should be lesser than publish date 2020-12-01</Error>
      <Error>Revision date 2023-12-02 should be lesser than publish date 2020-12-01</Error>
   </book>
</catalog>
,

在内部for-each里面,我想你想要

 <xsl:if test="xs:date(.) &gt; xs:date(../publish_date)">

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...