使用(提供されたXMLフラグメントが現在のノードの子である要素であり、目的のプロパティを持つ要素が1つだけであると想定):
substring-before(*[not(starts-with(., 'info:eu-repo'))], '-')
XSLTベースの検証:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<xsl:copy-of select=
"substring-before(*[not(starts-with(., 'info:eu-repo'))], '-') "/>
</xsl:template>
</xsl:stylesheet>
この変換が次のXMLドキュメントに適用される場合(提供されたフラグメントが単一の最上位要素にラップされ、名前空間が宣言されます):
<t xmlns:dc="some:dc">
<dc:date>info:eu-repo/date/embargoEnd/2013-06-12</dc:date>
<dc:date>2012-07-04</dc:date>
</t>
XPath式は最上位の要素から評価され、この評価の結果が出力にコピーされます。
2012
II。目的のプロパティを持つ複数の要素:
この場合、単一のXPath1.0式で目的のデータを生成することはできません。
このXSLT変換:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*[not(starts-with(., 'info:eu-repo'))]/text()">
<xsl:copy-of select="substring-before(., '-') "/>
==============
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
このXMLドキュメントに適用した場合:
<t xmlns:dc="some:dc">
<dc:date>info:eu-repo/date/embargoEnd/2013-06-12</dc:date>
<dc:date>2012-07-04</dc:date>
<dc:date>info:eu-repo/date/embargoEnd/2013-06-12</dc:date>
<dc:date>2011-07-05</dc:date>
</t>
必要な正しい結果を生成します。
2012
==============
2011
==============
III。XPath2.0ワンライナー
*[not(starts-with(., 'info:eu-repo'))]/substring-before(., '-')
このXPath2.0式が最後のXMLドキュメント(上記に最も近い)の最上位要素から評価されると、必要な年数が生成されます。
2012 2011
XSLT 2.0ベースの検証:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<xsl:sequence select=
"*[not(starts-with(., 'info:eu-repo'))]/substring-before(., '-')"/>
</xsl:template>
</xsl:stylesheet>
この変換が最後のXMLドキュメントに適用されると、XPath式が評価され、この評価の結果が出力にコピーされます。
2012 2011
IV。最も一般的で困難なケース:
それでは、このXMLドキュメントを作成しましょう。
<t xmlns:dc="some:dc">
<dc:date>info:eu-repo/date/embargoEnd/2013-06-12</dc:date>
<dc:date>2012-07-04</dc:date>
<dc:date>info:eu-repo/date/embargoEnd/2013-06-12</dc:date>
<dc:date>2011-07-05</dc:date>
<dc:date>*/date/embargoEnd/2014-06-12</dc:date>
</t>
dc:date
文字列値が「info:eu-repo」で始まらないすべての要素の年の部分を取得する必要があります。dc:date
ただし、前述のソリューションはいずれも、上記の最後の要素では正しく機能しません。
驚くべきことに、必要なデータは1つのXPAth2.0式で生成できます。
for $s in
*[not(starts-with(., 'info:eu-repo'))]/tokenize(.,'/')[last()]
return
substring-before($s, '-')
この式が上記のXMLドキュメントの最上位要素から評価されると、必要な正しい結果が生成されます。
2012 2011 2014
そしてこれはXSLT2.0ベースの検証です:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<xsl:sequence select=
"for $s in
*[not(starts-with(., 'info:eu-repo'))]/tokenize(.,'/')[last()]
return
substring-before($s, '-')
"/>
</xsl:template>
</xsl:stylesheet>