0

I want to compare a single value if it's existing to an array. I have an XML input file:

<wd:Report_Entry
    xmlns:wd="urn:com.workday.report/CRI_-_INTSG031_CM_011_-_PUBBS_HSA_Customer_Invoice">
    <wd:CFI_ARI_PUBBS_HSA_group>
        <wd:pubbsType>1</wd:pubbsType>
        <wd:pubbsService>A+</wd:pubbsService>
        <wd:validFrom>2019-08-01</wd:validFrom>
        <wd:validTo>2100-01-01</wd:validTo>
        <wd:CF_FD_PUBBS_HSA_Valid_From_Date>2019-08-01</wd:CF_FD_PUBBS_HSA_Valid_From_Date>
        <wd:CF_FD_PUBBS_HSA_Valid_To_Date>2100-01-01</wd:CF_FD_PUBBS_HSA_Valid_To_Date>
    </wd:CFI_ARI_PUBBS_HSA_group>
    <wd:CFI_ARI_PUBBS_HSA_group>
        <wd:pubbsType>1</wd:pubbsType>
        <wd:pubbsService>A-</wd:pubbsService>
        <wd:validFrom>2019-08-01</wd:validFrom>
        <wd:validTo>2100-01-01</wd:validTo>
        <wd:CF_FD_PUBBS_HSA_Valid_From_Date>2019-08-01</wd:CF_FD_PUBBS_HSA_Valid_From_Date>
        <wd:CF_FD_PUBBS_HSA_Valid_To_Date>2100-01-01</wd:CF_FD_PUBBS_HSA_Valid_To_Date>
    </wd:CFI_ARI_PUBBS_HSA_group>
    <wd:CFI_ARI_PUBBS_HSA_group>
        <wd:pubbsType>FMDAUTOPSY</wd:pubbsType>
        <wd:pubbsService>FMDAUTOPSY</wd:pubbsService>
        <wd:validFrom>2020-02-02</wd:validFrom>
        <wd:validTo>2020-02-01</wd:validTo>
        <wd:CF_FD_PUBBS_HSA_Valid_From_Date>2020-02-01</wd:CF_FD_PUBBS_HSA_Valid_From_Date>
        <wd:CF_FD_PUBBS_HSA_Valid_To_Date>2100-01-01</wd:CF_FD_PUBBS_HSA_Valid_To_Date>
    </wd:CFI_ARI_PUBBS_HSA_group>
    <wd:CFI_ARI_PUBBS_HSA_group>
        <wd:pubbsType>FMDBODYSTOR</wd:pubbsType>
        <wd:pubbsService>FMDBODYSTOR</wd:pubbsService>
        <wd:validFrom>2020-01-01</wd:validFrom>
        <wd:validTo>2020-01-31</wd:validTo>
        <wd:CF_FD_PUBBS_HSA_Valid_From_Date>2020-01-01</wd:CF_FD_PUBBS_HSA_Valid_From_Date>
        <wd:CF_FD_PUBBS_HSA_Valid_To_Date>2020-01-31</wd:CF_FD_PUBBS_HSA_Valid_To_Date>
    </wd:CFI_ARI_PUBBS_HSA_group>
 <wd:Customer_Invoice_Lines_group>
        <wd:referenceID>CUSTOMER_INVOICE_LINE-6-46</wd:referenceID>
        <wd:Unit_Cost>0</wd:Unit_Cost>
        <wd:Quantity>0</wd:Quantity>
        <wd:Calculated_Tax_Amount>0</wd:Calculated_Tax_Amount>
        <wd:CFI_AC_Merchandise_Amount>100</wd:CFI_AC_Merchandise_Amount>
        <wd:Line_Item_Description>LINE ITEM DESC</wd:Line_Item_Description>
        <wd:Line_Memo>LINE MEMO</wd:Line_Memo>
        <wd:Transaction_Tax_Amount>0</wd:Transaction_Tax_Amount>
        <wd:CFI_LRV_Sales_Item_Reference_ID>FMDAUTOPSY</wd:CFI_LRV_Sales_Item_Reference_ID>
    </wd:Customer_Invoice_Lines_group>
    <wd:CFI_FD_PUBBS_HSA_Current_Date>2020-02-16</wd:CFI_FD_PUBBS_HSA_Current_Date>
