Home > Blockchain >  xslt 3.0 based on a condition insert child node
xslt 3.0 based on a condition insert child node

Time:11-09

I have a requirement to insert a child node in xslt when a condition is satisfied i,e when a offered course is present in Source course . Kndly refer the Source xml and My xslt in below link

https://stackoverflow.com/questions/74339222/xslt-3-0-mapping-and-validation

Added a child node which is a copy of Learning course. This child node must be present for each offerring with corresponding course data i.e when the offered Course-Number is present in Source xml

 <!-- Matching course-->
                                
                                <xsl:if test="exists($LearningCoursesMap(current()/Lesson[1]/Course-Number))">
                                    <CourseDetails>
                                        <xsl:copy-of select="$LearningCoursesMap(current()/Lesson[1]/Course-Number)"/> 
                                    </CourseDetails>
                                </xsl:if>

Expected output

<?xml version="1.0" encoding="UTF-8"?>
<OfferingsWithCourse xmlns:map="http://www.w3.org/2005/xpath-functions/map"
    xmlns:wd="urn:com.workday/bsvc" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <Offering>
        <Lesson>
            <Offering-ID>C2212001 DL 3Wks Mon 4pm-7pm EASTERN</Offering-ID>
            <Course-Number>00151744</Course-Number>
            <Webinar>Y</Webinar>
            <Title>Launch Class Link - HDU_VILT01 - 1</Title>
            <Facilitator-LDAP>AXH4006</Facilitator-LDAP>
            <Webinar-URL>https://hdu.adobeconnect.com</Webinar-URL>
            <Location/>
            <Room>Ottawa District Training Center</Room>
            <InstructorID>119417764</InstructorID>
            <MappedLocation/>
            <MappedRoom>LOCATION-6-3881</MappedRoom>
        </Lesson>
        <Lesson>
            <Offering-ID>C2212001 DL 3Wks Mon 4pm-7pm EASTERN</Offering-ID>
            <Course-Number>00151744</Course-Number>
            <Webinar>Y</Webinar>
            <Title>Launch Class Link - HDU_VILT01 - 3</Title>
            <Facilitator-LDAP/>
            <Webinar-URL>https://hdu.adobeconnect.com</Webinar-URL>
            <Location>7070</Location>
            <Room>Ottawa District Training </Room>
            <InstructorID/>
            <MappedLocation/>
            <MappedRoom/>
            <Error>Missing required field Facilitator LDAP for at least one of the lessons in the
                offering. </Error>
            <Error>Mapped location not found for 7070. </Error>
            <Error>Mapped room not found for Ottawa District Training . </Error>
        </Lesson>
        <CourseDetails>
            <wd:Learning_Course xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                <wd:Learning_Blended_Course_Reference>
                    <wd:ID wd:type="WID">88388986cbbf10128d2b24f4f5c40000</wd:ID>
                    <wd:ID wd:type="Learning_Course_ID">00151744</wd:ID>
                    <wd:ID wd:type="Learning_Course">Designing Kitchens Part II</wd:ID>
                </wd:Learning_Blended_Course_Reference>
                <wd:Learning_Course_Data>
                    <wd:ID>00151744</wd:ID>
                    <wd:Effective_Date>2014-10-04</wd:Effective_Date>
                    <wd:Inactive>0</wd:Inactive>
                    <wd:Course_Title>Designing Kitchens Part II</wd:Course_Title>
                    <wd:Description>Designing Kitchens Part II</wd:Description>
                    <wd:Course_Number>00151744</wd:Course_Number>
                    <wd:Topic_Reference>
                        <wd:ID wd:type="WID">9d1f49654c8310154e239c7090fc0000</wd:ID>
                        <wd:ID wd:type="Learning_Topic">Product Knowledge - US</wd:ID>
                    </wd:Topic_Reference>
                    <wd:Language_Reference>
                        <wd:ID wd:type="WID">da594226446c11de98360015c5e6daf6</wd:ID>
                        <wd:ID wd:type="User_Language_ID">en_US</wd:ID>
                    </wd:Language_Reference>
                    <wd:Minimum_Enrollment_Capacity>0</wd:Minimum_Enrollment_Capacity>
                    <wd:Maximum_Enrollment_Capacity>0</wd:Maximum_Enrollment_Capacity>
                    <wd:Waitlist_Capacity>0</wd:Waitlist_Capacity>
                    <wd:Unlimited_Capacity>1</wd:Unlimited_Capacity>
                    <wd:Learning_Pricing_Data>
                        <wd:Pricing_Enabled>0</wd:Pricing_Enabled>
                        <wd:Price_in_Training_Credits>0</wd:Price_in_Training_Credits>
                    </wd:Learning_Pricing_Data>
                    <wd:Time_Value_Reference>
                        <wd:ID wd:type="WID">f31be4fd5caa10001d7c625e27014929</wd:ID>
                        <wd:ID wd:type="Learning_Time_Unit_ID">HOURS</wd:ID>
                    </wd:Time_Value_Reference>
                    <wd:Total_Course_Duration>18</wd:Total_Course_Duration>
                    <wd:Enable_Auto_Enrollment_from_Waitlist>0</wd:Enable_Auto_Enrollment_from_Waitlist>
                    <wd:Legacy_Course>1</wd:Legacy_Course>
                    <wd:Allowed_Instructor_Reference>
                        <wd:ID wd:type="WID">88388986cbbf10124df56ab318140000</wd:ID>
                        <wd:ID wd:type="Learning_Instructor_ID">100735117</wd:ID>
                    </wd:Allowed_Instructor_Reference>
                    <wd:Allowed_Instructor_Reference>
                        <wd:ID wd:type="WID">b7f5fd27bc071018bbed7a460ce30000</wd:ID>
                        <wd:ID wd:type="Learning_Instructor_ID">119417764</wd:ID>
                    </wd:Allowed_Instructor_Reference>
                    <wd:Allowed_Instructor_Reference>
                        <wd:ID wd:type="WID">88388986cbbf10124df57b8e81f30002</wd:ID>
                        <wd:ID wd:type="Learning_Instructor_ID">104034590</wd:ID>
                    </wd:Allowed_Instructor_Reference>
                    <wd:All_Locations>0</wd:All_Locations>
                    <wd:Exclude_from_Recommendations>1</wd:Exclude_from_Recommendations>
                    <wd:Exclude_from_Search_and_Browse>0</wd:Exclude_from_Search_and_Browse>
                    <wd:Disable_Express_Interest>1</wd:Disable_Express_Interest>
                    <wd:Course_Lesson_Data>
                        <wd:Lesson_Order>1</wd:Lesson_Order>
                        <wd:Make_Lesson_Mandatory>1</wd:Make_Lesson_Mandatory>
                        <wd:Instructor_Led_Webinar_Lesson_Data>
                            <wd:Title>Launch Class Link - HDU_VILT01 - 1</wd:Title>
                            <wd:Webinar_Lesson_Unit_Track_Attendance>1</wd:Webinar_Lesson_Unit_Track_Attendance>
                            <wd:Webinar_Lesson_Unit_Track_Grades>0</wd:Webinar_Lesson_Unit_Track_Grades>
                        </wd:Instructor_Led_Webinar_Lesson_Data>
                    </wd:Course_Lesson_Data>
                    <wd:Course_Lesson_Data>
                        <wd:Lesson_Order>2</wd:Lesson_Order>
                        <wd:Make_Lesson_Mandatory>1</wd:Make_Lesson_Mandatory>
                        <wd:Instructor_Led_Webinar_Lesson_Data>
                            <wd:Title>Launch Class Link - HDU_VILT01 - 2</wd:Title>
                            <wd:Webinar_Lesson_Unit_Track_Attendance>1</wd:Webinar_Lesson_Unit_Track_Attendance>
                            <wd:Webinar_Lesson_Unit_Track_Grades>0</wd:Webinar_Lesson_Unit_Track_Grades>
                        </wd:Instructor_Led_Webinar_Lesson_Data>
                    </wd:Course_Lesson_Data>
                    <wd:Course_Lesson_Data>
                        <wd:Lesson_Order>3</wd:Lesson_Order>
                        <wd:Make_Lesson_Mandatory>1</wd:Make_Lesson_Mandatory>
                        <wd:Instructor_Led_Webinar_Lesson_Data>
                            <wd:Title>Launch Class Link - HDU_VILT01 - 3</wd:Title>
                            <wd:Webinar_Lesson_Unit_Track_Attendance>1</wd:Webinar_Lesson_Unit_Track_Attendance>
                            <wd:Webinar_Lesson_Unit_Track_Grades>0</wd:Webinar_Lesson_Unit_Track_Grades>
                        </wd:Instructor_Led_Webinar_Lesson_Data>
                    </wd:Course_Lesson_Data>
                    <wd:Course_Lesson_Data>
                        <wd:Lesson_Order>4</wd:Lesson_Order>
                        <wd:Make_Lesson_Mandatory>1</wd:Make_Lesson_Mandatory>
                        <wd:Instructor_Led_Webinar_Lesson_Data>
                            <wd:Title>Launch Class Link - HDU_VILT01 - 4</wd:Title>
                            <wd:Webinar_Lesson_Unit_Track_Attendance>1</wd:Webinar_Lesson_Unit_Track_Attendance>
                            <wd:Webinar_Lesson_Unit_Track_Grades>0</wd:Webinar_Lesson_Unit_Track_Grades>
                        </wd:Instructor_Led_Webinar_Lesson_Data>
                    </wd:Course_Lesson_Data>
                    <wd:Course_Lesson_Data>
                        <wd:Lesson_Order>5</wd:Lesson_Order>
                        <wd:Make_Lesson_Mandatory>1</wd:Make_Lesson_Mandatory>
                        <wd:Instructor_Led_Webinar_Lesson_Data>
                            <wd:Title>Launch Class Link - HDU_VILT01 - 5</wd:Title>
                            <wd:Webinar_Lesson_Unit_Track_Attendance>1</wd:Webinar_Lesson_Unit_Track_Attendance>
                            <wd:Webinar_Lesson_Unit_Track_Grades>0</wd:Webinar_Lesson_Unit_Track_Grades>
                        </wd:Instructor_Led_Webinar_Lesson_Data>
                    </wd:Course_Lesson_Data>
                    <wd:Course_Lesson_Data>
                        <wd:Lesson_Order>6</wd:Lesson_Order>
                        <wd:Make_Lesson_Mandatory>1</wd:Make_Lesson_Mandatory>
                        <wd:Instructor_Led_Webinar_Lesson_Data>
                            <wd:Title>Launch Class Link - HDU_VILT01 - 6</wd:Title>
                            <wd:Webinar_Lesson_Unit_Track_Attendance>1</wd:Webinar_Lesson_Unit_Track_Attendance>
                            <wd:Webinar_Lesson_Unit_Track_Grades>0</wd:Webinar_Lesson_Unit_Track_Grades>
                        </wd:Instructor_Led_Webinar_Lesson_Data>
                    </wd:Course_Lesson_Data>
                    <wd:Course_Lesson_Data>
                        <wd:Lesson_Order>7</wd:Lesson_Order>
                        <wd:Make_Lesson_Mandatory>1</wd:Make_Lesson_Mandatory>
                        <wd:Media_Lesson_Data>
                            <wd:Media_Reference>
                                <wd:ID wd:type="WID">88388986cbbf10128d2b23247d3f0004</wd:ID>
                                <wd:ID wd:type="Media_ID">MEDIA-6-4195</wd:ID>
                                <wd:ID wd:type="Workdrive_Item_ID">MEDIA-6-4195</wd:ID>
                            </wd:Media_Reference>
                            <wd:Learning_Course_Lesson_Title>Welcome to HDU Distance
                                Learning</wd:Learning_Course_Lesson_Title>
                            <wd:Provide_Course_Grade>0</wd:Provide_Course_Grade>
                        </wd:Media_Lesson_Data>
                    </wd:Course_Lesson_Data>
                    <wd:Course_Lesson_Data>
                        <wd:Lesson_Order>8</wd:Lesson_Order>
                        <wd:Make_Lesson_Mandatory>0</wd:Make_Lesson_Mandatory>
                        <wd:Media_Lesson_Data>
                            <wd:Media_Reference>
                                <wd:ID wd:type="WID">358e28e73d5d10109aef5b042fc20000</wd:ID>
                                <wd:ID wd:type="Media_ID">cninv000000000020562.zip</wd:ID>
                                <wd:ID wd:type="Workdrive_Item_ID">cninv000000000020562.zip</wd:ID>
                            </wd:Media_Reference>
                            <wd:Learning_Course_Lesson_Title>Course Feedback
                                HDUv3</wd:Learning_Course_Lesson_Title>
                            <wd:Provide_Course_Grade>0</wd:Provide_Course_Grade>
                        </wd:Media_Lesson_Data>
                    </wd:Course_Lesson_Data>
                </wd:Learning_Course_Data>
            </wd:Learning_Course>
        </CourseDetails>
    </Offering>
    <Offering>
        <Lesson>
            <Offering-ID>00170024CA - 2022 Group 12 - Thurs 10AM-1PM EST</Offering-ID>
            <Course-Number>00170024CA</Course-Number>
            <Webinar>Y</Webinar>
            <Title>Session 4</Title>
            <Facilitator-LDAP>TXB786</Facilitator-LDAP>
            <Webinar-URL>https://hdu-canada.adobeconnect.com/design_basics_2/</Webinar-URL>
            <Location>Laval</Location>
            <Room/>
            <InstructorID>713400041</InstructorID>
            <MappedLocation>7070</MappedLocation>
            <MappedRoom/>
        </Lesson>
        <Lesson>
            <Offering-ID>00170024CA - 2022 Group 12 - Thurs 10AM-1PM EST</Offering-ID>
            <Course-Number>00170024CA</Course-Number>
            <Webinar>Y</Webinar>
            <Title>Session 5</Title>
            <Facilitator-LDAP>ZXF99S</Facilitator-LDAP>
            <Webinar-URL>https://hdu-canada.adobeconnect.com/design_basics_2/</Webinar-URL>
            <Location>Laval</Location>
            <Room/>
            <InstructorID/>
            <MappedLocation>7070</MappedLocation>
            <MappedRoom/>
            <Error>Instructor ID not found for Workday account ZXF99S. </Error>
        </Lesson>
        <CourseDetails>
            <wd:Learning_Course xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                <wd:Learning_Blended_Course_Reference>
                    <wd:ID wd:type="WID">88388986cbbf10128d2c78dc802e0001</wd:ID>
                    <wd:ID wd:type="Learning_Course_ID">00170024CA</wd:ID>
                    <wd:ID wd:type="Learning_Course">Design Basics (Designer Training 4) - Virtual
                        ILT</wd:ID>
                </wd:Learning_Blended_Course_Reference>
                <wd:Learning_Course_Data>
                    <wd:ID>00170024CA</wd:ID>
                    <wd:Effective_Date>2019-02-26</wd:Effective_Date>
                    <wd:Inactive>0</wd:Inactive>
                    <wd:Course_Title>Design Basics (Designer Training 4) - Virtual
                        ILT</wd:Course_Title>
                    <wd:Description>&lt;p&gt;In this 5 module virtual course the designer will learn
                        the basics of drafting and laying out basic designs for Contractors and DIY
                        customers.&lt;/p&gt;</wd:Description>
                    <wd:Course_Number>00170024CA</wd:Course_Number>
                    <wd:Topic_Reference>
                        <wd:ID wd:type="WID">9d1f49654c8310154e4430e071ef0000</wd:ID>
                        <wd:ID wd:type="Learning_Topic">Customer Service and Selling Skills -
                            CAN</wd:ID>
                    </wd:Topic_Reference>
                    <wd:Language_Reference>
                        <wd:ID wd:type="WID">da5948c0446c11de98360015c5e6daf6</wd:ID>
                        <wd:ID wd:type="User_Language_ID">en_CA</wd:ID>
                    </wd:Language_Reference>
                    <wd:Minimum_Enrollment_Capacity>4</wd:Minimum_Enrollment_Capacity>
                    <wd:Maximum_Enrollment_Capacity>10</wd:Maximum_Enrollment_Capacity>
                    <wd:Waitlist_Capacity>0</wd:Waitlist_Capacity>
                    <wd:Unlimited_Capacity>0</wd:Unlimited_Capacity>
                    <wd:Learning_Pricing_Data>
                        <wd:Pricing_Enabled>0</wd:Pricing_Enabled>
                        <wd:Price_in_Training_Credits>0</wd:Price_in_Training_Credits>
                    </wd:Learning_Pricing_Data>
                    <wd:Time_Value_Reference>
                        <wd:ID wd:type="WID">f31be4fd5caa10001d7c6231be3b4927</wd:ID>
                        <wd:ID wd:type="Learning_Time_Unit_ID">MINUTES</wd:ID>
                    </wd:Time_Value_Reference>
                    <wd:Total_Course_Duration>900</wd:Total_Course_Duration>
                    <wd:Enable_Auto_Enrollment_from_Waitlist>0</wd:Enable_Auto_Enrollment_from_Waitlist>
                    <wd:Legacy_Course>1</wd:Legacy_Course>
                    <wd:Allowed_Instructor_Reference>
                        <wd:ID wd:type="WID">88388986cbbf10124df5cfa198ca0000</wd:ID>
                        <wd:ID wd:type="Learning_Instructor_ID">718700459</wd:ID>
                    </wd:Allowed_Instructor_Reference>
                    <wd:Allowed_Instructor_Reference>
                        <wd:ID wd:type="WID">88388986cbbf10124df5c99611170000</wd:ID>
                        <wd:ID wd:type="Learning_Instructor_ID">713400041</wd:ID>
                    </wd:Allowed_Instructor_Reference>
                    <wd:Allowed_Instructor_Reference>
                        <wd:ID wd:type="WID">88388986cbbf10124df5c72adbc60000</wd:ID>
                        <wd:ID wd:type="Learning_Instructor_ID">702300581</wd:ID>
                    </wd:Allowed_Instructor_Reference>
                    <wd:All_Locations>0</wd:All_Locations>
                    <wd:Exclude_from_Recommendations>1</wd:Exclude_from_Recommendations>
                    <wd:Exclude_from_Search_and_Browse>0</wd:Exclude_from_Search_and_Browse>
                    <wd:Disable_Express_Interest>1</wd:Disable_Express_Interest>
                    <wd:Course_Lesson_Data>
                        <wd:Lesson_Order>1</wd:Lesson_Order>
                        <wd:Make_Lesson_Mandatory>1</wd:Make_Lesson_Mandatory>
                        <wd:Instructor_Led_Webinar_Lesson_Data>
                            <wd:Title>Session 1</wd:Title>
                            <wd:Webinar_Lesson_Unit_Track_Attendance>1</wd:Webinar_Lesson_Unit_Track_Attendance>
                            <wd:Webinar_Lesson_Unit_Track_Grades>0</wd:Webinar_Lesson_Unit_Track_Grades>
                        </wd:Instructor_Led_Webinar_Lesson_Data>
                    </wd:Course_Lesson_Data>
                    <wd:Course_Lesson_Data>
                        <wd:Lesson_Order>2</wd:Lesson_Order>
                        <wd:Make_Lesson_Mandatory>1</wd:Make_Lesson_Mandatory>
                        <wd:Instructor_Led_Webinar_Lesson_Data>
                            <wd:Title>Session 2</wd:Title>
                            <wd:Webinar_Lesson_Unit_Track_Attendance>1</wd:Webinar_Lesson_Unit_Track_Attendance>
                            <wd:Webinar_Lesson_Unit_Track_Grades>0</wd:Webinar_Lesson_Unit_Track_Grades>
                        </wd:Instructor_Led_Webinar_Lesson_Data>
                    </wd:Course_Lesson_Data>
                    <wd:Course_Lesson_Data>
                        <wd:Lesson_Order>3</wd:Lesson_Order>
                        <wd:Make_Lesson_Mandatory>1</wd:Make_Lesson_Mandatory>
                        <wd:Instructor_Led_Webinar_Lesson_Data>
                            <wd:Title>Session 3</wd:Title>
                            <wd:Webinar_Lesson_Unit_Track_Attendance>1</wd:Webinar_Lesson_Unit_Track_Attendance>
                            <wd:Webinar_Lesson_Unit_Track_Grades>0</wd:Webinar_Lesson_Unit_Track_Grades>
                        </wd:Instructor_Led_Webinar_Lesson_Data>
                    </wd:Course_Lesson_Data>
                    <wd:Course_Lesson_Data>
                        <wd:Lesson_Order>4</wd:Lesson_Order>
                        <wd:Make_Lesson_Mandatory>1</wd:Make_Lesson_Mandatory>
                        <wd:Instructor_Led_Webinar_Lesson_Data>
                            <wd:Title>Session 4</wd:Title>
                            <wd:Webinar_Lesson_Unit_Track_Attendance>1</wd:Webinar_Lesson_Unit_Track_Attendance>
                            <wd:Webinar_Lesson_Unit_Track_Grades>0</wd:Webinar_Lesson_Unit_Track_Grades>
                        </wd:Instructor_Led_Webinar_Lesson_Data>
                    </wd:Course_Lesson_Data>
                    <wd:Course_Lesson_Data>
                        <wd:Lesson_Order>5</wd:Lesson_Order>
                        <wd:Make_Lesson_Mandatory>1</wd:Make_Lesson_Mandatory>
                        <wd:Instructor_Led_Webinar_Lesson_Data>
                            <wd:Title>Session 5</wd:Title>
                            <wd:Webinar_Lesson_Unit_Track_Attendance>1</wd:Webinar_Lesson_Unit_Track_Attendance>
                            <wd:Webinar_Lesson_Unit_Track_Grades>0</wd:Webinar_Lesson_Unit_Track_Grades>
                        </wd:Instructor_Led_Webinar_Lesson_Data>
                    </wd:Course_Lesson_Data>
                    <wd:Course_Lesson_Data>
                        <wd:Lesson_Order>6</wd:Lesson_Order>
                        <wd:Make_Lesson_Mandatory>1</wd:Make_Lesson_Mandatory>
                        <wd:Media_Lesson_Data>
                            <wd:Media_Reference>
                                <wd:ID wd:type="WID">88388986cbbf10128d2c770c4c2b0003</wd:ID>
                                <wd:ID wd:type="Media_ID">MEDIA-6-4222</wd:ID>
                                <wd:ID wd:type="Workdrive_Item_ID">MEDIA-6-4222</wd:ID>
                            </wd:Media_Reference>
                            <wd:Learning_Course_Lesson_Title>Log in Instructions (Adobe Connect -
                                Designer 4_DesignBasics) (updated May
                                2022)</wd:Learning_Course_Lesson_Title>
                            <wd:Provide_Course_Grade>0</wd:Provide_Course_Grade>
                        </wd:Media_Lesson_Data>
                    </wd:Course_Lesson_Data>
                    <wd:Course_Lesson_Data>
                        <wd:Lesson_Order>7</wd:Lesson_Order>
                        <wd:Make_Lesson_Mandatory>0</wd:Make_Lesson_Mandatory>
                        <wd:Media_Lesson_Data>
                            <wd:Media_Reference>
                                <wd:ID wd:type="WID">358e28e73d5d1010910342e9c79a0000</wd:ID>
                                <wd:ID wd:type="Media_ID">cninv000000000019226.zip</wd:ID>
                                <wd:ID wd:type="Workdrive_Item_ID">cninv000000000019226.zip</wd:ID>
                            </wd:Media_Reference>
                            <wd:Learning_Course_Lesson_Title>Course Evaluation - vILT
                                (L1)</wd:Learning_Course_Lesson_Title>
                            <wd:Provide_Course_Grade>0</wd:Provide_Course_Grade>
                        </wd:Media_Lesson_Data>
                    </wd:Course_Lesson_Data>
                </wd:Learning_Course_Data>
            </wd:Learning_Course>
        </CourseDetails>
    </Offering>
