【问题标题】:Schematron test for text of content node (up to a certain position)Schematron 测试内容节点的文本(直到某个位置)
【发布时间】:2013-07-12 21:24:50
【问题描述】:

我想编写一个 Schematron 规则来测试其中的值是今年、去年还是明年。

问题在于该值可能包含年份范围,例如:2013-2014。我只想测试(2013)文本节点的前四位。

这是我写的,但它不正确。能否返回一个文本节点的位置?

XML 文件:

<article>
<front>
    <article-meta>
        <pub-date pub-type="epub-ppub">
            <year>2013-2014</year>
        </pub-date>
    </article-meta>
</front></article>

Schematron 规则:

<pattern abstract="false" id="year">
    <rule context="/article/front/article-meta">
        <assert
            test="number(pub-date[@pub-type='epub-ppub']/year/text()[position() = 4]) = year-from-date(current-date()) or number(pub-date/year) = (year-from-date(current-date()) + 1) or number(pub-date/year) = (year-from-date(current-date()) - 1) or number(pub-date/year) = (year-from-date(current-date()) - 2)"
            >The publication year (pub-date[@pub-type='epub-ppub']/year) is "<value-of
                select="pub-date[@pub-type='epub-ppub']/year"/>". It should be the current year
                (<value-of select="year-from-date(current-date())"/>), last year, or next
            year.</assert>
    </rule>
</pattern>

验证 XML 文件时,会触发规则,但 2013 年是有效年份:发布年份 (pub-date[@pub-type='epub-ppub']/year) 为“2013-2014”。应该是今年(2013 年)、去年或明年。

【问题讨论】:

    标签: xpath schematron


    【解决方案1】:

    您似乎使用的是 XPath 2.0,因为 XPath 1.0 不支持日期函数。

    您的错误是您无法使用序列访问子字符串,请使用substring(from, length, start)

    我将您的问题简化为在该范围内查找年份元素并添加一些空格以使答案不那么复杂,我想这对您来说很容易扩展。

    //year[
      substring(., 1, 4)[
        year-from-dateTime(current-dateTime()) - 1 <= .
        and year-from-dateTime(current-dateTime()) + 1 >= .
       ]
      ]
    

    【讨论】:

      【解决方案2】:

      您可以使用 substring 仅返回部分文本内容:

      substring(//year/text(), 1, 4)
      

      或者 substring-before 来获取某个字符串或字符之前的内容:

      substring-before(//year/text(), '-')
      

      对于您的示例 XML,两者都返回 2013

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多