Search code examples
xmlxsltxinclude

Get list of Image paths from given XML document using XSLT 1.0


I am having a xml document like below,

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

I am using following XSLT which I got from another answer to get list of image paths list from given xml document. But it does not output First Chapter section1's image paths.

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

I want a list of image paths output like below structure. With it I can access all images in section1 by "mainrelativepath" and section2, section3 images by basepath and relativepath nodes. Please help me to get First chapter's image paths also with a different node.

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

Thanks in advance..!!


Solution

  • This transformation:

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

    when applied on the provided XML document:

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

    produces the wanted, correct result:

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