Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
724 views
in Technique[技术] by (71.8m points)

xslt - Populating empty nodes based on header level condition in Multi-Level xml

I am working with the below XML structure and want to update all the TaskCode & TASK nodes with value as "I" for the Partner record with ObjectChangeIndicator field value set as "INSERT".

    <?xml version="1.0" encoding="utf-8"?>
    <BusinessPartner Date="2021-01-09" Time="15:31:19">
        <Partner>
            <PartnerHeader>
                <ObjectInstance>
                    <Partner>
                        <PartyID schemeID="PartnerID">0001000001</PartyID>
                    </Partner>
                </ObjectInstance>
            </PartnerHeader>
            <PartnerRecord>
                <PartnerRecordHeader>
                    <ObjectChangeIndicator>UPDATE</ObjectChangeIndicator>
                </PartnerRecordHeader>
                <EmploymentInformation>
                    <Employment>
                        <TaskCode>U</TaskCode>
                        <EmploymentDataKey>2018-04-04</EmploymentDataKey>
                        <EmploymentData>
                            <TASK>U</TASK>
                            <EndDate>2030-04-30</EndDate>
                            <StatusCode>01</StatusCode>
                            <Employer>ABC Inc.</Employer>
                            <OccupationGroupCode>TEAC</OccupationGroupCode>
                            <AnnualGrossIncome Amount="0.00 "/>
                        </EmploymentData>
                    </Employment>
                </EmploymentInformation>
            </PartnerRecord>
        </Partner>
        <Partner>
            <PartnerHeader>
                <ObjectInstance>
                    <Partner>
                        <PartyID schemeID="PartnerID">0001000002</PartyID>
                    </Partner>
                </ObjectInstance>
            </PartnerHeader>
            <PartnerRecord>
                <PartnerRecordHeader>
                    <ObjectChangeIndicator>INSERT</ObjectChangeIndicator>
                </PartnerRecordHeader>
                <EmploymentInformation>
                    <Employment>
                        <TaskCode/>
                        <EmploymentDataKey>2020-01-09</EmploymentDataKey>
                        <EmploymentData>
                            <TASK/>
                            <EndDate>2025-01-31</EndDate>
                            <StatusCode>02</StatusCode>
                            <Employer>XYZ Inc.</Employer>
                            <OccupationGroupCode>ACBK</OccupationGroupCode>
                            <AnnualGrossIncome Amount="80000.00 "/>
                        </EmploymentData>
                    </Employment>
                </EmploymentInformation>
            </PartnerRecord>
        </Partner>
    </BusinessPartner>

Earlier the message had only one Partner record per message and it didn't require any looping condition and was working fine. When I added the loop along with the if condition it stopped working.

Any suggestions/code reference would be more than helpful. Below is my XSL transformation

    <?xml version="1.0" encoding="UTF-8"?>
    <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:strip-space elements="*"/>
        
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
        
        <xsl:for-each select="BusinessPartner/Partner">
            <xsl:if test="PartnerRecord/PartnerRecordHeader/ObjectChangeIndicator[text()='INSERT']">
                <xsl:template match="TaskCode[not(node())]">
                    <TaskCode>I</TaskCode>
                </xsl:template>
                <xsl:template match="TASK[not(node())]">
                    <TASK>I</TASK>
                </xsl:template>
            </xsl:if>
        </xsl:for-each>
    </xsl:stylesheet>
question from:https://stackoverflow.com/questions/65644006/populating-empty-nodes-based-on-header-level-condition-in-multi-level-xml

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I guess you want

    <xsl:template match="BusinessPartner/Partner/PartnerRecord[PartnerRecordHeader/ObjectChangeIndicator[. = 'INSERT']]//TaskCode[not(node())] | 
                         BusinessPartner/Partner/PartnerRecord[PartnerRecordHeader/ObjectChangeIndicator[. = 'INSERT']]//TASK[not(node())]">
        <xsl:copy>I</xsl:copy>
    </xsl:template>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...