2012-07-16 23 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>