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>

我正在使用从另一个答案中获得的 XSLT,以从给定的 xml 文档中获取图像路径列表。但它不输出第一章 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>

我想要一个如下结构的图像路径输出列表。有了它,我可以通过“mainrelativepath”访问section1中的所有图像,通过basepath和relativepath节点访问section2、section3图像。请帮助我使用不同的节点获取第一章的图像路径。

<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 回答