0

I am trying to set the condition when price is greater than 100 or less than 50 it should output, however with the following condition I am getting all results outputted.

<xsl:for-each select=".//G_1/Product[.//PRICE >'100' or .//PRICE <'50' ]"><?PRICE?></xsl:for-each>

The XML

<?xml version="1.0" encoding="UTF-8" ?>
<DATA_DS>
    <G_1>
        <Product>
            <OBJECT_ACTION_ID>79071819</OBJECT_ACTION_ID>
            <Details>
                <ID>170057541</ID>
                <NAME>ITEM NAME</NAME>
            </Details>
            <Rec>
                <Rec1>
                    <Rec_Detail>
                        <C_Product_ID>300000155370949</C_Product_ID>
                        <PRICE>200</PRICE>
                    </Rec_Detail>
                </Rec1>
            </Rec>
        </Product>
        <Product>
            <OBJECT_ACTION_ID>79071820</OBJECT_ACTION_ID>
            <Details>
                <ID>170057542</ID>
                <NAME>ITEM NAME2</NAME>
            </Details>
            <Rec>
                <Rec1>
                    <Rec_Detail>
                        <C_Product_ID>300000155370950</C_Product_ID>
                        <PRICE>90</PRICE>
                    </Rec_Detail>
                </Rec1>
            </Rec>
        </Product>
    </G_1>
</DATA_DS>
user1660680
  • 97
  • 2
  • 11
  • Please post a [mcve]. Note that the code you show should produce an error because `<` is reserved character. Also `` is a processing instruction and will not create any output. And if you want the comparison to be numerical, then do not put quotes around the numbers. – michael.hor257k Jul 25 '21 at 22:40
  • added the xml, I tried to replace > with > as well but it didn't seem to work. – user1660680 Jul 25 '21 at 22:50
  • Add the expected output too. -- P.S. The problem is not with `>` but with `<`. – michael.hor257k Jul 25 '21 at 22:55
  • Does your query produce an incorrect answer with this XML? If not, please show us an XML where it does produce an incorrect answer. You say you are "getting all results outputted" -- ie presumably all products -- but there is only one product here, and it should be selected, so that's the expected result. – Michael Kay Jul 26 '21 at 07:59
  • Updated xml to include the other record it also outputs. – user1660680 Jul 26 '21 at 08:04
  • We are not making progress here. Please post a [mcve] showing an XML, a complete, executable XSLT and the expected result. – michael.hor257k Jul 26 '21 at 09:14

1 Answers1

0

Yes. Your output is probably correct (as in "what you requested"). In your expression you used the .// axis as a prefix for all of your conditions. But you'd better use them only in the predicate. Have a look at What is the difference between .// and //* in XPath?.

So, to make your XSLT/XPath probably work, change it to

<xsl:for-each select="//G_1/Product[.//PRICE > 100 or 50 > .//PRICE]">YOUR OUTPUT</xsl:for-each>

This will match - and iterate over all matches - if anyPRICE descendant of any G_1/Product element has a value below 50 or above 100. I changed the predicate (the condition between [ and ]) in a non-obvious way to avoid the usage of the < character, because it is invalid in predicate expressions. Therefore I changed PRICE < 50 to 50 > PRICE - that's all, nothing special.

zx485
  • 28,498
  • 28
  • 50
  • 59