Xpath 内容节点文本Schematron测试(到某个位置)

Xpath 内容节点文本Schematron测试(到某个位置),xpath,schematron,Xpath,Schematron,我想编写一个Schematron规则,测试其中的值是当前年份、去年还是明年 问题在于该值可能包含一个年份范围,例如:2013-2014年。我只想测试(2013)文本节点的前四位数字 这是我写的,但不正确。能否返回文本节点的位置 XML文件: <article> <front> <article-meta> <pub-date pub-type="epub-ppub"> <year>2013

我想编写一个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>

2013-2014
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>

出版年份(出版日期[@pub type='epub-ppub']/年)为“”。应该是今年
(),去年还是明年
一年。

验证XML文件时,将触发规则,但2013年为有效年份:发布年份(发布日期[@pub type='epub-ppub']/年)为“2013-2014”。它应该是当前年份(2013年)、上一年或下一年。

您可以使用子字符串仅返回部分文本内容:

substring(//year/text(), 1, 4)
或者,在获取某个字符串或字符之前的内容之前使用子字符串before:

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

两者都返回示例XML中的
2013

您可以使用子字符串仅返回部分文本内容:

substring(//year/text(), 1, 4)
或者,在获取某个字符串或字符之前的内容之前使用子字符串before:

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

两者都返回示例XML中的
2013

似乎您使用的是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 >= .
   ]
  ]
//年份[
子字符串(,1,4)[
年份自日期时间(当前-dateTime())-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 >= .
   ]
  ]
//年份[
子字符串(,1,4)[
年份自日期时间(当前-dateTime())-1=。
]
]