在XML文档中创建图像路径列表

时间:2012-07-16 15:55:30

标签: xml xslt xinclude

我有xml文件,其中包含其他xml文件。所有这些xml文件都包含位于不同源位置的图像的相对路径。

<chapter xml:id="chapter1">
    <title>First chapter in Main Document</title>
    <section xml:id="section1">
        <title>Section 1 in Main Document</title>
        <para>this is paragraph<figure>
                <title>Car images</title>
                <mediaobject>
                    <imageobject>
                        <imagedata fileref="images/image1.jpg"/>
                    </imageobject>
                </mediaobject>
            </figure></para>
    </section>
    <xi:include href="../doc/section2.xml"/>
    <xi:include href="../doc/section3.xml"/>
</chapter>

这是第2节和第3节xml文档的样子。

<section xml:id="section2"  
        <title>Main Documentation Section2</title>
        <para>This is also paragraph <figure>
                <title>Different Images</title>
                <mediaobject>
                    <imageobject>
                        <imagedata fileref="images/image2.jpg"/>
                    </imageobject>
                </mediaobject>
            </figure></para>
    </section>

我想创建XSLT 1.0样式表,它将生成所有xml文档中的图像路径列表。我打算将位于不同源位置的图像复制到单个图像文件夹中。然后我将能够使用该图像路径列表来复制这些图像。如果将图像路径列表保存在可以通过java类访问的结构中,那就太棒了。

目前我正在使用XSLT,这是我从另一个问题得到的。但是这个XSLT将其他节点的值与图像路径一起提供。我尝试通过更改模板值来过滤它们。

<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="xi:include[@href][@parse='xml' or not(@parse)]">
<xsl:apply-templates select="document(@href)" />
</xsl:template>

预期结果列表可能是一些事情,

/home/vish/test/images/image1.jpg

/home/vish/test/doc/other/images/image2.jpg

/home/vish/test2/other/images/image3.jpg

提前致谢.. !!

1 个答案:

答案 0 :(得分:2)

怎么样......

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xi="http://www.w3.org/2001/XInclude"
      exclude-result-prefixes="xsl xi">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*" />

<xsl:template match="/">
 <image-paths>
  <xsl:apply-templates select="*" />
 </image-paths>
</xsl:template>

<xsl:template match="*">
 <xsl:apply-templates select="*" />
</xsl:template>

<xsl:template match="imagedata">
 <imagedata fileref="{@fileref}" />
</xsl:template>

<xsl:template match="xi:include[@href][@parse='xml' or not(@parse)]">
<xsl:apply-templates select="document(@href)" />
</xsl:template>

</xsl:stylesheet>

您应该获得类似......

的输出
<image-paths>
 <imagedata fileref="path1/image1.jpg" />
 <imagedata fileref="path2/image2.jpg" />
 <imagedata fileref="path3/image3.jpg" />
</image-paths>
相关问题