我正在使用如下的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>
与文件一样,每个xincluded文件中的图像都有相对路径。我想获得图像的绝对路径。为此,我将把每章的xml:base值与该章中的相对图像路径结合起来。然后我可以获得每章中图像的所有绝对路径。为此,我使用了以下XSLT 1.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:base值和相对路径。它不提供xml:每章的基值和该章中的相对路径之间的任何映射。我想在xml:base值和该章中的所有相对路径之间建立映射。我这个映射应该如何做?我认为通过输出如下所示,我可以进行映射并获得图像的绝对路径。请帮我用XSLT获取以下输出。有了它,我可以通过“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>
提前致谢.. !!
答案 0 :(得分: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="/*">
<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>