0

Wikipedia APIを使用して、英語版にない ''SQLTemplate:Infobox Scientifique'' を含むフランス語のページを見つけたいと考えています。したがって、私の考えは、次のドキュメントを xproc で処理することでした。

http://fr.wikipedia.org/w/api.php?action=query&format=xml&list=embeddedin&eititle=Template:Infobox%20Scientifique&eilimit=400

および次の xslt スタイルシート:

<?xml version='1.0' ?>
<xsl:stylesheet
    xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
    version='1.0'
    >
<xsl:output method='text' indent="yes"/> 
<xsl:template match="/">
<xsl:apply-templates select="api"/>
</xsl:template>

<xsl:template match="api">
<xsl:for-each select="query/embeddedin/ei">
<xsl:variable name="title" select="translate(@title,&apos; &apos;,&apos;_&apos;)"/>
<xsl:variable name="english-title">
<xsl:call-template name="englishTitle"><xsl:with-param name="title" select="@title"/></xsl:call-template>
</xsl:variable>

<xsl:value-of select="$english-title"/><xsl:text>
</xsl:text>

</xsl:for-each>
</xsl:template>

<xsl:template name="englishTitle">
<xsl:param name="title"/>
<xsl:variable name="uri1" select="concat(&apos;http://fr.wikipedia.org/w/api.php?action=query&amp;format=xml&amp;prop=langlinks&amp;lllimit=500&amp;titles=&apos;,translate($title,&apos; &apos;,&apos;_&apos;))"/>
<xsl:message><xsl:value-of select="$uri1"/></xsl:message>
<xsl:message>count=<xsl:value-of select="count(document($uri1,/api/query/pages/page/langlinks/ll))"/></xsl:message>
</xsl:template>

</xsl:stylesheet>

XSLT はテンプレートを含むすべての記事を抽出し、記事ごとにウィキペディアを呼び出してウィキ間のリンクを取得します。ここで、テンプレートenglishTitleは xpath 関数を呼び出しますdocument()

count(ll)=1しかし、ノードがたくさんあるのに対して、それは常に言っています。(例: http://fr.wikipedia.org/w/api.php?action=query&format=xml&prop=langlinks&lllimit=500&titles=Carl_Sagan )。

document()関数によって返されたノードを処理できませんか?

4

1 に答える 1

1

試してみてください:

<xsl:value-of select="count(document($uri1)/api/query/pages/page/langlinks/ll)"/>

別のメモで - とは

translate(@title,&apos; &apos;,&apos;_&apos;)

意味するはず?どうしたの:

translate(@title, ' ', '_')

属性値を区切るタイプの引用符を使用する場合を除き、XML 属性で一重引用符をエンコードする必要はありません。これらはすべて有効です。

name="foo&quot;'foo"
name='foo&apos;"foo'

全体の変換は、次のように縮小できます。

<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <xsl:output method="text" /> 

  <xsl:param name="baseUrl" select="'http://fr.wikipedia.org/w/api.php?action=query&amp;format=xml&amp;prop=langlinks&amp;lllimit=500&amp;titles='" />

  <xsl:template match="ei">
    <xsl:variable name="uri" select="concat($baseUrl ,translate(@title,' ','_'))"/>
    <xsl:variable name="doc" select="document($uri)"/>

    <xsl:value-of select="$uri"/>
    <xsl:text>&#10;</xsl:text>

    <xsl:text>count=</xsl:text>
    <xsl:value-of select="count($doc/api/query/pages/page/langlinks/ll)"/>
    <xsl:text>&#10;</xsl:text>
  </xsl:template>

  <xsl:template match="text()" />  
</xsl:stylesheet>

XSLT の既定のテンプレートをそのまま使用します。すべての再帰がバックグラウンドで実行されます。必要なのは、処理するノードをキャッチすることだけです (既定のtext()テンプレートを空のテンプレートでオーバーライドすることで、不要なテキストの出力を防ぎます)。

于 2009-07-14T18:07:40.230 に答える