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處理器。
在此先感謝..!
Woow .. !! = D這非常好。非常感謝你Martin ..你真棒.. – vish 2012-08-06 11:08:20
Martin,我看不到'ext:node-set()'在這裏被使用 - 也許你需要編輯回答並刪除提及EXSLT - 還有代碼中的命名空間聲明? – 2012-08-06 12:13:40
Dimitre,我不明白你在評論中提出的問題。在樣式表示例中,你看不見 '我可以理解使用'exsl:node-set'是否有必要解決這個問題的想法,但是現在我想XSLT 2.0太多了,就像使用和訪問XPath臨時樹那樣如果可用,我更喜歡在XSLT 1.0中使用'exsl:node-set'。 –
2012-08-06 13:53:49