</wd:Report_Entry>

I want to know if the wd:CFI_LRV_Sales_Item_Reference_ID value is equal to wd:Customer_Invoice_Lines_group/wd:CFI_ARI_PUBBS_HSA_group/wd:pubbsService and wd:CFI_FD_PUBBS_HSA_Current_Date is greater than wd:CF_FD_PUBBS_HSA_Valid_To_Date and then just return a string value true if there is. I have a code like this but this doesn't work because I'm comparing a value to an object that has multiple values. Not sure how to do compare single value to an array using xslt 2 or 3.. Appreciate the help.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mf="http://example.com/mf"
    exclude-result-prefixes="#all" version="3.0"
    xmlns:wd="urn:com.workday.report/CRI_-_INTSG031_CM_011_-_PUBBS_HSA_Customer_Invoice"
    xmlns:d9="urn:this-stylesheet">

    <xsl:output method="xml"/>

    <xsl:template match="/">
        <PUBBS>      


            <xsl:for-each select="wd:Report_Entry/wd:Customer_Invoice_Lines_group">
                <xsl:if test="wd:CFI_LRV_Sales_Item_Reference_ID != ''">
                    <xsl:for-each select="../wd:CFI_ARI_PUBBS_HSA_group">
                        <xsl:if test="../wd:CFI_ARI_PUBBS_HSA_group/wd:pubbsService != '' 
                            and wd:CFI_LRV_Sales_Item_Reference_ID = ../wd:CFI_ARI_PUBBS_HSA_group/wd:pubbsService
                            and xs:date(../wd:CFI_FD_PUBBS_HSA_Current_Date) &gt; xs:date(../wd:CFI_ARI_PUBBS_HSA_group/wd:CF_FD_PUBBS_HSA_Valid_To_Date)
                            ">    
                            <HASINVALIDSALESITEM>
                                <xsl:value-of select="'true'"/>
                            </HASINVALIDSALESITEM>   
                        </xsl:if>
                    </xsl:for-each>
                </xsl:if>  
            </xsl:for-each>
    </PUBBS>
    </xsl:template>
</xsl:stylesheet>
1
  • First of all, while XSLT 3 with XPath 3.1 has an array data type, that is an extension to the previous XSLT 2 data model to deal with JSON arrays and to have a nestable, lightweight data structure when not using XML; your question shows XML data, so I don't see how you would encounter any arrays there. As for your existing code, when it doesn't work, which error exactly do you get? Or which wrong output do you get, which is the exact result you want for the show input sample? Commented Feb 16, 2020 at 9:46

1 Answer 1

0

I have tried to simplify the code by using xpath-default-namespace and by using templates; I have also assumed the two values you want to compare the two details of the various CFI_ARI_PUBBS_HSA_group to only exist once in the input document:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xpath-default-namespace="urn:com.workday.report/CRI_-_INTSG031_CM_011_-_PUBBS_HSA_Customer_Invoice"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:output method="xml" indent="yes"/>

  <xsl:mode on-no-match="shallow-skip"/>

  <xsl:template match="/*">
      <PUBBS>
          <xsl:apply-templates select="CFI_ARI_PUBBS_HSA_group"/>
      </PUBBS>
  </xsl:template>

  <xsl:template match="CFI_ARI_PUBBS_HSA_group[xs:date(//CFI_FD_PUBBS_HSA_Current_Date) > xs:date(CF_FD_PUBBS_HSA_Valid_To_Date)
                       and
                       //CFI_LRV_Sales_Item_Reference_ID = pubbsService]">
     <HASINVALIDSALESITEM>true</HASINVALIDSALESITEM> 
  </xsl:template>

</xsl:stylesheet>

At https://xsltfiddle.liberty-development.net/jz1Q1ym that gives an empty <PUBBS/>, which seems the right result as the only CFI_ARI_PUBBS_HSA_group with pubbsService having the value FMDAUTOPSY has a CF_FD_PUBBS_HSA_Valid_To_Date of 2100-01-01 and the CFI_FD_PUBBS_HSA_Current_Date with the value 2020-02-16 is not greater than that.

It is not quite clear whether you want a single output of HASINVALIDSALESITEM even if there are several but the code could obviously adapted.

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

Comments

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.