xsltmulti-table

XSLT combine multiple list from one file to table


I need help with merging several tables of one file into one output. The files that I get processed perfectly according to the XSLT schema that is listed below. But today I received an interesting file that was not processed as I wanted.

source file

<?xml version="1.0" encoding="utf-8"?>
<extdata user="test">
  <scheme name="Order">
    <data>
      <s>
        <d name="CashOrder">
          <f name="ActionDate" type="Date" />
          <f name="AddressId" type="String" />
          <f name="CompanyId" type="String" />
        </d>
      </s>
      <o>
        <d name="CashOrder">
          <r>
            <f>2022-01-24T00:00:00</f>
            <f>10234</f>
            <f>10234</f>
          </r>
          <r>
            <f>2022-01-24T00:00:00</f>
            <f>52817</f>
            <f>52817</f>
          </r>
          <r>
            <f>2022-01-24T00:00:00</f>
            <f>58259</f>
            <f>58259</f>
          </r>
        </d>
      </o>
    </data>
    <data>
      <s>
        <d name="OrderOption">
          <f name="OrderDate" type="Date" />
          <f name="OrderNumber" type="String" />
        </d>
      </s>
      <o>
        <d name="OrderOption">
          <r>
            <f>2022-01-24T00:00:00</f>
            <f>WE30395.c17639</f>
          </r>
          <r>
            <f>2022-01-24T00:00:00</f>
            <f>WE30395.c17639</f>
          </r>
          <r>
            <f>2022-01-24T00:00:00</f>
            <f>WE30395.c17639</f>
          </r>
        </d>
      </o>
    </data>
  </scheme>
</extdata>

my XSLT (here they helped me with writing.there was a problem with the recursive processing of the hierarchy)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="xml" indent="yes" />
   <xsl:template match="/extdata/scheme">
      <ValueTable xmlns="http://v8.1c.ru/8.1/data/core" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
         <xsl:for-each select="data/s/d/f | data/s/d/d/f">
            <column>
               <Name xsi:type="xs:string">
                  <xsl:value-of select="@name" />
               </Name>
               <ValueType>
                  <xsl:if test="@type= 'String'">
                     <Type>xs:string</Type>
                     <StringQualifiers>
                        <Length>150</Length>
                        <AllowedLength>Variable</AllowedLength>
                     </StringQualifiers>
                  </xsl:if>
                  <xsl:if test="@type= 'Date'">
                     <Type>xs:dateTime</Type>
                     <DateQualifiers>
                        <DateFractions>DateTime</DateFractions>
                     </DateQualifiers>
                  </xsl:if>
                  <xsl:if test="@type= 'Decimal' or @type= 'Currency'">
                     <Type>xs:decimal</Type>
                     <NumberQualifiers>
                        <Digits>20</Digits>
                        <FractionDigits>4</FractionDigits>
                        <AllowedSign>Any</AllowedSign>
                     </NumberQualifiers>
                  </xsl:if>
                  <xsl:if test="@type= 'Integer'">
                     <Type>xs:decimal</Type>
                     <NumberQualifiers>
                        <Digits>20</Digits>
                        <FractionDigits>0</FractionDigits>
                        <AllowedSign>Any</AllowedSign>
                     </NumberQualifiers>
                  </xsl:if>
               </ValueType>
            </column>
         </xsl:for-each>
         <xsl:for-each select="data/o/d/r">
            <row>
               <xsl:for-each select=".//f">
                  <Value>
                     <xsl:value-of select="." />
                  </Value>
               </xsl:for-each>
            </row>
         </xsl:for-each>
      </ValueTable>
   </xsl:template>
</xsl:stylesheet>

output now

      <ValueTable xmlns="http://v8.1c.ru/8.1/data/core"
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <column>
      <Name xsi:type="xs:string">ActionDate</Name>
      <ValueType>
         <Type>xs:dateTime</Type>
         <DateQualifiers>
            <DateFractions>DateTime</DateFractions>
         </DateQualifiers>
      </ValueType>
   </column>
   <column>
      <Name xsi:type="xs:string">AddressId</Name>
      <ValueType>
         <Type>xs:string</Type>
         <StringQualifiers>
            <Length>150</Length>
            <AllowedLength>Variable</AllowedLength>
         </StringQualifiers>
      </ValueType>
   </column>
   <column>
      <Name xsi:type="xs:string">CompanyId</Name>
      <ValueType>
         <Type>xs:string</Type>
         <StringQualifiers>
            <Length>150</Length>
            <AllowedLength>Variable</AllowedLength>
         </StringQualifiers>
      </ValueType>
   </column>
   <column>
      <Name xsi:type="xs:string">OrderDate</Name>
      <ValueType>
         <Type>xs:dateTime</Type>
         <DateQualifiers>
            <DateFractions>DateTime</DateFractions>
         </DateQualifiers>
      </ValueType>
   </column>
   <column>
      <Name xsi:type="xs:string">OrderNumber</Name>
      <ValueType>
         <Type>xs:string</Type>
         <StringQualifiers>
            <Length>150</Length>
            <AllowedLength>Variable</AllowedLength>
         </StringQualifiers>
      </ValueType>
   </column>
   <row>
      <Value>2022-01-24T00:00:00</Value>
      <Value>10234</Value>
      <Value>10234</Value>
   </row>
   <row>
      <Value>2022-01-24T00:00:00</Value>
      <Value>52817</Value>
      <Value>52817</Value>
   </row>
   <row>
      <Value>2022-01-24T00:00:00</Value>
      <Value>58259</Value>
      <Value>58259</Value>
   </row>
   <row>
      <Value>2022-01-24T00:00:00</Value>
      <Value>WE30395.c17639</Value>
   </row>
   <row>
      <Value>2022-01-24T00:00:00</Value>
      <Value>WE30395.c17639</Value>
   </row>
   <row>
      <Value>2022-01-24T00:00:00</Value>
      <Value>WE30395.c17639</Value>
   </row>
