2016-08-12 481 views
1

我需要創建一個新的xml結構,其中只存在一個映像版本,並且該版本應該是「a」,「16x9s」或「4x3s」 - 在這個優先事項。所以如果圖像版本「a」存在,那麼只有該圖像被選擇。如果它沒有,那麼尋找「16x9s」,最後是「4x3s」。 還需要一些其他節點(在下面的想要的結果代碼中大寫),而所有其他節點應該被丟棄。XSLT2:如何匹配基於優先級的單個節點

的XML(摘錄,它可以有很多較長或較短):

<?xml version="1.0" encoding="iso-8859-1"?> 
<io> 
    <publication id="106125"/> 
    <related-images> 
     <image id="1112835"> 
      <caption>null</caption> 
      <placement>FRONTPAGETITLE</placement> 
      <alignment>left</alignment> 
      <photographer></photographer> 
      <credit></credit> 
      <name>Some text</name> 
      <author></author> 
      <copyright></copyright> 
      <path>Image Root</path> 
      <description></description> 
      <alt-text></alt-text> 
      <image-version version-id="2060510" version-type="16x9s"> 
       <url>/archive/01112/Thomas-Hicks-1_111283516x9s.jpg</url> 
       <width>649</width> 
       <height>365</height> 
      </image-version> 
      <image-version version-id="2060512" version-type="a"> 
       <url>/archive/01112/Thomas-Hicks-16_11128353x4s.jpg</url> 
       <width>413</width> 
       <height>550</height> 
      </image-version> 
      <image-version version-id="2060513" version-type="4x3s"> 
       <url>/archive/01112/Thomas-Hicks-16_11128354x3s.jpg</url> 
       <width>550</width> 
       <height>413</height> 
      </image-version> 
     </image> 
     <image id="1111482"> 
      <caption>Some text</caption> 
      <placement>FRONTPAGELEADTEXT</placement> 
      <alignment>left</alignment> 
      <photographer></photographer> 
      <credit></credit> 
      <name>Some text</name> 
      <author></author> 
      <copyright></copyright> 
      <path>Image Root</path> 
      <description></description> 
      <alt-text></alt-text> 
      <image-version version-id="2060099" version-type="4x3s"> 
       <url>/archive/01111/1904_tugofwar_11114824x3s.jpg</url> 
       <width>640</width> 
       <height>363</height> 
      </image-version> 
      <image-version version-id="2060075" version-type="f-tn"> 
       <url>/archive/01111/1904_tugofwar_1111482f-tn.jpg</url> 
       <width>200</width> 
       <height>113</height> 
      </image-version> 
      <image-version version-id="2060094" version-type="y"> 
       <url>/archive/01111/1904_tugofwar_1111482y.jpg</url> 
       <width>484</width> 
       <height>274</height> 
      </image-version> 
     </image> 
     <image id="1111465"> 
      <caption>Some text</caption> 
      <placement>FRONTPAGELEADTEXT</placement> 
      <alignment>left</alignment> 
      <photographer>Getty Images</photographer> 
      <credit></credit> 
      <name>Some text</name> 
      <author></author> 
      <copyright></copyright> 
      <path>Image Root</path> 
      <description></description> 
      <alt-text></alt-text> 
      <image-version version-id="2058398" version-type="a"> 
       <url>/archive/01111/1896_1111465a.jpg</url> 
       <width>1499</width> 
       <height>1103</height> 
      </image-version> 
     </image> 
    </related-images> 
</io> 

通緝結果:

<PTR:image> 
    <PTR:width>WIDTH OF IMAGE</PTR:width> 
    <PTR:height>HEIGHT OF IMAGE</PTR:height> 

    <PTR:src>IMAGE URL</PTR:src> 

    <PTR:title> 
     NAME 
    </PTR:title> 

    <PTR:description> 
     CAPTION 
    </PTR:description> 

    <PTR:author> 
     PHOTOGRAPHER 
    </PTR:author> 
</PTR:image> 

我已經嘗試了一些東西,像xsl:什麼時候,但是我的技術水平還不足以讓它工作。任何幫助將不勝感激。 乾杯。

+0

