yuping he
yuping he

Reputation: 13

xsl - how to retrieve the value of a xml node based on another value of a xml node

assuming that I have following 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>

If I only want print the Value of the OrderTotal when it Name is AmountPaid, how could achieve this?

Thank you for the helps.

Upvotes: 1

Views: 87

Answers (2)

khachik
khachik

Reputation: 28703

I don't know how you are going to print the value you need, but it can be gathered using the following XPath expression:

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

For example:

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>

Output:

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

Upvotes: 1

sundar
sundar

Reputation: 1760

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

Try the above XSL.

Upvotes: 0

Related Questions