2012-08-08 98 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