質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

XSLT

XSLTは、組み立てられた文書を(XML、HTML、そしてプレーンテキストのような)別のフォーマットに変化する為にデザインされたXMLの為の変換用言語です。

Q&A

解決済

1回答

560閲覧

for-each-groupとresult-documentを使用して、グループ毎にファイルを出力したい

UG_ito_Pcorp

総合スコア13

XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

XSLT

XSLTは、組み立てられた文書を(XML、HTML、そしてプレーンテキストのような)別のフォーマットに変化する為にデザインされたXMLの為の変換用言語です。

0グッド

0クリップ

投稿2023/04/06 05:44

for-each-groupとresult-documentを使用して、グループ毎にファイルを出力したい

変換元ソースをfor-each-groupを使用して複数のグループに分割し、これをグループ毎にファイル名を付けて出力したいのですが、エラーで実現できません。

エラーメッセージ

Cannot write more than one result document to the same URI:

変換元ソースとXSLTは以下のとおりです。

変換元ソース

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE xml> <root> <p class="brand">MARTIN</p> <p class="product">D-18</p> <p class="product">D-28</p> <p class="product">D-35</p> <p class="product">D-45</p> <p class="brand">GIBSON</p> <p class="product">J-15</p> <p class="product">J-45</p> <p class="brand">TAYLOR</p> <p class="product">700 Series</p> <p class="product">800 Series</p> <p class="product">900 Series</p> </root>

エラーが発生するXSLT

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" name="XML-format"/> <xsl:template match="/"> <topic> <title>acoustic guitar</title> <body> <xsl:apply-templates/> </body> </topic> </xsl:template> <xsl:template match="root"> <xsl:for-each-group select="*" group-starting-with="p[@class='brand']"> <xsl:variable name="file_name" select="concat(current(),'.xml')"/> <xsl:result-document href="$file_name" format="XML-format"> <section> <xsl:apply-templates select="current-group()"/> </section> </xsl:result-document> </xsl:for-each-group> </xsl:template> <xsl:template match="p"> <xsl:choose> <xsl:when test="./@class='brand'"> <title> <xsl:value-of select="."/> </title> </xsl:when> <xsl:otherwise> <p><xsl:value-of select="."/></p> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>

出力するファイル名が被っているらしいエラーなので、<xsl:template match="root">の内容を変更して変換したところ、for-each-group毎に意図どおりのファイル名が表示されました。

ファイル名確認のため変更した<xsl:template match="root">

<xsl:template match="root"> <xsl:for-each-group select="*" group-starting-with="p[@class='brand']"> <xsl:variable name="file_name" select="concat(current(),'.xml')"/> <!-- <xsl:result-document href="$file_name" format="XML-format"> --> file_name:<xsl:value-of select="$file_name"/> <section> <xsl:apply-templates select="current-group()"/> </section> <!-- </xsl:result-document> --> </xsl:for-each-group> </xsl:template>

上記による結果

<?xml version="1.0" encoding="UTF-8"?> <topic> <title>acoustic guitar</title> <body> file_name:MARTIN.xml<section> <title>MARTIN</title> <p>D-18</p> <p>D-28</p> <p>D-35</p> <p>D-45</p> </section> file_name:GIBSON.xml<section> <title>GIBSON</title> <p>J-15</p> <p>J-45</p> </section> file_name:TAYLOR.xml<section> <title>TAYLOR</title> <p>700 Series</p> <p>800 Series</p> <p>900 Series</p> </section> </body> </topic>

どこに問題があるかご教授ください。
そもそも、for-each-groupとresult-documentで実行不可な場合、何か他の方法があればアドバイスいただけると助かります。
よろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

xsl:result-document@hrefはattribute value templateなので、href="$file_name"では文字通り$file_nameというURLを指定したことになってしまいます。
意図されているのはhref="{$file_name}"だと思われます。

投稿2023/04/08 15:55

OACleaner

総合スコア63

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

UG_ito_Pcorp

2023/04/11 02:15

ご回答、ありがとうございました。 問題、解決いたしました。 基本的な知識が不足しており、お恥ずかしい限りです。 今後も精進いたします。 また、アドバイスいただければ幸いです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問