アプリ版:「スタンプのみでお礼する」機能のリリースについて

xml,xslを勉強しているのですが、xPathの指定が上手く定義できない状態です。ご教授の程お願い致します。

下記がxml,xslです

<!-- アーティスト名とタイトルを明記したxml -->
-- music.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="music.xsl"?>
<music>
<artist>
Ciel
<title>wake</title>
</artist>
<artist>
Flew
<title>BANG</title>
</artist>
<artist>
cobu                 // (1)
<title>calling</title>
</artist>
<artist>
cobukure
<title>dammy</title>
</artist>
</music>

<!-- 条件に一致したアーティスト名、タイトルを表示するxsl -->
-- music.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head><title></title></head>
<body>
<xsl:for-each select = "descendant::artist[contains(text(),'cobu')]"> // (2)
<xsl:value-of select ="text()" />:
<xsl:value-of select ="title" /><br/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

//上記の処理結果
cobu :calling
cobukure :dammy

//希望する処理結果
cobu :calling

・質問内容
XMLのartistのテキストノード=cobuをxsl:for-eachでselectして、
titleのcallingを表示したい

本、サイトなどで調べて、
descendant::artist[contains(text(),'cobu')]のxPathまでは、
導けたのですが、この場合は、cobuを含むので、
artistのテキストノード=cobukureもselectされdummyが表示されてしまいます。
処理結果、
cobu :callingのみselectできるxPathの定義をご教授の程お願い致します

A 回答 (2件)

選ばれた要素の空白を除いた長さが4であるかをチェックしてみてはいかがでしょう



<xsl:for-each select = "descendant::artist[contains(text(),'cobu')]">
  <xsl:if test="string-length( normalize-space( text() ) ) = 4" >
    <xsl:value-of select ="text()" />:
    <xsl:value-of select ="title" /><br/>
  </xsl:if>
</xsl:for-each>
といった具合で ...

この回答への補足

自己解決だと思うのですが、下記のように明記したらデータを
取得することができました。
*Mac FireFox3.5.2でのみの確認となりますが...

<xsl:for-each select = "descendant::artist[normalize-space(text())='cobu']">

他の方法で、定義する方法がありましたら、ご教授願います。

補足日時:2009/08/08 21:28
    • good
    • 0
この回答へのお礼

回答、教授して頂きありがとうございます。

ご指摘の方法で、下記の用に定義したところ、データを取得できました。

<xsl:for-each select = "descendant::artist[contains(text(),'cobu') and (string-length( normalize-space( text() ) ) = string-length('cobu'))]">

上記以外の方法で、xPath指定できないか、考えたいと思います。
ありがとうございました。

お礼日時:2009/08/08 15:08

<xsl:for-each select = "descendant::artist[normalize-space(text())='cobu']">


で IE6でも表示しましたよ
    • good
    • 0
この回答へのお礼

検証して頂きありがとうございます。

normalize-spaceを意識していなかったので、
<xsl:for-each select = "descendant::artist[text()='cobu']">
と定義した場合、データをひっぱってくることができておりませんでした。

また、ひとつ勉強させて頂きました。
ご丁寧な返答ありがとうございました。

お礼日時:2009/08/10 10:41

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!