title: XML to CSV: Print lines for each child node and sometimes child node is missing
body: <p>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:</p>
  
  <p>Ambiguous rule match for /Report_Data/Report_Entry[2]
  Matches both "Report_Data/Report_Entry[not(Support_Order_Detail)]" 
  and "Report_Data/Report_Entry"</p>
  
  <p>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.</p>
  
  <pre><code>Doe,Jane,Child Support,Mandatory,12345
  Doe,Jane,Child Support,Mandatory,12345
  Dole,Bob,Student Loan,Federal,56789
  </code></pre>
  
  <p>.</p>
  
  <pre><code>    &lt;Report_Data&gt;
        &lt;Report_Entry&gt;
            &lt;Worker&gt;
                &lt;Last_Name&gt;Doe&lt;/Last_Name&gt;
                &lt;First_Name&gt;Jane&lt;/First_Name&gt;
            &lt;/Worker&gt;
            &lt;Lien_Type&gt;Support Order&lt;/Lien_Type&gt;
            &lt;Lien_Sub_Type&gt;Mandatory&lt;/Lien_Sub_Type&gt;
            &lt;Support_Order_Detail&gt;
                &lt;Support_Type Descriptor="Current Child Support"&gt;
                    &lt;ID type="Support_Type"&gt;CS&lt;/ID&gt;
                &lt;/Support_Type&gt;
            &lt;/Support_Order_Detail&gt;
            &lt;Support_Order_Detail&gt;
                &lt;Support_Type Descriptor="Past Due Child Support"&gt;
                    &lt;ID type="Support_Type"&gt;PDCS&lt;/ID&gt;
                &lt;/Support_Type&gt;
            &lt;/Support_Order_Detail&gt;
            &lt;Case_ID&gt;12345&lt;/Case_ID&gt;
        &lt;/Report_Entry&gt;
        &lt;Report_Entry&gt;
            &lt;Worker&gt;
                &lt;Last_Name&gt;Dole&lt;/Last_Name&gt;
                &lt;First_Name&gt;Bob&lt;/First_Name&gt;
            &lt;/Worker&gt;
            &lt;Lien_Type&gt;Student Loan&lt;/Lien_Type&gt;
            &lt;Lien_Sub_Type&gt;Federal&lt;/Lien_Sub_Type&gt;
            &lt;Case_ID&gt;56789&lt;/wd:Case_ID&gt;
        &lt;/Report_Entry&gt;
      &lt;/Report_Data&gt;
  </code></pre>
  
  <p>XSLT:</p>
  
  <pre><code>    &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
       exclude-result-prefixes="xs"
       xmlns:xtt="urn:com.workday/xtt"
       version="2.0"&gt;
  
  
      &lt;xsl:template match="/"&gt;
          &lt;File xtt:separator="&amp;#xd;&amp;#xa;"&gt;
              &lt;xtt:class xtt:name="dateformat" xtt:dateFormat="dd/MM/yyyy"/&gt;
              &lt;xsl:apply-templates/&gt;
          &lt;/File&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match="Report_Data/Report_Entry"&gt; 
          &lt;xsl:apply-templates select="Support_Order_Detail"/&gt;
      &lt;/xsl:template&gt;
  
  
      &lt;xsl:template match="Support_Order_Detail" &gt;
          &lt;Record xtt:separator=","&gt;
              &lt;LastName xtt:maxLength="14"&gt;&lt;xsl:value-of select="preceding-sibling::Worker/Last_Name[1]"/&gt;&lt;/LastName&gt;
              &lt;FirstName xtt:maxLength="17"&gt;&lt;xsl:value-of select="preceding-sibling::Worker/First_Name[1]"/&gt;&lt;/FirstName&gt;
              &lt;LienType&gt;
              &lt;xsl:choose&gt;
                  &lt;xsl:when test="preceding-sibling::Lien_Type[1] = 'Support Order' and Support_Type/@Descriptor = 'Current Child Support'"&gt;
                      &lt;xsl:text&gt;Child Support&lt;/xsl:text&gt;
                  &lt;/xsl:when&gt;
                  &lt;xsl:when test="preceding-sibling::Lien_Type[1] = 'Support Order' and Support_Type/@Descriptor = 'Past Due Child Support'"&gt;
                      &lt;xsl:text&gt;Child Support&lt;/xsl:text&gt;
                  &lt;/xsl:when&gt;
                  &lt;xsl:otherwise&gt;
                      &lt;xsl:value-of select="Support_Type/@Descriptor"/&gt;
                  &lt;/xsl:otherwise&gt;
              &lt;/xsl:choose&gt;
              &lt;/LienType&gt;
              &lt;LienSubType&gt;&lt;xsl:value-of select="preceding-sibling::Lien_Sub_Type[1]"/&gt;&lt;/LienSubType&gt;
              &lt;CaseID xtt:maxLength="20"&gt;&lt;xsl:value-of select="following-sibling::Case_ID"/&gt;&lt;/CaseID&gt; 
          &lt;/Record&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match="Report_Data/Report_Entry[not(Support_Order_Detail)]" &gt;
          &lt;Record xtt:separator=","&gt;
              &lt;LastName xtt:maxLength="14"&gt;&lt;xsl:value-of select="Worker/Last_Name"/&gt;&lt;/LastName&gt;
              &lt;FirstName xtt:maxLength="17"&gt;&lt;xsl:value-of select="Worker/First_Name"/&gt;&lt;/FirstName&gt;
              &lt;LienType&gt;&lt;xsl:value-of select="Lien_Type"/&gt;&lt;/LienType&gt;
              &lt;LienSubType&gt;&lt;xsl:value-of select="Lien_Sub_Type"/&gt;&lt;/LienSubType&gt;
              &lt;CaseID xtt:maxLength="20"&gt;&lt;xsl:value-of select="Case_ID"/&gt;&lt;/CaseID&gt; 
          &lt;/Record&gt;
      &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt; 
  </code></pre>
  
  <p>Expected XML results:</p>
  
  <pre><code>    &lt;Record xtt:separator=","&gt;
          &lt;LastName xtt:maxLength="14"&gt;Doe&lt;/LastName&gt;
          &lt;FirstName xtt:maxLength="17"&gt;Jane&lt;/FirstName&gt;
          &lt;LienType&gt;Child Support&lt;/LienType&gt;
          &lt;LienSubType&gt;Mandatory&lt;/LienSubType&gt;
          &lt;CaseID xtt:maxLength="20"&gt;12345&lt;/CaseID&gt;
      &lt;/Record&gt;
      &lt;Record xtt:separator=","&gt;
          &lt;LastName xtt:maxLength="14"&gt;Doe&lt;/LastName&gt;
          &lt;FirstName xtt:maxLength="17"&gt;Jane&lt;/FirstName&gt;
          &lt;LienType&gt;Child Support&lt;/LienType&gt;
          &lt;LienSubType&gt;Mandatory&lt;/LienSubType&gt;
          &lt;CaseID xtt:maxLength="20"&gt;12345&lt;/CaseID&gt;
      &lt;/Record&gt;
      &lt;Record xtt:separator=","&gt;
          &lt;LastName xtt:maxLength="14"&gt;Dole&lt;/LastName&gt;
          &lt;FirstName xtt:maxLength="17"&gt;Bob&lt;/FirstName&gt;
          &lt;LienType&gt;Student Loan&lt;/LienType&gt;
          &lt;LienSubType&gt;Federal&lt;/LienSubType&gt;
          &lt;CaseID xtt:maxLength="20"&gt;56789&lt;/CaseID&gt;
      &lt;/Record&gt;
  </code></pre>