</OfferingsWithCourse>

CodePudding user response:

<?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:map="http://www.w3.org/2005/xpath-functions/map"
  xmlns:wd="urn:com.workday/bsvc"
  exclude-result-prefixes="#all"
  expand-text="yes"
  version="3.0">
  
  <xsl:output method="xml" indent="yes"/>
  
  <xsl:accumulator name="location-internal-value" as="xs:string?" initial-value="()" streamable="yes">
      <xsl:accumulator-rule match="Maps/Locations/Location" select="()"/>
      <xsl:accumulator-rule match="Maps/Locations/Location/InternalValue/text()" select="."/>
  </xsl:accumulator>
  
  <xsl:accumulator name="location-map" as="map(xs:string, xs:string)" initial-value="map{}" streamable="yes">
      <xsl:accumulator-rule match="Maps/Locations" select="map{}"/>
      <xsl:accumulator-rule match="Maps/Locations/Location/ExternalValue/text()" 
          select="map:put($value, string(), accumulator-before('location-internal-value'))"/>
  </xsl:accumulator>
  
  <xsl:accumulator name="Room-internal-value" as="xs:string?" initial-value="()" streamable="yes">
      <xsl:accumulator-rule match="Maps/Rooms/Room" select="()"/>
      <xsl:accumulator-rule match="Maps/Rooms/Room/InternalValue/text()" select="."/>
  </xsl:accumulator>
  
  <xsl:accumulator name="Room-map" as="map(xs:string, xs:string)" initial-value="map{}" streamable="yes">
      <xsl:accumulator-rule match="Maps/Rooms" select="map{}"/>
      <xsl:accumulator-rule match="Maps/Rooms/Room/ExternalValue/text()" 
          select="map:put($value, string(), accumulator-before('Room-internal-value'))"/>
  </xsl:accumulator>
  
  <xsl:accumulator name="Instructor-internal-value" as="xs:string?" initial-value="()" streamable="yes">
      <xsl:accumulator-rule match="Instructors/Instructor" select="()"/>
      <xsl:accumulator-rule match="Instructors/Instructor/InstructorID/text()" select="."/>
  </xsl:accumulator>
  
  <xsl:accumulator name="Instructor-map" as="map(xs:string, xs:string)" initial-value="map{}" streamable="yes">
      <xsl:accumulator-rule match="Instructors" select="map{}"/>
      <xsl:accumulator-rule match="Instructors/Instructor/WorkdayUserName/text()" 
          select="map:put($value, string(), accumulator-before('Instructor-internal-value'))"/>
  </xsl:accumulator>
  
  <xsl:accumulator name="learning-course-id" as="xs:string?" initial-value="()" streamable="yes">
      <xsl:accumulator-rule match="AllCourses/wd:Learning_Course/wd:Learning_Course_Data/wd:ID/text()" select="string()"/>
  </xsl:accumulator>
  
  <xsl:accumulator name="lesson-mandatory" as="xs:boolean?" initial-value="()" streamable="yes">
      <xsl:accumulator-rule match="AllCourses/wd:Learning_Course/wd:Learning_Course_Data/wd:Course_Lesson_Data" select="false()"/>
      <xsl:accumulator-rule match="AllCourses/wd:Learning_Course/wd:Learning_Course_Data/wd:Course_Lesson_Data/wd:Make_Lesson_Mandatory/text()" select=". = 1"/>
  </xsl:accumulator>
  
  <xsl:accumulator name="webinars" as="map(xs:string, xs:string*)" initial-value="map{}" streamable="yes">
      <xsl:accumulator-rule match="AllCourses/wd:Learning_Course/wd:Learning_Course_Data/wd:Course_Lesson_Data/wd:Instructor_Led_Webinar_Lesson_Data[accumulator-before('lesson-mandatory')]/wd:Title/text()" 
          select="let $id := accumulator-before('learning-course-id')
          return
          if (map:contains($value, $id))
          then map:put($value, $id, ($value($id), string()))
          else map:put($value, $id, string())"/>
  </xsl:accumulator>
  <xsl:accumulator name="classroom-lessons" as="map(xs:string, xs:string*)" initial-value="map{}" streamable="yes">
      <xsl:accumulator-rule match="AllCourses/wd:Learning_Course/wd:Learning_Course_Data/wd:Course_Lesson_Data/wd:Instructor_Led_Lesson_Data[accumulator-before('lesson-mandatory')]/wd:Title/text()" 
          select="let $id := accumulator-before('learning-course-id')
          return
          if (map:contains($value, $id))
          then map:put($value, $id, ($value($id), string()))
          else map:put($value, $id, string())"/>
  </xsl:accumulator>
  
  <xsl:accumulator name="offered-lesson-id" as="xs:string?" initial-value="()" streamable="yes">
      <xsl:accumulator-rule match="Offerings/Offering/Lesson/Course-Number/text()" select="string()"/>
  </xsl:accumulator>
  <xsl:accumulator name="offered-lessons" as="map(xs:string, xs:string*)" initial-value="map{}" streamable="yes">
      <xsl:accumulator-rule match="Offerings/Offering" select="map{}"/>
      <xsl:accumulator-rule match="Offerings/Offering/Lesson/Title/text()" 
          select="let $id := accumulator-before('offered-lesson-id')
          return
          if (map:contains($value, $id))
          then map:put($value, $id, ($value($id), string()))
          else map:put($value, $id, string())"/>
  </xsl:accumulator>
  
  <xsl:accumulator name="match-courses" as="map(xs:string, xs:string*)" initial-value="map{}" streamable="yes">
      <xsl:accumulator-rule match="AllCourses/wd:Learning_Course/wd:Learning_Course_Data/wd:ID/text()"
          select="let $id := accumulator-before('offered-lesson-id')
          return
          if (map:contains($value, $id))
          then map:put($value, $id, ($value($id), string()))
          else map:put($value, $id, string())"/>
  </xsl:accumulator>
  
  
  
  <xsl:mode on-no-match="shallow-copy" use-accumulators="#all" streamable="yes"/>
  
  <xsl:output method="xml" indent="yes"/>
  
  <xsl:template match="/FileAndCourses" expand-text="yes">
      <OfferingsWithCourse>
          <xsl:apply-templates select="Offerings/Offering"/>
      </OfferingsWithCourse>
  </xsl:template>
  
  <xsl:template match="Offering">
      <xsl:copy>
          <xsl:apply-templates/>
          <xsl:variable name="webinars" select="accumulator-after('webinars')"/>
          <xsl:variable name="offered-lessons" select="accumulator-after('offered-lessons')"/>
          <xsl:for-each select="map:keys(accumulator-after('webinars'))[. = map:keys($offered-lessons)]">
              <xsl:variable name="webinar-id" select="."/>
              <xsl:for-each select="$webinars(.)[not(. = $offered-lessons($webinar-id))]">
                  <Error>No matching required lesson found for title {.} in Course num {$webinar-id}</Error>
              </xsl:for-each>
          </xsl:for-each>
