プロが教える店舗&オフィスのセキュリティ対策術

XML/XSLについて質問です。
XMLのデータをmsxlsプログラムを使ってXSLフォーマットに合わせてCSVファイルに変更しようと思っています。

XMLファイルは以下の通りです。

<?xml version="1.0" encoding="UTF-8"?>
<Items>
<Item Type="Car" CommandType="Add">
<No>1</No>
<Title>WAGON R </Title>
<Car>
<option1>6</option1>
<option2>1</option2>
</Car>
<Year>2012</Year>
<Images>
<Image>i2.jpg</Image>
<Image>i3.jpg</Image>
<Image>i4.jpg</Image>
</Images>
</Item>
<Item Type="Car" CommandType="Add">
<No>2</ReferenceNo>
<Title>WAGON R </Title>
<Car>
<option1>4</option1>
<option2>5</option2>
</Car>
<Year>2008</Year>
<Images>
<Image>i5.jpg</Image>
<Image>i6.jpg</Image>
</Images>
</Item>

<No>, <Title>などグループ化していないものや、 <Car>の <option1>や <option2>は、フィールド名が違うのでCSVファイルに変更できるのですが、<Images>のようにグループ化してあり、<Image>のようにフィールド名が全て同じの場合、CSVファイルにしたときに横に並べることが出来ません。
これを以下のCSVに出来るXLSコマンドをお教え頂けますでしょうか。
当方まったくの素人ですので、ご理解ください。

No,Title,option1,option2,Year,Image,Image,Image,

宜しくお願いします。

A 回答 (1件)

<?xml version="1.0" encoding="UTF-8"?>


<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="xsl">

<xsl:output
method="text"
media-type="text/comma-separated-values"
/>

<xsl:template match="/">
<xsl:apply-templates select="Items/Item"/>
</xsl:template>

<xsl:template match="Item">
<xsl:value-of select="No"/>
<xsl:text>,</xsl:text><xsl:value-of select="Title"/>
<xsl:text>,</xsl:text><xsl:value-of select="Car/option1"/>
<xsl:text>,</xsl:text><xsl:value-of select="Car/option2"/>
<xsl:text>,</xsl:text><xsl:value-of select="Year"/>
<xsl:apply-templates select="Images/Image"/>
<xsl:text>
</xsl:text>
</xsl:template>

<xsl:template match="Image">
<xsl:text>,</xsl:text><xsl:value-of select="."/>
</xsl:template>

</xsl:stylesheet>
    • good
    • 0

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