2012-08-06 94 views
2

我有一個主XML文檔這樣的:替換XML文件的屬性值1.0

<chapter xmlns:xi="http://www.w3.org/2001/XInclude" xml:id="chapter1"> 
<title>First chapter</title> 
     <section xml:id="section1"> 
       <imageobject> 
        <id>aa12</id> 
        <image fileref="image1.jpg"/> 
       </imageobject> 
       <imageobject> 
        <id>bb13</id> 
        <image fileref="image2.jpg"/> 
       </imageobject> 
     </section> 
     <section xml:id="section2" xml:base="../other/section1.xml"> 
        <imageobject> 
         <id>ab14</id> 
         <image fileref="image1.jpg"/> 
        </imageobject> 
        <imageobject> 
         <id>ab15</id> 
         <image fileref="image2.jpg"/> 
        </imageobject> 

      <section xml:id="section3" xml:base="../some-other/more/section3.xml"> 
        <imageobject> 
         <id>ac16</id> 
         <image fileref="image1.jpg"/> 
        </imageobject> 
      </section> 
    </section> 
    <section xml:id="section4" xml:base="../some-other/section4.xml"> 
        <imageobject> 
         <id>ac17</id> 
         <image fileref="image2.jpg"/> 
        </imageobject> 
    </section> 
</chapter> 

和另一個XML文件和值等:

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
    <NewData> 
     <Rename id="ab14" imageName="aaaa.jpg"/> 
     <Rename id="ab15" imageName="bbbb.jpg"/> 
     <Rename id="ac16" imageName="cccc.jpg"/> 
     <Rename id="ac17" imageName="dddd.jpg"/> 
    </NewData> 

而最後我需要一個類似下面的輸出,根據id值將其替換爲正確的重命名圖像名稱。

<chapter xmlns:xi="http://www.w3.org/2001/XInclude" xml:id="chapter1"> 
    <title>First chapter</title> 
    <section xml:id="section1"> 
        <imageobject> 
         <image fileref="image1.jpg"/> 
        </imageobject> 
        <imageobject> 
         <image fileref="image2.jpg"/> 
        </imageobject> 
    </section> 
    <section xml:id="section2" xml:base="../other/section1.xml"> 
         <imageobject> 
          <image fileref="aaaa.jpg"/> 
         </imageobject> 
         <imageobject> 
          <image fileref="bbbb.jpg"/> 
         </imageobject> 

      <section xml:id="section3" xml:base="../some-other/more/section3.xml"> 
         <imageobject> 
          <image fileref="cccc.jpg"/> 
         </imageobject> 
      </section> 
    </section> 
    <section xml:id="section4" xml:base="../some-other/section4.xml"> 
         <imageobject> 
          <image fileref="dddd.jpg"/> 
         </imageobject> 
    </section> 
</chapter> 

這裏發生的是:如果第一個XML文檔中的id屬性從第二XML的id屬性相匹配,那麼fileref第一文檔中的值由匹配imageName從第二XML文件替換。

請看我在這裏給出的例子。

我該如何使用XSLT 1.0來做到這一點?

我正在使用Saxon或Xsltproc處理器。

在此先感謝..!

回答

3

下面是一個XSLT 1.0樣式表(使用exsl:其由兩個xsltproc的支持以及撒克遜6節點集):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:exsl="http://exslt.org/common" 
    exclude-result-prefixes="exsl"> 

<xsl:param name="doc2-url" select="'test2012080602.xml'"/> 
<xsl:variable name="doc2" select="document($doc2-url)"/> 

<xsl:strip-space elements="*"/> 
<xsl:output indent="yes"/> 

<xsl:key name="img-by-id" match="Rename" use="@id"/> 

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

<xsl:template match="imageobject"> 
    <xsl:copy> 
    <xsl:variable name="id" select="id"/> 
    <xsl:variable name="ref"> 
     <xsl:for-each select="$doc2"> 
     <xsl:if test="key('img-by-id', $id)"> 
      <image filref="{key('img-by-id', $id)/@imageName}"/> 
     </xsl:if> 
     </xsl:for-each> 
    </xsl:variable> 
    <xsl:variable name="new" select="exsl:node-set($ref)/image"/> 
    <xsl:choose> 
     <xsl:when test="$new"> 
     <xsl:copy-of select="$new"/> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:copy-of select="image"/> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 

可以將參數doc2-url設置爲二次輸入的位置具有新名稱的文件。

+0

Woow .. !! = D這非常好。非常感謝你Martin ..你真棒.. – vish 2012-08-06 11:08:20

+0

Martin,我看不到'ext:node-set()'在這裏被使用 - 也許你需要編輯回答並刪除提及EXSLT - 還有代碼中的命名空間聲明? – 2012-08-06 12:13:40

+0

Dimitre,我不明白你在評論中提出的問題。在樣式表示例中,你看不見'我可以理解使用'exsl:node-set'是否有必要解決這個問題的想法,但是現在我想XSLT 2.0太多了,就像使用和訪問XPath臨時樹那樣如果可用,我更喜歡在XSLT 1.0中使用'exsl:node-set'。 – 2012-08-06 13:53:49