<!--            Trying to insert Child node in Offering-->
          <xsl:variable name="match-courses" select="accumulator-after('match-courses')"/>
          <xsl:for-each select="map:keys(accumulator-after('match-courses'))">
              <CourseDetails>
                  <xsl:copy-of select="Lesson[1]/Course-Number"/> 
              </CourseDetails>
          </xsl:for-each>
      </xsl:copy>
      
  </xsl:template>
  
  <xsl:template match="Lesson">
      <xsl:apply-templates select="copy-of()" mode="grounded"/>
  </xsl:template>
  
  <xsl:template match="Offering/Lesson">
      <xsl:apply-templates select="copy-of()" mode="grounded"/>
  </xsl:template>
  
  <xsl:mode name="grounded" on-no-match="shallow-copy"/>
  <!-- Validation Errors for Lessons --> 
  <xsl:template mode="grounded" match="Lesson">
      <xsl:copy>
          <xsl:apply-templates mode="#current"/>
          <xsl:if test="Course-Number = ''">
              <Error>
                  <xsl:text>Missing required field Course Number. </xsl:text>                                    
              </Error>
          </xsl:if>                         
          <xsl:if test="Facilitator-LDAP = ''">
              <Error>
                  <xsl:text>Missing required field Facilitator LDAP for at least one of the lessons in the offering. </xsl:text>                                    
              </Error>
          </xsl:if> 
          <xsl:if test="Offering-ID = ''">
              <Error>
                  <xsl:text>Missing required field Offering ID. </xsl:text>                                    
              </Error>
          </xsl:if>  
          <xsl:if test="Start-Date = ''">
              <Error>
                  <xsl:text>Missing required field Start Date for at least one of the lessons in the offering. </xsl:text>                                    
              </Error>
          </xsl:if>
          <xsl:if test="Start-Time = ''">
              <Error>
                  <xsl:text>Missing required field Start Time for at least one of the lessons in the offering. </xsl:text>                                    
              </Error>
          </xsl:if>
          <xsl:if test="End-Time = ''">
              <Error>
                  <xsl:text>Missing required field End Time for at least one of the lessons in the offering. </xsl:text>                                    
              </Error>
          </xsl:if>
          <xsl:if test="Title = ''">
              <Error>
                  <xsl:text>Missing required field Title for at least one of the lessons in the offering. </xsl:text>                                    
              </Error>
          </xsl:if>  
          <xsl:if test="Webinar = 'Y' and Webinar-URL = ''">
              <Error>
                  <xsl:text>Missing required field Webinar URL for webinar type lesson. </xsl:text>                                    
              </Error>
          </xsl:if>  
          <xsl:if test="Webinar != 'Y' and Location = ''">
              <Error>
                  <xsl:text>Missing required field location for classroom type lesson. </xsl:text>                                    
              </Error>
          </xsl:if>   
          <!-- Validation errors for Offering: validating same value for min seat, max seat, and language -->
          <xsl:if test="Lesson/Min-Seats != Lesson/Min-Seats">
              <Error>
                  <xsl:text>Different values found for field Min Seats among the lessons. Min Seat should be the same for all the lessons within the same offering.</xsl:text>                          
              </Error>
          </xsl:if>
          <xsl:if test="Lesson/Max-seats != Lesson/Max-seats">
              <Error>
                  <xsl:text>Different values found for field Max Seats among the lessons. Max Seat should be the same for all the lessons within the same offering.</xsl:text>                           
              </Error>
          </xsl:if>
          <xsl:if test="Lesson/Language != Lesson/Language">
              <Error>
                  <xsl:text>Different values found for field Language among the lessons. Language should be the same for all the lessons within the same offering.</xsl:text>                           
              </Error>
          </xsl:if>
          
      </xsl:copy>
  </xsl:template>
  <!--    Mapped location-->
  <xsl:template mode="grounded" match="Lesson/Location[normalize-space() and not(map:contains(accumulator-before('location-map'), string()))]">
      <xsl:next-match/>
      <Error>Mapped location not found for {.}</Error>
  </xsl:template>
  
  <xsl:template mode="grounded" match="Lesson/Location[normalize-space() and map:contains(accumulator-before('location-map'), string())]">
      <xsl:next-match/>
      <MappedLocation>{accumulator-before('location-map')(.)}</MappedLocation>
  </xsl:template>
  <!--    Mapped Room-->
  <xsl:template mode="grounded" match="Lesson/Room[normalize-space() and not(map:contains(accumulator-before('Room-map'), string()))]">
      <xsl:next-match/>
      <Error>Mapped Room not found for {.}</Error>
  </xsl:template>
  
  <xsl:template mode="grounded" match="Lesson/Room[normalize-space() and map:contains(accumulator-before('Room-map'), string())]">
      <xsl:next-match/>
      <MappedRoom>{accumulator-before('Room-map')(.)}</MappedRoom>
  </xsl:template>
  <!--    Mapped Instructor -->
  <xsl:template mode="grounded" match="Lesson/Facilitator-LDAP[normalize-space() and not(map:contains(accumulator-before('Instructor-map'), string()))]">
      <xsl:next-match/>
      <Error>Mapped Instructor not found for {.}</Error>
  </xsl:template>
  
  <xsl:template mode="grounded" match="Lesson/Facilitator-LDAP[normalize-space() and map:contains(accumulator-before('Instructor-map'), string())]">
      <xsl:next-match/>
      <MappedInstructor>{accumulator-before('Instructor-map')(.)}</MappedInstructor>
  </xsl:template>
  <!--    No intructor-led webinar lesson found in course with title:-->
  <xsl:template mode="grounded" match="Lesson[Webinar = 'Y'][let $webinars := accumulator-before('webinars') return if (map:contains($webinars, Course-Number)) then not(Title = $webinars(Course-Number)) else false()]">
      <xsl:copy>
          <xsl:copy-of select="*"/>
          <Error>
              <xsl:text>No intructor-led webinar lesson found in course with title: </xsl:text>
              <xsl:value-of select="Title"/>
              <xsl:text>. </xsl:text>
          </Error>
      </xsl:copy>
  </xsl:template>
  <!--    No intructor-led classrom lesson found in course with title:-->
  <xsl:template mode="grounded" match="Lesson[Webinar = 'N'][let $lessons := accumulator-before('classroom-lessons') return if (map:contains($lessons, Course-Number)) then not(Title = $lessons(Course-Number)) else false()]">
      <xsl:copy>
          <xsl:copy-of select="*"/>
          <Error>
              <xsl:text>No intructor-led classrom lesson found in course with title: </xsl:text>
              <xsl:value-of select="Title"/>
              <xsl:text>. </xsl:text>
          </Error>
      </xsl:copy>
  </xsl:template>
  <!--    Unable to find active course-->
  <xsl:template mode="grounded" match="Lesson[Webinar = 'Y'][not(map:contains(accumulator-before('webinars'), Course-Number))] | Lesson[Webinar = 'N'][not(map:contains(accumulator-before('classroom-lessons'), Course-Number))]">
      <xsl:copy>
          <xsl:copy-of select="*"/>
          <Error>
              <xsl:text>Unable to find active course </xsl:text>
              <xsl:value-of select="Course-Number"/>
              <xsl:text> in Workday. </xsl:text>
          </Error>
      </xsl:copy>
  </xsl:template>
  
  <xsl:template mode="grounded" match="Lesson[Webinar = 'Y'][not(map:contains(accumulator-before('webinars'), Course-Number))] | Lesson[Webinar = 'N'][not(map:contains(accumulator-before('classroom-lessons'), Course-Number))]">
      <xsl:copy>
          <xsl:copy-of select="*"/>
          
      </xsl:copy>
  </xsl:template>
