XML : XML to CSV: Print lines for each child node and sometimes child node is missing

I need the XML below converted to csv. This was tricky for two reasons: 1) There are multiple Support_Order_Detail child elements and a separate line is needed for each child 2) There are no Support_Order_Detail child elements in the second record. I used preceding-sibling matching on Support_Order_Detail and used the not[] function to handle the record without Support_Order_Detail node. I've included the XSLT that I've struggled with and it does work. I get the correct output. However, I know there is a better way and I do get an error in my debugger:

Ambiguous rule match for /Report_Data/Report_Entry[2] Matches both "Report_Data/Report_Entry[not(Support_Order_Detail)]" and "Report_Data/Report_Entry"

I'm hoping to learn if anyone can suggest where I would not have duplicate code, i.e. two templates. I'd like to get rid of the one using the not[] function.

  Doe,Jane,Child Support,Mandatory,12345  Doe,Jane,Child Support,Mandatory,12345  Dole,Bob,Student Loan,Federal,56789        <Report_Data>        <Report_Entry>            <Worker>                <Last_Name>Doe</Last_Name>                <First_Name>Jane</First_Name>            </Worker>            <Lien_Type>Support Order</Lien_Type>            <Lien_Sub_Type>Mandatory</Lien_Sub_Type>            <Support_Order_Detail>                <Support_Type Descriptor="Current Child Support">                    <ID type="Support_Type">CS</ID>                </Support_Type>            </Support_Order_Detail>            <Support_Order_Detail>                <Support_Type Descriptor="Past Due Child Support">                    <ID type="Support_Type">PDCS</ID>                </Support_Type>            </Support_Order_Detail>            <Case_ID>12345</Case_ID>        </Report_Entry>        <Report_Entry>            <Worker>                <Last_Name>Dole</Last_Name>                <First_Name>Bob</First_Name>            </Worker>            <Lien_Type>Student Loan</Lien_Type>            <Lien_Sub_Type>Federal</Lien_Sub_Type>            <Case_ID>56789</wd:Case_ID>        </Report_Entry>      </Report_Data>    

XSLT:

      <xsl:template match="/">          <File xtt:separator="&#xd;&#xa;">              <xtt:class xtt:name="dateformat" xtt:dateFormat="dd/MM/yyyy"/>              <xsl:apply-templates/>          </File>      </xsl:template>        <xsl:template match="Report_Data/Report_Entry">           <xsl:apply-templates select="Support_Order_Detail"/>      </xsl:template>          <xsl:template match="Support_Order_Detail" >          <Record xtt:separator=",">              <SiteID xtt:maxLength="4"><xsl:value-of select="preceding-sibling::CF_ADP_Site_ID[1]"/></SiteID>              <LastName xtt:maxLength="14"><xsl:value-of select="preceding-sibling::Worker/Last_Name[1]"/></LastName>              <FirstName xtt:maxLength="17"><xsl:value-of select="preceding-sibling::Worker/First_Name[1]"/></FirstName>              <LienType>              <xsl:choose>                  <xsl:when test="preceding-sibling::Lien_Type[1] = 'Support Order' and Support_Type/@Descriptor = 'Current Child Support'">                      <xsl:text>Child Support</xsl:text>                  </xsl:when>                  <xsl:when test="preceding-sibling::Lien_Type[1] = 'Support Order' and Support_Type/@Descriptor = 'Past Due Child Support'">                      <xsl:text>Child Support</xsl:text>                  </xsl:when>                  <xsl:otherwise>                      <xsl:value-of select="Support_Type/@Descriptor"/>                  </xsl:otherwise>              </xsl:choose>              </LienType>              <LienSubType><xsl:value-of select="preceding-sibling::Lien_Sub_Type[1]"/></LienSubType>          </Record>      </xsl:template>        <xsl:template match="Report_Data/Report_Entry[not(Support_Order_Detail)]" >          <Record xtt:separator=",">              <SiteID xtt:maxLength="4"><xsl:value-of select="CF_ADP_Site_ID"/></SiteID>              <LastName xtt:maxLength="14"><xsl:value-of select="Worker/Last_Name"/></LastName>              <FirstName xtt:maxLength="17"><xsl:value-of select="Worker/First_Name"/></FirstName>              <LienType><xsl:value-of select="Lien_Type"/></LienType>              <LienSubType><xsl:value-of select="Lien_Sub_Type"/> </LienSubType>          </Record>      </xsl:template>    </xsl:stylesheet>     

No comments:

Post a Comment