2012-07-16 22 views
0

我有xml文档,其中包含其他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> 

这里是section2和section3的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

/家庭/ vish /测试/ DOC /其它/图片/图像2 .JPG

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

在此先感谢..!

+0

在什么地方 “的/ home/vish /测试” 部分从何而来? – 2012-07-16 16:19:07

+0

我想添加xml:base添加该公共路径和出绝对路径,我将无法使用xml文档中的相对路径将图像复制到输出目录。但我不确定。 – vish 2012-07-16 17:24:56

+0

document()函数应该能够同时解析绝对引用和相对引用。 – 2012-07-17 00:57:19

回答

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>