</xsl:stylesheet> 

CodePudding user response:

A capturing accumulator for your data would be (with xmlns:saxon="http://saxon.sf.net/" declared on the stylesheet)

<xsl:accumulator name="learning-courses" as="element(wd:Learning_Course)*" initial-value="()" streamable="yes">
  <xsl:accumulator-rule saxon:capture="yes" match="AllCourses/wd:Learning_Course" phase="end" select="$value, ."/>
</xsl:accumulator>

then you could change the template matching Offering to

<xsl:template match="Offering">
    <xsl:copy>
        <xsl:apply-templates/>
        <xsl:variable name="webinars" select="accumulator-after('webinars')"/>
        <xsl:variable name="offered-lessons" select="accumulator-after('offered-lessons')"/>
        <xsl:for-each select="map:keys(accumulator-after('webinars'))[. = map:keys($offered-lessons)]">
            <xsl:variable name="webinar-id" select="."/>
            <xsl:for-each select="$webinars(.)[not(. = $offered-lessons($webinar-id))]">
                <Error>No matching required lesson found for title {.} in Course num {$webinar-id}</Error>
            </xsl:for-each>
        </xsl:for-each>
        <xsl:where-populated>
          <CourseDetails>
            <xsl:copy-of select="accumulator-after('learning-courses')[wd:Learning_Course_Data/wd:ID = map:keys($offered-lessons)]"/>
          </CourseDetails>
        </xsl:where-populated>
    </xsl:copy>
</xsl:template>
  • Related