どうして名前空間が必要ですか?
WORDからOOXMLを抽出すると、画像などのリンク情報が別ファイル(document.xml.rels)に書き込まれます。XSLTでリンクファイル名を取得する際、このファイルへアクセスしますが、単にアクセスしても情報を取得することがなぜかできません。
変換対象のXML:
<w:body> <w:p> <w:r> <w:drawing> <wp:inline distT="0" distB="0" distL="0" distR="0"> <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"> <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture"> <pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"> <pic:blipFill> <a:blip r:embed="rId7"> <a:extLst/> </a:blip> </pic:blipFill> </pic:pic> </a:graphicData> </a:graphic> </wp:inline> </w:drawing> </w:r> </w:p> </w:body>
リンク情報のファイル(document.xml.rels):
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> <Relationship Id="rId7" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.png"/> </Relationships>
作成したXSLT(抜粋):
<xsl:variable name="Relationships" select="document('_rels/document.xml.rels')"/> <xsl:template match="w:drawing"> <fig> <image> <xsl:attribute name="href"> <xsl:variable name="embed_id" select="descendant::a:blip/@r:embed"/> <xsl:for-each select="$Relationships//Relationship"> <xsl:variable name="RelationshipId" select="@Id"/> <xsl:choose> <xsl:when test="$embed_id = $RelationshipId"> <xsl:value-of select="@Target"/> </xsl:when> </xsl:choose> </xsl:for-each> </xsl:attribute> </image> </fig> </xsl:template>
出力されるXML:
<fig> <image href=""/> </fig>
※<image>のhref属性が空になってしまいます。
そこで(知識不足ゆえ)何の根拠もなく、リンク情報のファイルに名前空間を加えてみたところ、目的の情報を取得することができました。
手を加えたリンク情報のファイル(document.xml.rels):
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"> <r:Relationship Id="rId7" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.png"/> </Relationships>
※名前空間の宣言追加と<Relationship>タグの変更をしました。
変更したXSLT(抜粋):
<xsl:for-each select="$Relationships//r:Relationship">
※selectの内容を「select="$Relationships//Relationship"」から「select="$Relationships//r:Relationship"」に変更しました。
出力されたXML:(目的が達成された出力)
<fig> <image href="media/image1.png"/> </fig>
何が原因で、名前空間なしでは、目的の情報を得ることができないのでしょうか。
リンク情報のファイル(document.xml.rels)に手を加えることなく、目的の出力を得たいのですが、どうすればよいかご教授いただけないでしょうか。
また、そもそもトンチンカンなことをしているようであれば、ご指摘いただけると助かります。
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/06/26 06:38
2020/06/26 07:21