【问题标题】:Get list of Image paths from given XML document using XSLT 1.0使用 XSLT 1.0 从给定 XML 文档中获取图像路径列表
【发布时间】:2012-07-22 15:19:19
【问题描述】:

我有一个如下所示的 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>

提前谢谢..!!

【问题讨论】:

    标签: xml xslt xinclude


    【解决方案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>
    

    【讨论】:

    • 非常感谢..那很好..你很棒..=)
    猜你喜欢
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    相关资源
    最近更新 更多