</ValueTable>

need

<ValueTable xmlns="http://v8.1c.ru/8.1/data/core"
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <column>
      <Name xsi:type="xs:string">ActionDate</Name>
      <ValueType>
         <Type>xs:dateTime</Type>
         <DateQualifiers>
            <DateFractions>DateTime</DateFractions>
         </DateQualifiers>
      </ValueType>
   </column>
   <column>
      <Name xsi:type="xs:string">AddressId</Name>
      <ValueType>
         <Type>xs:string</Type>
         <StringQualifiers>
            <Length>150</Length>
            <AllowedLength>Variable</AllowedLength>
         </StringQualifiers>
      </ValueType>
   </column>
   <column>
      <Name xsi:type="xs:string">CompanyId</Name>
      <ValueType>
         <Type>xs:string</Type>
         <StringQualifiers>
            <Length>150</Length>
            <AllowedLength>Variable</AllowedLength>
         </StringQualifiers>
      </ValueType>
   </column>
   <column>
      <Name xsi:type="xs:string">OrderDate</Name>
      <ValueType>
         <Type>xs:dateTime</Type>
         <DateQualifiers>
            <DateFractions>DateTime</DateFractions>
         </DateQualifiers>
      </ValueType>
   </column>
   <column>
      <Name xsi:type="xs:string">OrderNumber</Name>
      <ValueType>
         <Type>xs:string</Type>
         <StringQualifiers>
            <Length>150</Length>
            <AllowedLength>Variable</AllowedLength>
         </StringQualifiers>
      </ValueType>
   </column>
   <row>
      <Value>2022-01-24T00:00:00</Value>
      <Value>10234</Value>
      <Value>10234</Value>
      <Value>2022-01-24T00:00:00</Value>
      <Value>WE30395.c17639</Value>
   </row>
   <row>
      <Value>2022-01-24T00:00:00</Value>
      <Value>52817</Value>
      <Value>52817</Value>
      <Value>2022-01-24T00:00:00</Value>
      <Value>WE30395.c17639</Value>
   </row>
   <row>
      <Value>2022-01-24T00:00:00</Value>
      <Value>58259</Value>
      <Value>58259</Value>
     <Value>2022-01-24T00:00:00</Value>
      <Value>WE30395.c17639</Value>
   </row>
 </ValueTable>

Briefly, the structure can be described as follows
I mean this is how I got it

<table>
  <header>  
    <column1>
    <column2>
    <column3>
    <column4>
    <column5>
  <header>
<row>
   value1
   value2
   value3
</row>
<row>
   value4
   value5
</row>
</table>

but it should be like this

<table>
  <header> 
   <column1>
   <column2>
   <column3>
   <column4>
   <column5>
  </header>
<row>
   value1
   value2
   value3
   value4
   value5
</row>
</table>

Solution

  • The example is rather confusing because many values are identical and it's hard to see what goes where. It's also not clear if there can be more data elements and if so, what should the result be then.

    AFAICT, this produces the expected result for the given example. For clarity, I have omitted the header part, which seems to be working fine.

    XSLT 1.0

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:output method="xml" indent="yes" />
       
    <xsl:template match="/extdata/scheme">
        <ValueTable xmlns="http://v8.1c.ru/8.1/data/core" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <!-- header -->
    
            <!-- data -->
            <xsl:for-each select="data[1]/o/d/r">
                <row>
                    <xsl:variable name="i" select="position()"/>
                    <xsl:for-each select="//data/o/d/r[$i]/f">
                        <Value>
                            <xsl:value-of select="." />
                        </Value>
                    </xsl:for-each>
                </row>
            </xsl:for-each>     
        </ValueTable>
    </xsl:template>
       
    </xsl:stylesheet>