重要なお知らせ

「教えて! goo」は2025年9月17日(水)をもちまして、サービスを終了いたします。詳細はこちら>

【6/2終了】教えて!gooアプリ版

下記のXMLのフィールドの項目を
xslを使い、msxsl.exe経由で、csvへ変換をしたいのですが、
各field nameを抽出するのがうまくいきません。

copy condition=の箇所は必要ありません。

どなたかご教示のほど、お願いいたします。

■XML
<?xml version="1.0" encoding="UTF-8"?>
<exportData>
<Book id="id">
<field name="keywords"></field>
<field name="listName"></field>
<field name="publisher"></field>
<field name="publishDate"></field>
<field name="illustrators"></field>
<field name="isbn"></field>
<field name="length"></field>
<field name="id"></field>
<field name="series"></field>
<field name="authors"></field>
<field name="title"></field>
<field name="summary"></field>
<field name="format"></field>
<field name="genre"></field>
<field name="coverImage"></field>
<field name="List Price"></field>
<field name="URL"></field>
<field name="Current Value"></field>
<field name="isbn13"></field>
<copy condition="" dateAcquired="" location="Bookshelf" owner="" presentValue="" source="">
</copy>
</Book>


■xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" encoding="UTF-8"/>

<xsl:template match="/">keywords,listName,publisher,publishDate,illustrators,isbn,length,id,series,authors,title,summary,format,genre,coverImage,URL,CurrentValue,isbn13
<xsl:apply-templates select="//Book"/>
</xsl:template>

<xsl:template match="Book">
<xsl:call-template name="keywords"/>,<xsl:call-template name="listName"/>,<xsl:call-template name="publisher"/>,<xsl:call-template name="publishDate"/>,<xsl:call-template name="illustrators"/>,<xsl:call-template name="isbn"/>,<xsl:call-template name="length"/>,<xsl:call-template name="id"/>,<xsl:call-template name="series"/>,<xsl:call-template name="authors"/>,<xsl:call-template name="title"/>,<xsl:call-template name="summary"/>,<xsl:call-template name="format"/>,<xsl:call-template name="genre"/>,<xsl:call-template name="coverImage"/>,<xsl:call-template name="URL"/>,<xsl:call-template name="CurrentValue"/>,<xsl:call-template name="isbn13"/><xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>

よろしくお願いいたします。

A 回答 (2件)

わかってしまえば、なんてことないんだけど、



ポイントは、値が必要な field 要素の一つ上の要素(Book)から、相対パスで field 要素を指定するところで、このとき select の条件に name 属性の条件も含めればいいだけ。


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" encoding="UTF-8"/>

<xsl:template match="/">
  <!-- 省略 -->
  <xsl:apply-templates select="//Book" />
</xsl:template>

<!-- csv のレコードを出力する処理 -->
<xsl:template match="Book">
<xsl:value-of select="field[@name='keywords']"/>,<xsl:value-of select="field[@name='listName']"/>,<xsl:value-of select="field[@name='publisher']"/>,...(省略)..,<xsl:value-of select="field[@name='isbn13']"/><xsl:text>
</xsl:text>
</xsl:template>

</xsl:stylesheet>
    • good
    • 0
この回答へのお礼

dscripty様

お礼が遅くなりすみませんでした。

filedの要素の指定でselect条件に
name属性の条件を含めるのが鍵ですね。

ご教示いただいた方法で、変換がうまくいきました。
ありがとうございました。

お礼日時:2011/11/17 10:41

> 各field nameを抽出するのがうまくいきません。



『各field name に対応する値を抽出する方法がわかりません』 という質問?

それとも、

単に 『要素の属性を XPATH で指定する方法』 がわからないだけ?

この回答への補足

dscripty様

『各field name に対応する値を抽出する方法がわかりません』 という質問になります。
分かりにくくてすみません。
記載したxmlの中には実際の値が入りますが、質問のxmlの中では削除しております。
このxml内の各field nameの値を取り出して、csvに変換できればと思います。

よろしくお願いいたします。

補足日時:2011/11/14 11:20
    • good
    • 0

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