0

内部の値が今年、昨年、または来年のいずれかであることをテストする Schematron ルールを作成したいと考えています。

問題は、値に 2013 ~ 2014 などの年の範囲が含まれる可能性があることです。(2013) のテキスト ノードの最初の 4 桁のみをテストしたい。

これは私が書いたものですが、正しくありません。テキスト ノードの位置を返すことはできますか?

XML ファイル:

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

スキーマトロンのルール:

<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 年)、昨年、または来年である必要があります。

4

2 に答える 2

3

XPath 1.0 は日付関数をサポートしていないため、XPath 2.0 を使用しているようです。

あなたの間違いは、シーケンスを使用して部分文字列にアクセスできないことですsubstring(from, length, start)

私はあなたの問題をその範囲内の年の要素を見つけることまで取り除き、空白を追加して答えを簡単にしました.これはあなたが簡単に拡張できると思います.

//year[
  substring(., 1, 4)[
    year-from-dateTime(current-dateTime()) - 1 <= .
    and year-from-dateTime(current-dateTime()) + 1 >= .
   ]
  ]
于 2013-07-12T21:58:42.150 に答える
2

substring を使用して、テキスト コンテンツの一部のみを返すことができます。

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

または、特定の文字列または文字の前にコンテンツを取得する substring-before:

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

2013どちらもサンプル XML に返されます。

于 2013-07-12T21:58:38.490 に答える