我正在使用如下的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文档获取图像路径列表列表。但它没有输出第一章第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>
我想要一个像下面的结构输出的图像路径列表。有了它,我可以通过“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="/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>