你的輸入代碼段有三個'image'元素,[R esult只有一個。這是否意味着你只想從所有圖像中提取一個圖像?或者你想爲每個輸入的'image'元素提取符合條件的數據嗎? –

+0

每個圖像節點在輸入xml中可以有多個圖像版本節點。 我想從每個圖像節點只提取一個圖像url,並且該圖像url應該來自image-versjon a或者如果a不存在,那麼16x9s或者如果這兩個都不存在,則4x3s。如果三者都不存在,則不應選擇圖像url。 – Shaidar

+0

我現在看到我的結果代碼有點難以理解。該結果代碼就是我想要的輸入xml中的每個圖像節點。因此,多個節點,每個節點只有一個,其中源爲圖像版本a,16x9s或4x3s。 希望更清楚。 – Shaidar

回答

1

像凱博士建議,創建序列,並在該序列使用第一次出現。

這是一個完整的例子。

XML輸入

<io> 
    <publication id="106125"/> 
    <related-images> 
     <image id="1112835"> 
      <caption>null</caption> 
      <placement>FRONTPAGETITLE</placement> 
      <alignment>left</alignment> 
      <photographer></photographer> 
      <credit></credit> 
      <name>Some text</name> 
      <author></author> 
      <copyright></copyright> 
      <path>Image Root</path> 
      <description></description> 
      <alt-text></alt-text> 
      <image-version version-id="2060510" version-type="16x9s"> 
       <url>/archive/01112/Thomas-Hicks-1_111283516x9s.jpg</url> 
       <width>649</width> 
       <height>365</height> 
      </image-version> 
      <image-version version-id="2060512" version-type="a"> 
       <url>/archive/01112/Thomas-Hicks-16_11128353x4s.jpg</url> 
       <width>413</width> 
       <height>550</height> 
      </image-version> 
      <image-version version-id="2060513" version-type="4x3s"> 
       <url>/archive/01112/Thomas-Hicks-16_11128354x3s.jpg</url> 
       <width>550</width> 
       <height>413</height> 
      </image-version> 
     </image> 
     <image id="1111482"> 
      <caption>Some text</caption> 
      <placement>FRONTPAGELEADTEXT</placement> 
      <alignment>left</alignment> 
      <photographer></photographer> 
      <credit></credit> 
      <name>Some text</name> 
      <author></author> 
      <copyright></copyright> 
      <path>Image Root</path> 
      <description></description> 
      <alt-text></alt-text> 
      <image-version version-id="2060099" version-type="4x3s"> 
       <url>/archive/01111/1904_tugofwar_11114824x3s.jpg</url> 
       <width>640</width> 
       <height>363</height> 
      </image-version> 
      <image-version version-id="2060075" version-type="f-tn"> 
       <url>/archive/01111/1904_tugofwar_1111482f-tn.jpg</url> 
       <width>200</width> 
       <height>113</height> 
      </image-version> 
      <image-version version-id="2060094" version-type="y"> 
       <url>/archive/01111/1904_tugofwar_1111482y.jpg</url> 
       <width>484</width> 
       <height>274</height> 
      </image-version> 
     </image> 
     <image id="1111465"> 
      <caption>Some text</caption> 
      <placement>FRONTPAGELEADTEXT</placement> 
      <alignment>left</alignment> 
      <photographer>Getty Images</photographer> 
      <credit></credit> 
      <name>Some text</name> 
      <author></author> 
      <copyright></copyright> 
      <path>Image Root</path> 
      <description></description> 
      <alt-text></alt-text> 
      <image-version version-id="2058398" version-type="a"> 
       <url>/archive/01111/1896_1111465a.jpg</url> 
       <width>1499</width> 
       <height>1103</height> 
      </image-version> 
     </image> 
    </related-images> 
</io> 

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:PTR="whatever the PTR namespace uri is"> 
    <xsl:output indent="yes"/> 
    <xsl:strip-space elements="*"/> 

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

    <xsl:template match="image"> 
    <xsl:variable name="targetImage" select=" 
     (image-version[@version-type='a'], 
     image-version[@version-type='16x9s'], 
     image-version[@version-type='4x3s'])[1]"/> 
    <PTR:image> 
     <xsl:apply-templates select="@*"/> 
     <PTR:width><xsl:value-of select="$targetImage/width"/></PTR:width> 
     <PTR:height><xsl:value-of select="$targetImage/height"/></PTR:height> 
     <PTR:src><xsl:value-of select="$targetImage/url"/></PTR:src> 
     <PTR:title><xsl:value-of select="name"/></PTR:title> 
     <PTR:description><xsl:value-of select="caption"/></PTR:description> 
     <PTR:author><xsl:value-of select="photographer"/></PTR:author> 
    </PTR:image> 
    </xsl:template> 

</xsl:stylesheet> 

XML輸出

<io> 
    <publication id="106125"/> 
    <related-images> 
     <PTR:image xmlns:PTR="whatever the PTR namespace uri is" id="1112835"> 
     <PTR:width>413</PTR:width> 
     <PTR:height>550</PTR:height> 
     <PTR:src>/archive/01112/Thomas-Hicks-16_11128353x4s.jpg</PTR:src> 
     <PTR:title>Some text</PTR:title> 
     <PTR:description>null</PTR:description> 
     <PTR:author/> 
     </PTR:image> 
     <PTR:image xmlns:PTR="whatever the PTR namespace uri is" id="1111482"> 
     <PTR:width>640</PTR:width> 
     <PTR:height>363</PTR:height> 
     <PTR:src>/archive/01111/1904_tugofwar_11114824x3s.jpg</PTR:src> 
     <PTR:title>Some text</PTR:title> 
     <PTR:description>Some text</PTR:description> 
     <PTR:author/> 
     </PTR:image> 
     <PTR:image xmlns:PTR="whatever the PTR namespace uri is" id="1111465"> 
     <PTR:width>1499</PTR:width> 
     <PTR:height>1103</PTR:height> 
     <PTR:src>/archive/01111/1896_1111465a.jpg</PTR:src> 
     <PTR:title>Some text</PTR:title> 
     <PTR:description>Some text</PTR:description> 
     <PTR:author>Getty Images</PTR:author> 
     </PTR:image> 
    </related-images> 
</io> 
+1

謝謝:)我確實設法自己解決這個問題,但我用它來選擇。你的答案好多了,所以再次感謝你。 – Shaidar

0

因此,如果圖像版本「a」存在,那麼只有該圖像被選擇。如果 它不會然後尋找「16x9s」,最後是「4x3s」。

這是

select="(image[@version-type='a'], image[@version-type='16x9s'], 
      image[@version-type='4x3s'])[1]" 
+0

謝謝:)我接受了Daniel Haley的回答,因爲他提供了一個完整的例子,但是我很感激你向我展示瞭如何解決我的主要問題。 – Shaidar