xpath - How do I check for two codes in xml list? -


i need know if given product list contains 2 specific products. if both exist need ignore one. if 1 of them exists need retain product.

xml 1

<ns0:items xmlns:ns0="abc">   <ns0:item>     <ns0:code>x1</ns0:code> <!-- keep because 1 -->     <ns0:quantity>1</ns0:quantity>   </ns0:item> </ns0:items> 

xml 2

<ns0:items xmlns:ns0="abc">   <ns0:item>     <ns0:code>x1</ns0:code> <!-- ignore because have valid product -->     <ns0:quantity>1</ns0:quantity>   </ns0:item>   <ns0:item>     <ns0:code>m1</ns0:code>     <ns0:quantity>1</ns0:quantity>   </ns0:item> </ns0:items> 

xslt

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:i="http://www.w3.org/2001/xmlschema-instance" xmlns:ns0="abc" version="1.0">   <xsl:output method="xml" indent="yes" encoding="utf-16" omit-xml-declaration="no" />   <xsl:template match="ns0:items">     <items>       <xsl:variable name="hasboth">         <xsl:value-of select="boolean(ns0:item/ns0:code[.='m1']) , boolean(ns0:item/ns0:code[.='x1'])" />       </xsl:variable>       <xsl:for-each select="ns0:item">         <xsl:variable name="validitem">           <xsl:choose>             <xsl:when test="$hasboth , ns0:code='x1' , ns0:quantity=1">               <xsl:value-of select="0"/>             </xsl:when>             <xsl:otherwise>               <xsl:value-of select="1"/>             </xsl:otherwise>           </xsl:choose>         </xsl:variable>         <both>           <xsl:value-of select="$hasboth"/>         </both>         <expr>           <xsl:value-of select="$hasboth , ns0:code='x1' , ns0:quantity=1"/>         </expr>         <valid>           <xsl:value-of select="$validitem"/>         </valid>         <xsl:if test="$validitem = 1">           <salesorderdetail>             <xsl:copy-of select="."/>           </salesorderdetail>         </xsl:if>       </xsl:for-each>     </items>   </xsl:template> </xsl:stylesheet> 

result 1 - wrong, removes x1 product though one, how can $hasboth false , expr true?

<items>   <both>false</both>   <expr>true</expr>   <valid>0</valid> </items> 

result 2 - correct, removes x1 product

<items>   <both>true</both>   <expr>true</expr>   <valid>0</valid>   <both>true</both>   <expr>false</expr>   <valid>1</valid>   <salesorderdetail>   </salesorderdetail> </items> 

i think it's issue hasboth variable. when use xsl:value-of when create it, result string.

when test $hasboth true when string value "false" because:

boolean("false") = true() 

also, shouldn't need use boolean().

try changing this:

<xsl:variable name="hasboth">   <xsl:value-of select="boolean(ns0:item/ns0:code[.='m1']) , boolean(ns0:item/ns0:code[.='x1'])" /> </xsl:variable> 

to this:

<xsl:variable name="hasboth"          select="ns0:item/ns0:code[.='m1'] , ns0:item/ns0:code[.='x1']"/> 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -