2012-07-21 39 views
1

我有像下方的XML文檔,做映射:base屬性和一個XML文件的其他節點值使用XSLT 1.0

<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:基準值和相對路徑。它不提供每章的xml:base值和該章節中的相對路徑之間的映射。我想要在該章節中的xml:base值和所有相對路徑之間進行映射。我如何做這個映射?我認爲通過像下面這樣的輸出,我可以進行映射並獲得圖像的絕對路徑。請幫助我使用我的XSLT獲得以下輸出。有了它,我可以通過「mainrelativepath」和section2訪問section1中的所有圖像,通過basepath和relativepath節點訪問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> 

在此先感謝.. !!

回答

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> 
+0

嗨Dimitre .. 非常感謝您的快速回復。這是我正在尋找的。但是你可以注意到在xincluding xml文件中有一個像「images/image1.jpg」的相對路徑。用你的XSLT,看起來我們錯過了這條路。如果你可以請修改你的答案,以添加圖像路徑也.. ..我不能提前它。我已經修改了新的輸出..感謝提前.. – vish 2012-07-21 18:54:49

+0

@vish,我認爲你犯了一個結構性錯誤 - 第一章包括所有其他章節。如果真的如此,那麼*所有*圖像應該屬於第一章 - 但是這並不反映在所提供的期望輸出中。因此,如果還應該生成最外面的章節的圖像,那麼這個問題就有一個顯着的差距,而不是解釋本章所考慮的內容,什麼不是。請編輯問題並提供一個不衝突的解釋和/或修改想要的輸出。 – 2012-07-21 20:15:05

+0

對不起,遲到了。我將更改爲並生成了我所需的輸出。你太棒了..!!非常感謝你.. =) – vish 2012-07-22 10:50:12

相關問題