- PR -

XSLTを利用してXMLデータ処理

投稿者投稿内容
未記入
会議室デビュー日: 2007/08/22
投稿数: 5
投稿日時: 2007-08-22 21:11
皆様、はじめまして。XSLTの初心者です。

XSLTを利用してXMLをグループすることに関して皆さんに教えて頂きたいです。
例えば、下記のXMLに対して

<papers>
<paper>
<title>XML情報検索システム</title>
<authors>
<author>
<name>Aさん</name>
<univ>
<name>情報科学研究科</name>
</univ>
</author>
<author>
<name>Bさん</name>
<univ>
<name>情報科学研究科</name>
</univ>
</author>
<author>
<name>Cさん</name>
<univ>
<name>物理科学研究科</name>
</univ>
</author>
</authors>
</paper>
</papers>


下記のXSLTを利用して

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform ">

<xsl:output method="html" encoding="Shift_JIS"/>

<xsl:template match="/">
<html>
<xsl:apply-templates select="papers"/>
</html>
</xsl:template>
<xsl:template match="papers">
<table>
<xsl:apply-templates select="paper/title"/>
<xsl:apply-templates select="paper/authors/author"/>
</table>
</xsl:template>
<xsl:template match="title">
<td>
<xsl:value-of select="."/>
</td>
</xsl:template>
<xsl:template match="author">
<tr>
<xsl:apply-templates select="univ/name"/>
<xsl:apply-templates select="name"/>
</tr>
</xsl:template>
<xsl:template match="name">
<td>
<xsl:value-of select="."/>
</td>
</xsl:template>

次のようなHTMLに変換できるが

*******************************************************
XML情報検索システムとその高速化に関する研究 *
*******************************************************
報科学研究科                * Aさん *
*******************************************************
報科学研究科                * Bさん *
*******************************************************
物理学研究科                * Cさん *
*******************************************************


次のようなHTMLに変換ができないのでどちら様から教えて頂きたいです。

******************************************************
XML情報検索システムとその高速化に関する研究 *
******************************************************
情報科学研究科                * Aさん*
                       * Bさん*
******************************************************
物理科学研究科                * Cさん*
******************************************************


自分は次の二種類のXSLTも利用して試して見たが失敗しました。

第一種類:

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform "
xmlns:fn=" http://www.w3.org/2003/xpath-functions">
<xsl:output method="html" encoding="Shift_JIS"/>

<xsl:template match="/">
<html>
<xsl:apply-templates select="papers"/>
</html>
</xsl:template>
<xsl:template match="papers">
<table>
<xsl:apply-templates select="paper/title"/>
<xsl:apply-templates select="paper/authors/author"/>
</table>
</xsl:template>
<xsl:template match="title">
<td>
<xsl:value-of select="."/>
</td>
</xsl:template>
<xsl:template match="author">
<tr>
<xsl:apply-templates select="univ/name"/>
<xsl:apply-templates select="name"/>
</tr>
</xsl:template>
<xsl:template match="name">
<xsl:for-each select="fn:distinct-values(name)">
<td>
<xsl:value-of select="."/>
</td>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


第二種類:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform ";>
<xsl:output method="html" encoding="Shift_JIS"/>

<xsl:template match="/">
<html>
<xsl:apply-templates select="papers"/>
</html>
</xsl:template>
<xsl:template match="papers">
<table>
<xsl:apply-templates select="paper/title"/>
<xsl:apply-templates select="paper/authors/author"/>
</table>
</xsl:template>
<xsl:template match="title">
<td>
<xsl:value-of select="."/>
</td>
</xsl:template>
<xsl:template match="author">
<tr>
<xsl:apply-templates select="univ/name"/>
<xsl:apply-templates select="name"/>
</tr>
</xsl:template>
<xsl:template match="name">
<xsl:for-each-group select="name" group-by="name">
<td>
<xsl:value-of select="."/>
</td>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>


何卒、宜しくお願い申し上げます。
fireworkss
会議室デビュー日: 2007/02/12
投稿数: 11
投稿日時: 2007-08-22 22:59
希望の出力結果を得たいのであれば、
xmlの構造からしておかしいと思います。
もちろん、そのままのxmlでもできますが、
素直な構造としては、

<paper>
<title>XML情報検索システム</title>
<authors>
<author univ="情報科学研究科">
<name>Aさん</name>
<name>Bさん</name>
</author>
<author univ="物理科学研究科">
<name>Cさん</name>
</author>
</authors>
</paper>

