Как исправить: проблемы условной фильтрации XSLT 3.0 (если) - PullRequest
0 голосов
/ 24 июня 2019

Условие «если» внутри цикла for-each не работает. На основе кода региона из входного файла и файла перекрестной ссылки некоторые сообщения с определенным кодом страны не должны создаваться.

например, если входное сообщение содержит код страны CH, которого нет во входном сообщении, полезная нагрузка не должна выводиться как выходной.

<?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:fn="http://www.w3.org/2005/xpath-functions" xmlns:math="http://www.w3.org/2005/xpath-functions/math" xmlns:array="http://www.w3.org/2005/xpath-functions/array" xmlns:map="http://www.w3.org/2005/xpath-functions/map" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:err="http://www.w3.org/2005/xqt-errors" exclude-result-prefixes="array fn map math xhtml xs err" version="3.0">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:param name="xslt.transform.params">DE|AT|UK|IE|NL</xsl:param>
    <xsl:variable name="vCountryCode">
        <xsl:value-of select="$xslt.transform.params"/>
    </xsl:variable>
    <xsl:param name="xslt.location.uriXSLTPath">
        <xsl:text>\app</xsl:text>
    </xsl:param>
    <xsl:param name="uriXSLT">
        <xsl:value-of select="$xslt.location.uriXSLTPath"/>
    </xsl:param>
    <xsl:variable name="vSupplierFullPath" select="string(concat($uriXSLT,'/SupplierCrossReference.xml'))"/>
    <xsl:variable name="vSupplierCrossReferences" select="document($vSupplierFullPath)/*:SupplierCrossReferences"/>
    <xsl:template match="/advice_file/product" name="xsl:initial-template">
        <xsl:text>"warehouses": [</xsl:text>
        <xsl:for-each select="warehouseBreakout/warehouse">
            <xsl:variable name="vVendor" select="../../vendor"/>
            <xsl:variable name="vWH" select="@warehouse-id"/>
            <xsl:variable name="vReg" select="fn:copy-of($vSupplierCrossReferences/SupplierCrossReference[TraitID=$vVendor]/warehouses/warehouse[@id=$vWH])"/>
            <xsl:text>{</xsl:text>
            <xsl:text>"warehouseId" : "</xsl:text>
            <xsl:value-of select=" fn:concat($vVendor,'-', $vWH)"/>
            <xsl:text>",</xsl:text>
            <xsl:text>"quantity" : </xsl:text>
            <xsl:value-of select="qtyonhand"/>
            <xsl:choose>
                <xsl:when test="next_available/@quantity != '' and next_available/@quantity !=0 ">
                    <xsl:variable name="vNextDate" select="next_available/@date"/>
                    <xsl:text>,</xsl:text>
                    <xsl:text>"nextAvailableDate": "</xsl:text>
                    <xsl:value-of select="fn:concat(fn:substring($vNextDate,1,4),'-',fn:substring($vNextDate,5,2),'-',fn:substring($vNextDate,7,2))"/>
                    <xsl:text>",</xsl:text>
                    <xsl:text>"nextAvailableQuantity": </xsl:text>
                    <xsl:value-of select="next_available/@quantity"/>
                </xsl:when>
            </xsl:choose>
            <xsl:text>,"servicedRegion" : [</xsl:text>
            <xsl:for-each select="$vReg//region">
                <xsl:text>"</xsl:text>
                <xsl:variable name="vRegionCode" select="."/>
                <h3>
                    <xsl:value-of select="$vRegionCode"/>
                </h3>
                <xsl:if test="fn:contains($vCountryCode,$vRegionCode)">
                    <xsl:text>"</xsl:text>
                    <xsl:if test="fn:position() != fn:last()">
                        <xsl:text>,</xsl:text>
                    </xsl:if>
                </xsl:if>
            </xsl:for-each>
            <xsl:text>]</xsl:text>
            <xsl:text>}</xsl:text>
            <xsl:if test="position() != last()">
                <xsl:text>, </xsl:text>
            </xsl:if>
        </xsl:for-each>
        <xsl:text>]}</xsl:text>
    </xsl:template>
</xsl:stylesheet>

