XSLT не дает желаемого результата от XML - PullRequest
1 голос
/ 01 апреля 2020

Из моего XML Я пытаюсь получить значение в теге Value, но оно всегда возвращается пустым.

В XML есть несколько дочерних элементов, поэтому я ищу ResultOutput тег с определенным именем

Вот мой XML, который я пытаюсь преобразовать и получить значения, я проверил это онлайн, используя XML -> XSD-преобразователь, он работает только и получает поврежденный вывод если теги xml, soap и body отсутствуют

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <ExecuteDecisionSmartResponse>
            <ExecuteDecisionSmartResult>
                <DecisionLogId>5d67e71e-45b4-4e9c-a801-5a8247c82cae</DecisionLogId>
                <DateExecuted>2020-04-01T07:28:40.2333046+02:00</DateExecuted>
                <UniqueIdentifier>6903245127088</UniqueIdentifier>
                <DecisionFlowExternalReference>PreBureauRules</DecisionFlowExternalReference>
                <DecisionFlowId>1</DecisionFlowId>
                <DecisionFlowVersionNo>1</DecisionFlowVersionNo>
                <DecisionSmartResults>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDE_N - ApplicantAge - DECISION</Name>
                        <Value>Accept</Value>
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDE_N - ApplicantAge - FAILUREREASON</Name>
                        <Value />
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDE_N - CustomerIdType - DECISION</Name>
                        <Value>Accept</Value>
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDE_N - CustomerIdType - FAILUREREASON</Name>
                        <Value />
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDE_N - ArticleType - DECISION</Name>
                        <Value>Accept</Value>
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDE_N - ArticleType - FAILUREREASON</Name>
                        <Value />
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDE_N - PaymentFrequency - DECISION</Name>
                        <Value>Decline</Value>
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDE_N - PaymentFrequency - FAILUREREASON</Name>
                        <Value>H006</Value>
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDE_N - AgreementType - DECISION</Name>
                        <Value>Decline</Value>
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDE_N - AgreementType - FAILUREREASON</Name>
                        <Value>H007</Value>
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDE_N - InterestRateType - DECISION</Name>
                        <Value>Decline</Value>
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDE_N - InterestRateType - FAILUREREASON</Name>
                        <Value>H008</Value>
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDE_N - PurchasePriceMin - DECISION</Name>
                        <Value>Accept</Value>
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDE_N - PurchasePriceMin - FAILUREREASON</Name>
                        <Value />
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDE_N - LoanDuration - DECISION</Name>
                        <Value>Accept</Value>
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDE_N - LoanDuration - FAILUREREASON</Name>
                        <Value />
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDE_N - DealerValidFlag - DECISION</Name>
                        <Value>Accept</Value>
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDE_N - DealerValidFlag - FAILUREREASON</Name>
                        <Value />
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDE_N - IGFValidFlag - DECISION</Name>
                        <Value>Accept</Value>
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDE_N - IGFValidFlag - FAILUREREASON</Name>
                        <Value />
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDE_N - ITCConcentIndicator - DECISION</Name>
                        <Value>Decline</Value>
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDE_N - ITCConcentIndicator - FAILUREREASON</Name>
                        <Value>H026</Value>
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDE_N - COMBINEDFAILUREREASONS</Name>
                        <Value>H006, H007, H008, H026</Value>
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDES_Y - SourceOfIncome - DECISION</Name>
                        <Value>Accept</Value>
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDES_Y - SourceOfIncome - FAILUREREASON</Name>
                        <Value />
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDES_Y - CustomerOccupation - DECISION</Name>
                        <Value>Accept</Value>
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDES_Y - CustomerOccupation - FAILUREREASON</Name>
                        <Value />
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDES_Y - PurchasePriceMax - DECISION</Name>
                        <Value>Accept</Value>
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDES_Y - PurchasePriceMax - FAILUREREASON</Name>
                        <Value />
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDES_Y - PaymentMethod - DECISION</Name>
                        <Value>Decline</Value>
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDES_Y - PaymentMethod - FAILUREREASON</Name>
                        <Value>H016</Value>
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDES_Y - EmploymentDuration - DECISION</Name>
                        <Value>Accept</Value>
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDES_Y - EmploymentDuration - FAILUREREASON</Name>
                        <Value />
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDES_Y - SAFPS_Response - DECISION</Name>
                        <Value>Decline</Value>
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDES_Y - SAFPS_Response - FAILUREREASON</Name>
                        <Value>H020</Value>
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDES_Y - GrossAnnualRemuneration - DECISION</Name>
                        <Value>Decline</Value>
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDES_Y - GrossAnnualRemuneration - FAILUREREASON</Name>
                        <Value>H010</Value>
                    </ResultOutput>
                    <ResultOutput>
                        <Name>RULES_PREBUREAU_OVERRIDES_Y - COMBINEDFAILUREREASONS</Name>
                        <Value>H016, H020, H010</Value>
                    </ResultOutput>
                    <ResultOutput>
                        <Name>OUTPUT_PREBUREAU_RESULT - CALCULATED</Name>
                        <Value>Declined</Value>
                    </ResultOutput>
                    <ResultOutput>
                        <Name>OUTPUT_PREBUREAU_OVERRIDE_RESULT - CALCULATED</Name>
                        <Value>N</Value>
                    </ResultOutput>
                </DecisionSmartResults>
            </ExecuteDecisionSmartResult>
        </ExecuteDecisionSmartResponse>
    </soap:Body>
