1

これは、インポートする必要のあるファイルの構造です。

<channel>
<item>
  <type>image</type>
  <title>title image</title>
  <id>1</id>
  <image_url>url_to_image</image_url>
</item>

<item>
  <type>page</type>
  <title>node title</title>
  <id>2</id>
  <ref>
    <entity>image_ref</entity>
    <ref_value>1</ref_value>
  </ref>
  <ref>
    <entity>category</entity>
    <ref_value>5</ref_value>
  </ref>
  </item>
</channel>

ページアイテムでは、タグに画像アイテムのIDが含まれています。画像アイテムからページアイテムに画像のURLを追加するにはどうすればよいですか?使おうとしています

/channel/item[id=ref/ref_value[../entity/text() = 'image_ref']]/image_urlしかし、それは機能しません...

画像アイテムではなくページアイテムのみをインポートするXPath式とは何ですか?

前もって感謝します

4

1 に答える 1

1

使用:

  /*/item[type='image' and id=../item[type='page']
           /ref[entity = 'image_ref']/ref_value]
                     /image_url/text()

XSLT ベースの検証:

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

 <xsl:template match="/">
     <xsl:copy-of select=
     "/*/item[type='image' and id=../item[type='page']
               /ref[entity = 'image_ref']/ref_value]
                         /image_url/text()"/>
 </xsl:template>
</xsl:stylesheet>

この変換が提供された XML ドキュメントに適用されると、次のようになります。

<channel>
    <item>
        <type>image</type>
        <title>title image</title>
        <id>1</id>
        <image_url>url_to_image</image_url>
    </item>
    <item>
        <type>page</type>
        <title>node title</title>
        <id>2</id>
        <ref>
            <entity>image_ref</entity>
            <ref_value>1</ref_value>
        </ref>
        <ref>
            <entity>category</entity>
            <ref_value>5</ref_value>
        </ref>
    </item>
</channel>

XPath 式が評価され、この評価の結果が出力にコピーされます。

url_to_image

更新

OPはコメントで、多くの「ページアイテム」と「画像アイテム」が存在する可能性があり、特定のページのみの画像URLを取得する式が必要であることを暗示しています.

この XPath 式:

 /*/item[type='image'
        and id=../item[type='page'][1]
                             /ref[entity = 'image_ref']/ref_value
               ]
                /image_url/text()"/>

次の XML ドキュメントの最初の「ページ アイテム」に必要な画像の URL を生成します。

<channel>
    <item>
        <type>image</type>
        <title>title image</title>
        <id>1</id>
        <image_url>url_to_image</image_url>
    </item>
    <item>
        <type>image</type>
        <title>title image</title>
        <id>2</id>
        <image_url>url2_to_image</image_url>
    </item>
    <item>
        <type>page</type>
        <title>node title</title>
        <id>3</id>
        <ref>
            <entity>image_ref</entity>
            <ref_value>1</ref_value>
        </ref>
        <ref>
            <entity>category</entity>
            <ref_value>5</ref_value>
        </ref>
    </item>
    <item>
        <type>page</type>
        <title>node title</title>
        <id>4</id>
        <ref>
            <entity>image_ref</entity>
            <ref_value>2</ref_value>
        </ref>
        <ref>
            <entity>category</entity>
            <ref_value>5</ref_value>
        </ref>
    </item>
</channel>

生成される結果は次のとおりです。

url_to_image

2 番目のページ アイテムに必要な URL を取得するには、上記の XPath 式を次のように変更するだけです。

 /*/item[type='image'
        and id=../item[type='page'][2]
                             /ref[entity = 'image_ref']/ref_value
               ]
                /image_url/text()"/>

そして今、結果は次のとおりです。

url2_to_image
于 2013-01-10T12:49:47.517 に答える