2014-03-19 374 views
1

我有以下的XML代碼(這是我正在嘗試使用的代碼片段)。For循環For循環和If語句

<products> 
    <product> 
     <productname>Name2</productname> 
     <productsize measurement="ml">250</productsize> 
     <productsize measurement="ml">500</productsize> 
     <productsize measurement="ml">750</productsize> 
     <otherelements></otherelements> 
     <price size="250" packsize="1">1.25</price> 
     <price size="250" packsize="6">7.50</price> 
     <price size="250" packsize="12">7.00</price> 
     <price size="500" packsize="1">1.75</price> 
     <price size="500" packsize="6">10.50</price> 
     <price size="500" packsize="12">19.00</price> 
     <price size="750" packsize="1">2.25</price> 
     <price size="750" packsize="6">13.50</price> 
     <price size="750" packsize="12">25.00</price> 
    </product> 
    <product> 
     <productname>Name1</productname> 
     <productsize measurement="ml">250</productsize> 
     <productsize measurement="ml">750</productsize> 
     <otherelements></otherelements> 
     <price size="250" packsize="1">1.25</price> 
     <price size="250" packsize="6">7.50</price> 
     <price size="250" packsize="12">7.00</price> 
     <price size="750" packsize="1">2.25</price> 
     <price size="750" packsize="6">13.50</price> 
     <price size="750" packsize="12">25.00</price> 
    </product> 
</products> 

我所試圖做的是這樣的

250ml 1=£1.25, 6=£7.50, 12=£7.00 
500ml 1=£1.75, 6=£10.50, 12=£19.00 
750ml 1=£2.25, 6=£13.50, 12=£25.00 

顯示它,但它沒有工作,這裏是我的XSL代碼什麼,我做錯了,我知道它有與內做for循環。

<xsl:for-each select="x:productsize"> 
<p> 
    <xsl:value-of select="."/> 
    <xsl:value-of select="@measurement"/> 
</p> 

    <xsl:for-each select="x:price"> 
    <xsl:if test="productsize = '@size'"> 
     <xsl:value-of select="."/> 
    </xsl:if> 
    </xsl:for-each>         
</xsl:for-each> 
+0

你得到了什麼錯誤?你有一個聲明的命名空間(如果你這樣做,請發佈完整的源代碼和xslt)。在什麼情況下你的每一個? (請發佈完整的xsl) – helderdarocha

+0

似乎在源XML中會有多個產品;如果是這樣,每個產品是否有唯一的標識符? –

+0

是的,所有完成的命名空間,正如** helderdarocha **向我展示的。道歉,但很多是缺少和頁面顯示,但我沒有得到任何數據。我知道這與** freefaller **解釋過的嵌套問題有關。 – Dino

回答

4

首先,您的XML無效......您不能有一個名爲<other elements></other elements>的元素。並且您的結算<product>應該是</product>。但我想這只是寫在那裏的問題。

的主要問題是,<price>不在範圍<productsize> ......所以,當你運行的productsize<xsl:for-each>您正在尋找有效/productsize/price

試試這個...

<xsl:for-each select="product/productsize"> 
    <xsl:variable name="sizevar" select="."/> 
    <p> 
    <xsl:value-of select="."/> 
    <xsl:value-of select="@measurement"/> &#160; 
    <xsl:for-each select="../price[@size=$sizevar]"> 
     <xsl:value-of select="@packsize"/> = 
     <xsl:value-of select="."/>, &#160; 
    </xsl:for-each>         
    </p> 
</xsl:for-each> 

這需要來自<productsize>size屬性的值,並用它找到那些具有相同size屬性的<price>元素

看到這個XMLPlayground for a working demo


按業務方案的意見,下面一行是匹配整個文檔中的所有元素<price> ...

<xsl:for-each select="//price[@size=$sizevar]"> 

所以我把它改成如下,它只會在父級中找到這些元素(即,該<productsize>元素<price>元素的孩子。)

<xsl:for-each select="../price[@size=$sizevar]"> 
+0

啊,謝謝** freefaller **這工作,所有在這裏是的,我確實有一個名稱空間聲明在我聲明'xmlns:x =「your-namespace」version = ....'。我的問題是我正在搜索'/ productsize/price','price'不在'productsize'內。 – Dino

+0

嗨freefaller,我似乎有這個代碼的問題,如果我有多個產品元素,它不起作用[http://www.xmlplayground.com/Jl2XzC]我在這裏創建一個鏈接,你可以看到即將每個產品的所有數據加載到價格中,我懷疑它與'//價格[@ size = $ sizevar]有關'不知道如何保存該鏈接:( – Dino

+0

將其從' //價格「轉換爲」../ price「,它只會在父級中尋找'price'元素'// price'將在整個結構中尋找元素 – freefaller

2

假設你的來源有一個命名空間,它應該有一個xmlns聲明無論是在product元素或一些祖先。我會假設是這樣的:

<product xmlns="your-namespace"> 
    <productsize measurement="ml">250</productsize> 
    <productsize measurement="ml">500</productsize> 
    ... 
</product> 

在這種情況下,你也必須聲明,命名空間在您的XSL:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:x="your-namespace" version="1.0"> ... </xsl:stylesheet> 

然後,爲了獲得你想要的結果,你可以使用這個樣式表:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:x="your-namespace" version="1.0"> 

    <xsl:output method="text"/> 

    <xsl:template match="/"> 
     <xsl:apply-templates select="x:product/x:productsize"/> 
    </xsl:template> 

    <xsl:template match="x:productsize"> 
     <xsl:variable name="size" select="."></xsl:variable> 
     <xsl:value-of select="."/> 
     <xsl:value-of select="@measurement"/><xsl:text> </xsl:text> 
     <xsl:apply-templates select="../x:price[@size=$size]"/><xsl:text> 
</xsl:text> 
    </xsl:template> 

    <xsl:template match="x:price"> 
     <xsl:value-of select="@packsize"/><xsl:text>=£</xsl:text> 
     <xsl:value-of select="."/> 
     <xsl:if test="not(position() = last())"> 
      <xsl:text>, </xsl:text> 
     </xsl:if> 
    </xsl:template> 

</xsl:stylesheet> 

至於爲什麼你的樣式表不工作,有很多可能的原因。如果名稱空間被正確聲明,則您嘗試在productsize的上下文中閱讀price,但它們不是嵌套的。您必須使用相對或絕對XPath表達式訪問每個移出您的上下文的價格。我使用上面的../price,但它也可以與//price一起使用。

我假設你沒有發佈任何其他代碼,並且不能從你的例子中假設是不相關的。如果您有其他product元素,或者products實際上不是root用戶,則必須修改樣式表。