2013-07-12 46 views
0

我想寫一個Schematron規則,它測試內部值是當前年份,去年還是明年。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 -type ='epub-ppub']/year)是「2013-2014」。應該是當年(2013年),去年或明年。

回答

3

看起來您正在使用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 >= . 
    ] 
    ] 
+0

非常優雅的解決方案!有一個upvote! – tfoo

+0

啊,子串!謝謝! – user2093335

2

您可以使用子只返回文本內容的一部分:

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

或者子串前的某個字符串或字符之前得到的內容:

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

都返回2013在你的例子XML中。

+0

子串有訣竅。謝謝! – user2093335

相關問題