1

私は以下のようなxml文書を持っています、

<chapter xmlns:xi="http://www.w3.org/2001/XInclude" xml:id="chapter1">
<title>First chapter</title>
<section xml:id="section1">
                <imageobject>
                    <imagedata fileref="images/image1.jpg"/>
                </imageobject>
                <imageobject>
                    <imagedata fileref="images/image5.jpg"/>
                </imageobject>
</section>
    <chapter xmlns:xi="http://www.w3.org/2001/XInclude" xml:id="chapter2"   xml:base="../foder1/section2.xml">        
   <section xml:id="section2">
                    <imageobject>
                        <imagedata fileref="images/image2.jpg"/>
                    </imageobject>
                    <imageobject>
                        <imagedata fileref="images/image3.jpg"/>
                    </imageobject>
    </section>
    </chapter>
    <chapter xmlns:xi="http://www.w3.org/2001/XInclude" xml:id="chapter3" xml:base="../folder3/section3.xml">  
   <section xml:id="section3">
                    <imageobject>
                        <imagedata fileref="images/image4.jpg"/>
                    </imageobject>
    </section>
   </chapter>
 </chapter>

特定のxmlドキュメントから画像パスリストのリストを取得するために、別の回答から取得した次のXSLTを使用しています。ただし、First Chapter section1 の画像パスは出力されません。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="/*">
<Imagedata>
<xsl:apply-templates select="chapter"/>
</Imagedata>
</xsl:template>

<xsl:template match="*/chapter">
<chapter>
<basepath><xsl:value-of select="@xml:base"/></basepath>
<xsl:apply-templates/>
</chapter>
</xsl:template>

<xsl:template match="imagedata">
<relativepath><xsl:value-of select="@fileref"/></relativepath>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>

以下の構造のような画像パス出力のリストが欲しいです。それを使用すると、セクション 1 のすべての画像に「mainrelativepath」でアクセスでき、セクション 2、セクション 3 の画像に basepath ノードと relativepath ノードでアクセスできます。別のノードでも最初の章の画像パスを取得するのを手伝ってください。

<Imagedata>
    <mainrelativepath>images/image1.jpg</mainrelativepath>
    <mainrelativepath>images/image5.jpg</mainrelativepath>
<chapter>
    <basepath>../foder1/section2.xml</basepath>
    <relativepath>images/image2.jpg</relativepath>
    <relativepath>images/image3.jpg</relativepath>
</chapter>
<chapter>
    <basepath>../foder3/section3.xml</basepath>
    <relativepath>images/image4.jpg</relativepath>
</chapter>

前もって感謝します..!!

4

1 に答える 1

1

この変換:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="/chapter">
        <Imagedata>
          <xsl:apply-templates select="section/*/imagedata">
            <xsl:with-param name="pElemName" select="'mainrelativepath'"/>
          </xsl:apply-templates>
            <xsl:apply-templates select="chapter"/>
        </Imagedata>
    </xsl:template>

    <xsl:template match="*/chapter">
        <chapter>
            <basepath>
                <xsl:value-of select="@xml:base"/>
            </basepath>
            <xsl:apply-templates/>
        </chapter>
    </xsl:template>

    <xsl:template match="imagedata">
    <xsl:param name="pElemName" select="'relativepath'"/>
        <xsl:element name="{$pElemName}">
            <xsl:value-of select="@fileref"/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="text()"/>
</xsl:stylesheet>

提供された XML ドキュメントに適用した場合:

<chapter xmlns:xi="http://www.w3.org/2001/XInclude" xml:id="chapter1">
    <title>First chapter</title>
    <section xml:id="section1">
        <imageobject>
            <imagedata fileref="images/image1.jpg"/>
        </imageobject>
        <imageobject>
            <imagedata fileref="images/image5.jpg"/>
        </imageobject>
    </section>
    <chapter xmlns:xi="http://www.w3.org/2001/XInclude" xml:id="chapter2"   xml:base="../foder1/section2.xml">
        <section xml:id="section2">
            <imageobject>
                <imagedata fileref="images/image2.jpg"/>
            </imageobject>
            <imageobject>
                <imagedata fileref="images/image3.jpg"/>
            </imageobject>
        </section>
    </chapter>
    <chapter xmlns:xi="http://www.w3.org/2001/XInclude" xml:id="chapter3" xml:base="../folder3/section3.xml">
        <section xml:id="section3">
            <imageobject>
                <imagedata fileref="images/image4.jpg"/>
            </imageobject>
        </section>
    </chapter>
</chapter>

必要な正しい結果が生成されます。

<Imagedata>
   <mainrelativepath>images/image1.jpg</mainrelativepath>
   <mainrelativepath>images/image5.jpg</mainrelativepath>
   <chapter>
      <basepath>../foder1/section2.xml</basepath>
      <relativepath>images/image2.jpg</relativepath>
      <relativepath>images/image3.jpg</relativepath>
   </chapter>
   <chapter>
      <basepath>../folder3/section3.xml</basepath>
      <relativepath>images/image4.jpg</relativepath>
   </chapter>
</Imagedata>
于 2012-07-22T15:54:25.553 に答える