我有一个要求,其中我必须验证两种情况:报价开始日期应在报价结束日期之前,报价开始日期账户应在账户开始日期之后。如果不满足任何场景,则应抛出错误。
报价开始日期和报价结束日期值将分别以空格分隔的格式显示在xml标记和xml标记中。
下面是示例xml代码:
<Accounts>
<Account>
<AccountStartDate>2020-12-01<AccountStartDate>
<offerStartDate>2020-10-02 2020-11-02</offerStartDate>
<offerEndDate>2019-10-02 2019-11-02</offerEndDate>
</Account>
</Accounts>
下面是xslt代码示例:
<xsl:for-each select="Accounts/Account">
<xsl:variable name="offerSDate" select="offerStartDate"/>
<xsl:variable name="offerEDate" select="offerEndDate"/>
<xsl:if test="$offerSDate > xs:date(AccountStartDate)">
<Error>
<xsl:text>Error: Invalid offer Date 
</xsl:text>
</Error>
</xsl:if>
<xsl:if test="$offerSDate > $offerEDate">
<Error>
<xsl:text>Error: Invalid offer Date 
</xsl:text>
</Error>
</xsl:if>
</xsl:for-each>
在执行xslt代码之后,我得到了无效日期";2020-10-02 2020-11-02"问题
如果您想对offerStartDate
中的每个日期进行单独的比较,那么您可以(在XSLT2.0中(执行以下操作之一:
<xsl:for-each select="Account">
<xsl:if test="some $offerStartDate in tokenize(offerStartDate, ' ') satisfies xs:date($offerStartDate) gt xs:date(AccountStartDate)">
<Error>error message</Error>
</xsl:if>
</xsl:for-each>
或者(取决于你的测试应该有什么意义(:
<xsl:for-each select="Account">
<xsl:if test="every $offerStartDate in tokenize(offerStartDate, ' ') satisfies xs:date($offerStartDate) gt xs:date(AccountStartDate)">
<Error>error message</Error>
</xsl:if>
</xsl:for-each>
只使用XSLT可能最简单的方法是从转换XML
<Accounts>
<Account>
<AccountStartDate>2020-12-01</AccountStartDate>
<offerStartDate>2020-10-02 2020-11-02</offerStartDate>
<offerEndDate>2019-10-02 2019-11-02</offerEndDate>
</Account>
</Accounts>
类似于:
<Accounts>
<Account>
<AccountStartDate>2020-12-01</AccountStartDate>
<offer>
<offerStartDate>2020-10-02</offerStartDate>
<offerEndDate>2019-10-02</offerEndDate>
</offer>
<offer>
<offerStartDate>2020-11-02</offerStartDate>
<offerEndDate>2019-11-02</offerEndDate>
</offer>
</Account>
</Accounts>