</soap:Envelope>

Вот мой XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:prin="http://tempuri.org/">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>  
    <xsl:template match="/">
        <DecisionSmartResults xmlns="http://tempuri.org/">
            <MQ_PreBureauResults>
                <xsl:value-of select=":ExecuteDecisionSmartResponse/ExecuteDecisionSmartResult/DecisionSmartResults/ResultOutput[Name='OUTPUT_PREBUREAU_RESULT - CALCULATED']/Value"/>
            </MQ_PreBureauResults>
            <MQ_PreBureauOverrideResults>
                <xsl:value-of select="ExecuteDecisionSmartResponse/ExecuteDecisionSmartResult/DecisionSmartResults/ResultOutput[Name='OUTPUT_PREBUREAU_OVERRIDE_RESULT - CALCULATED']/Value"/>
            </MQ_PreBureauOverrideResults>
            <MQ_PreBureauOverrideResultsY>
                <xsl:value-of select="ExecuteDecisionSmartResponse/ExecuteDecisionSmartResult/DecisionSmartResults/ResultOutput[Name='RULES_PREBUREAU_OVERRIDES_Y - COMBINEDFAILUREREASONS']/Value"/>
            </MQ_PreBureauOverrideResultsY>
            <MQ_PreBureauOverrideResultsN>
                <xsl:value-of select="ExecuteDecisionSmartResponse/ExecuteDecisionSmartResult/DecisionSmartResults/ResultOutput[Name='RULES_PREBUREAU_OVERRIDE_N - COMBINEDFAILUREREASONS']/Value"/>
            </MQ_PreBureauOverrideResultsN>     
        </DecisionSmartResults>
    </xsl:template>
</xsl:stylesheet>

1 Ответ

1 голос
/ 01 апреля 2020

Ваш шаблон соответствует узлу / root. Из этого контекста инструкция:

<xsl:value-of select="ExecuteDecisionSmartResponse/ExecuteDecisionSmartResult/DecisionSmartResults/ResultOutput[Name='OUTPUT_PREBUREAU_OVERRIDE_RESULT - CALCULATED']/Value"/>

ничего не выбирает, потому что ExecuteDecisionSmartResponse не является дочерним элементом узла контекста.

Чтобы избежать повторения кода, я бы предложил вам попробовать это так:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> 

<xsl:template match="/">
    <xsl:variable name="output" select="*/*/ExecuteDecisionSmartResponse/ExecuteDecisionSmartResult/DecisionSmartResults/ResultOutput"/>
    <DecisionSmartResults xmlns="http://tempuri.org/">
        <MQ_PreBureauResults>
            <xsl:value-of select="$output[Name='OUTPUT_PREBUREAU_RESULT - CALCULATED']/Value"/>
        </MQ_PreBureauResults>
        <MQ_PreBureauOverrideResults>
            <xsl:value-of select="$output[Name='OUTPUT_PREBUREAU_OVERRIDE_RESULT - CALCULATED']/Value"/>
        </MQ_PreBureauOverrideResults>
        <MQ_PreBureauOverrideResultsY>
            <xsl:value-of select="$output[Name='RULES_PREBUREAU_OVERRIDES_Y - COMBINEDFAILUREREASONS']/Value"/>
        </MQ_PreBureauOverrideResultsY>
        <MQ_PreBureauOverrideResultsN>
            <xsl:value-of select="$output[Name='RULES_PREBUREAU_OVERRIDE_N - COMBINEDFAILUREREASONS']/Value"/>
        </MQ_PreBureauOverrideResultsN>     
    </DecisionSmartResults>
</xsl:template>

</xsl:stylesheet>

или более явно:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
exclude-result-prefixes="soap">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> 

<xsl:template match="/">
    <xsl:variable name="output" select="soap:Envelope/soap:Body/ExecuteDecisionSmartResponse/ExecuteDecisionSmartResult/DecisionSmartResults/ResultOutput"/>

    <!-- same as above -->

</xsl:template>

</xsl:stylesheet>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...