使用XSLT1.0在xml文件的xml:base属性和其他节点值之间进行映射



我有一个如下的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:basevalue和该章中的相对图像路径结合起来。然后我可以得到每个章节中图像的所有绝对路径。为此,我使用了以下XSLT1.o文件。

<xsl:template match="/">
<imagepaths>
<xsl:for-each select="chapter/chapter">
 <basepath>
 <xsl:value-of select="@xml:base"/>
 </basepath>
</xsl:for-each>
 <xsl:apply-templates select="*" /> 
</image-paths>
</xsl:template>
  <xsl:template match="*">
  <xsl:apply-templates select="*" />
   </xsl:template>
   <xsl:template match="imagedata">
  <relativepath>
  <xsl:value-of select="@fileref" />
  </realtivepath>
  </xsl:template>
  </xsl:stylesheet>

但这分别给出了所有xml:基值和相对路径。它不提供每个章节的xml:basevalue和该章节中的相对路径之间的任何映射。我希望在该章中有一个xml:base-value和所有相关路径之间的映射。这个映射应该怎么做?我认为通过输出如下,我可以进行映射并获得图像的绝对路径。请帮助我使用XSLT获得以下输出。有了它,我可以通过"mainrelativepath"访问第1节中的所有图像,通过basepath和relativePth节点访问第2节、第3节中的图像。

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

提前感谢。。!!

此转换

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

应用于所提供的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>
    </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>
   <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>

最新更新