このような感じのがいいでしょう。
情報科学研究科も同じ文言を2度かくこともないし・・・
この構造だったら、希望の出力がえられるのではないですか?
未記入
会議室デビュー日: 2007/08/22
投稿数: 5
投稿日時: 2007-08-22 23:44
fireworkss様

忙しいところ、見て頂いて、さらにコメントを書いてもらったので誠に有難う御座いました!

fireworkss様が話している構造は構造化されたXMLデータからXSLTで取り出すが、、、。
しかし、私が希望するのはフラットな構造を持つXMLデータをクールピングしたいので。

これについてもうちょっと教えて頂きませんでしょうか?
宜しくお願い致します。
MMX
ぬし
会議室デビュー日: 2001/10/26
投稿数: 861
投稿日時: 2007-08-23 10:09
XSLT グループ化
の二語 で検索してみましょう。

2.0 では、どのソフトを使っていますか?
version="2.0" 1999/XSL/Transform で合致しますか

[ メッセージ編集済み 編集者: MMX 編集日時 2007-08-23 10:15 ]
未記入
会議室デビュー日: 2007/08/22
投稿数: 5
投稿日時: 2007-08-23 15:18
MMX様

始めまして。コメント有難う御座います。

先ず、誠に申し訳ありませんが私はXSLT2.0をよく分かりませんが。若しかして2.0は
1.0と違いどんなソフトを使って利用可能ですか?

次の話は良く分かりませんでした。
**************************************
2.0 では、どのソフトを使っていますか?
version="2.0" 1999/XSL/Transform で合致しますか
**************************************

また、XSLT グループ 二語で検索してみた後、前書いた通りのXSLTを作って実行して見たが

名前空間 'http://www.w3.org/2006/xpath-functions' は関数を含んでいません。

というエラが出てました。

これについてもうちょっと教えて頂きますか?宜しくお願い致します。 
かずくん
ぬし
会議室デビュー日: 2003/01/08
投稿数: 759
お住まい・勤務地: 太陽系第三惑星
投稿日時: 2007-08-23 16:06
引用:

未記入さんの書き込み (2007-08-23 15:18) より:

先ず、誠に申し訳ありませんが私はXSLT2.0をよく分かりませんが。若しかして2.0は
1.0と違いどんなソフトを使って利用可能ですか?


逆に聞きます。
XSL変換を行うために、あなたが現在使用しているツールもしくはライブラリは何ですか?
また、そのツールのバージョンはいくつですか?

引用:

名前空間 'http://www.w3.org/2006/xpath-functions' は関数を含んでいません。

というエラが出てました。

これについてもうちょっと教えて頂きますか?宜しくお願い致します。 


XSLT勧告仕様(w3c.org)を流し読みすると、

引用:

3.1 XSLT Namespace
(snip)
[Definition: The standard function namespace http://www.w3.org/2005/xpath-functions is used for functions in the function library defined in [Functions and Operators] and standard functions defined in this specification.]


となってますよ。
MMX
ぬし
会議室デビュー日: 2001/10/26
投稿数: 861
投稿日時: 2007-08-23 16:13
> 先ず、誠に申し訳ありませんが私はXSLT2.0をよく分かりませんが。若しかして2.0は
1.0と違いどんなソフトを使って利用可能ですか?
2.0仕様は読む気が起きないくらい、ページ数が多いです、それくらい違います。
http://www.w3.org/Style/XSL/
2007-05-12: oXygen 8.2 released
oXygen 8.2 supports editing, running, debugging and profiling for XSLT 1 and 2.
市販のxml開発環境ソフトは 2.0 対応になってます。他にもあります。
試用版で試せるでしょう。

ブラウザIEに内蔵の xslt プロセッサは 2.0と書いても 2.0で動作しません
エラーも 握り潰されて、詳しく分からないことがあります。
未記入
会議室デビュー日: 2007/08/22
投稿数: 5
投稿日時: 2007-08-23 19:56
かずくん 様

私はツールなどを使わなくてブラウザIEに内蔵の xslt プロセッサをそのまま使ってます。

この原因で関数利用可能のようなエラーが出て動かないでしょうか?

MMX 様

良いコメント 有難う御座いました。

若し、XSLT2.0変換を行うために、市販のxml開発環境ソフト(暫く手に入れないので)を正しく利用すると私が作ったXSLは希望のような結果が出るんですよね。

スキルアップ/キャリアアップ(JOB@IT)