2012-08-08 102 views
1

假設的另一個值的值我有以下XML:XSL - 如何檢索XML節點的基礎上的XML節點

<ns5:OrderTotal> 
    <ns5:Name>AmountPaid</ns5:Name> 
     <ns5:Description>Payment Received</ns5:Description> 
    <ns5:SortOrder>4</ns5:SortOrder> 
    <ns5:Value>16.95</ns5:Value> 
    </ns5:OrderTotal> 
    <ns5:OrderTotal> 
    <ns5:Name>AmountDue</ns5:Name> 
     <ns5:Description>Current Amount Due</ns5:Description> 
    <ns5:SortOrder>5</ns5:SortOrder> 
    <ns5:Value>0.0</ns5:Value> 
    </ns5:OrderTotal> 

如果我只想要打印的OrderTotal的價值時,它的名字是AmountPaid,怎麼可能做到這一點?

謝謝你的幫助。

回答

1

我不知道你將如何打印您需要的價值,但它可以使用下面的XPath表達式的途徑有:

//ns5:OrderTotal[child::ns5:Name = 'AmountPaid']/ns5:Value 

例如:

XML:

<ns5:Orders xmlns:ns5="a"> 
    <ns5:OrderTotal> 
     <ns5:Name>AmountPaid</ns5:Name> 
     <ns5:Description>Payment Received</ns5:Description> 
     <ns5:SortOrder>4</ns5:SortOrder> 
     <ns5:Value>16.95</ns5:Value> 
    </ns5:OrderTotal> 
    <ns5:OrderTotal> 
     <ns5:Name>AmountDue</ns5:Name> 
     <ns5:Description>Current Amount Due</ns5:Description> 
     <ns5:SortOrder>5</ns5:SortOrder> 
     <ns5:Value>0.0</ns5:Value> 
    </ns5:OrderTotal> 
</ns5:Orders> 

XSL:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" version="1.0" 
    xmlns:ns5="a"> 
    <xsl:template match="/"> 
    <xsl:element name="Value"> 
     <xsl:value-of select="//ns5:OrderTotal[child::ns5:Name = 'AmountPaid']/ns5:Value"/> 
    </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 

輸出:

<?xml version="1.0" encoding="utf-8"?> 
<Value>16.95</Value> 
+0

謝謝,這就是我要找的。 – 2012-08-08 23:58:53

+0

@yupinghe,不客氣。如果您認爲這是您問題的正確答案,請將其標記爲如此。 – khachik 2012-08-09 07:04:42

0
<xsl:if test="Name = 'AmountPaid'"> 
<xsl:value-of select="Value"/> 
</xsl:if> 

嘗試上面的XSL

+0

THX,但我想這和它似乎regardlessly返回第一個節點的值。因此,如果在AmountPaid之前有另一個名爲「ShippingPaid」的節點,它將返回ShippingPaid值。 – 2012-08-09 00:08:01