12

この XML の断片を考えると

<dc:date>info:eu-repo/date/embargoEnd/2013-06-12</dc:date>
<dc:date>2012-07-04</dc:date>

で始まらない 文字列の年のみを出力するには、XSL が必要info:eu-repoです。私はこのようにしようとしていますが、うまくいきません。私は間違っていfor-eachますか?

<xsl:if test="not(starts-with('dc:date', 'info:eu-repo'))">
                <xsl:for-each select="dc:date">
                    <publicationYear>
                        <xsl:variable name="date" select="."/>
                        <xsl:value-of select="substring($date, 0, 5)"/>
                    </publicationYear>
                </xsl:for-each>
</xsl:if>
4

3 に答える 3

23

クエリには必要ないと思います。'日付start-withを少し異なる方法で反復処理することもできます。

    <xsl:for-each select="dc:date">
         <xsl:variable name="date" select="."/>
         <xsl:if test="not(starts-with($date, 'info:eu-repo'))">
                <publicationYear>
                    <xsl:value-of select="substring($date, 0, 5)"/>
                </publicationYear>
        </xsl:if>
    </xsl:for-each>
于 2012-09-20T08:18:52.990 に答える
2

使用(提供された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>
于 2012-09-20T11:30:00.023 に答える
2

a を使用して、必要なものを取得することもできtemplate-matchます。たとえば、次のようになります。

<xsl:template match="date[not(starts-with(.,'info:eu-repo'))]">
    <xsl:value-of select="."/>
</xsl:template>

この入力 XML があります。

<?xml version="1.0" encoding="UTF-8"?>
<list>
<date>info:eu-repo/date/embargoEnd/2013-06-12</date>
<date>2012-07-04</date>
</list>

この XSLT をそれに適用します。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="text() | @*"/>

<xsl:template match="/">
    <xsl:apply-templates />
</xsl:template> 

<xsl:template match="date[not(starts-with(.,'info:eu-repo'))]">
    <xsl:value-of select="."/>
</xsl:template>

</xsl:stylesheet>

そして、私はこの出力を取得します:

<?xml version="1.0" encoding="UTF-8"?>2012-07-04
于 2012-09-20T11:14:26.430 に答える