Входное сообщение:

<?xml version="1.0" encoding="UTF-8"?>
<advice_file as-of-date="2019-06-20T09:45:10.882+01:00" advice-content="incr">
    <advice_file_control_number>1561020310882</advice_file_control_number>
    <product last-update-date="2019-06-20T08:48:22.583+01:00">
        <vendor_SKU>AW23377-GY</vendor_SKU>
        <qtyonhand>390</qtyonhand>
        <available>YES</available>
        <next_available_qty>0</next_available_qty>
        <vendor>393</vendor>
        <warehouseBreakout>
            <warehouse warehouse-id="No merchant (warehouse)ID has been provided.">
                <qtyonhand>390</qtyonhand>
                <next_available quantity="0"/>
            </warehouse>
        </warehouseBreakout>
    </product>
    <product last-update-date="2019-06-20T08:48:22.583+01:00">
        <vendor_SKU>AW22226-WH</vendor_SKU>
        <qtyonhand>250</qtyonhand>
        <available>YES</available>
        <next_available_qty>0</next_available_qty>
        <vendor>342</vendor>
        <warehouseBreakout>
            <!--<warehouse warehouse-id="No merchant (warehouse)ID has been provided.">-->
            <warehouse warehouse-id="01">
                <qtyonhand>250</qtyonhand>
                <next_available quantity="0"/>
            </warehouse>
        </warehouseBreakout>
    </product>
    <product last-update-date="2019-06-20T09:40:11.797+01:00">
        <vendor_SKU>EL-FC22</vendor_SKU>
        <qtyonhand>734</qtyonhand>
        <available>YES</available>
        <min_days_to_fulfill>3</min_days_to_fulfill>
        <UPC>4016946199445</UPC>
        <next_available_qty>0</next_available_qty>
        <vendor>364</vendor>
        <warehouseBreakout>
            <warehouse warehouse-id="01">
                <qtyonhand>734</qtyonhand>
                <next_available quantity="0"/>
            </warehouse>
        </warehouseBreakout>
    </product>
    <advice_file_count>298</advice_file_count>
</advice_file>

Фактический результат:

<?xml version="1.0" encoding="UTF-8"?>
    "warehouses": [{"warehouseId" : "393-No merchant (warehouse)ID has been provided.","quantity" : 390,"servicedRegion" : []}]}
    "warehouses": [{"warehouseId" : "342-01","quantity" : 250,"servicedRegion" : ["<h3>CH</h3>]}]}
    "warehouses": [{"warehouseId" : "364-01","quantity" : 734,"servicedRegion" : ["<h3>DE</h3>","<h3>AT</h3>"]}]}

Ожидаемый результат:

<?xml version="1.0" encoding="UTF-8"?>
    "warehouses": [{"warehouseId" : "393-No merchant (warehouse)ID has been provided.","quantity" : 390,"servicedRegion" : []}]}
    "warehouses": [{"warehouseId" : "364-01","quantity" : 734,"servicedRegion" : ["<h3>DE</h3>","<h3>AT</h3>"]}]}

Ссылочный файл:

<SupplierCrossReference name="Franken">
        <TraitID>364</TraitID>
        <!--<STEPSupplierID>SUPP_8888888</STEPSupplierID>-->
        <!-- NOTE the _C_ added as a tempory fix for Product stream POC -->
        <STEPSupplierID>SUPP_C_888888</STEPSupplierID>
        <STEPSuppliers>
            <Supplier store="DE">**********</Supplier>
            <Supplier store="AT">**********</Supplier>
        </STEPSuppliers>
        <AS400SupplierID>621</AS400SupplierID>
        <LegacySenderReceiverID>DE-FRANK2</LegacySenderReceiverID>
        <LegacySenderReceiverID>AT-FRANK2</LegacySenderReceiverID>
        <ComsysSenderReceiverID>DE-FRANK4</ComsysSenderReceiverID>
        <ComsysSenderReceiverID>AT-FRANK4</ComsysSenderReceiverID>
        <warehouses>
            <warehouse id='01'>
                <region>DE</region>
                <region>AT</region>
            </warehouse>
        </warehouses>
    </SupplierCrossReference>
...