1

I have an XML input as shown below. I want to print output of each customer information for fields Cust_1, Gender & Price. I only want to print the price of chairs with Red color. I tried using choose-when and if but not getting the desired output. Any help is greatly appreciated.

<Sales_Data>
    <Furniture>
        <Customer_Reference>Cust_1</Customer_Reference>
        <Gender>M</Gender>
        <Chairs>
            <Color_Ref>Black</Color_Ref>
            <Price>
                <Unit_Price>1000</Unit_Price>
            </Price>
        </Chairs>
        <Chairs>
            <Color_Ref>Red</Color_Ref>
            <Price>
                <Unit_Price>1100</Unit_Price>
            </Price>
        </Chairs>
    </Furniture>
    <Furniture>
        <Customer_Reference>Cust_2</Customer_Reference>
        <Gender>F</Gender>
        <Chairs>
            <Color_Ref>Blue</Color_Ref>
            <Price>
                <Unit_Price>950</Unit_Price>
            </Price>
        </Chairs>
        <Chairs>
            <Color_Ref>Red</Color_Ref>
            <Price>
                <Unit_Price>1050</Unit_Price>
            </Price>
        </Chairs>
    </Furniture>
</Sales_Data>
2
  • 1
    You should show us your code so we can see where you went wrong. You have obviously misunderstood some aspect of the language spec, and if you showed us your mistakes then we could explain the concepts you are missing. Commented May 4, 2020 at 21:28
  • thanks, this has been resolved now Commented May 5, 2020 at 20:26

1 Answer 1

1

To output the desired value, use the following XPath-1.0 expression

/Sales_Data/Furniture[Customer_Reference='Cust_1' and Gender='M']/Chairs[Color_Ref='Red']/Price/Unit_Price

or the full XSLT stylesheet

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="3.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" />

    <xsl:template match="/">
      <xsl:value-of select="/Sales_Data/Furniture[Customer_Reference='Cust_1' and Gender='M']/Chairs[Color_Ref='Red']/Price/Unit_Price"/>
    </xsl:template>

</xsl:stylesheet>

In both cases the output is the desired value 1100.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi zx485, Thanks for sharing your answer. There was a typo in my question, instead of stating customers I mentioned Cust_1. Regardless of that, I did follow the same XPATH as you've shared in your answer -> Chairs[Color_Ref='Red']. This worked fine for me. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.