How to sort the elements (columns) in xslt to tran

2019-07-21 14:24发布

<?xml version="1.0" encoding="utf-8"?>
<Report p1:schemaLocation="Customer details  http://reportserver?%2fCustomer details&amp;rs%3aFormat=XML&amp;rc%3aSchema=True" Name="Customer details" xmlns:p1="http://www.w3.org/2001/XMLSchema-instance" xmlns="Customer details">
  <table2>
   <Detail_Collection>
         <Detail Col1="aaa" col1_SeqID="2"  col1_Include="1"
                 Col2="aaa" col2_SeqID="1"  col2_Include="1"
                 Col3="aaa" col3_SeqID=""  col3_Include="0"
                 Col4="aaa" col4_SeqID="4"  col4_Include="1"
                 Col5="aaa" col5_SeqID=""  col5_Include="0"
                 ... ... ...
                 ... ... ...
                 ... ... ...
                 Col50="aaa" col50_SeqID="3"  col50_Include="1"
                 />
   <Detail_Collection>
</table2>
</Report>

The above xml is produced by SSRS for the RDL file. I want to transform the above xml file to CSV format using XSLT (customized format). The RDL file (SSRS report) is very simple with 50 columns, and displays the data for all the columns depending on the user selection on the user interface. The user interface has got the parameter selection for all the 50 columns (i.e they can select the order of the column, they can select a particular column to be included on the report or not, the fontstyle etc...). As mentioned the each column has 2 main functionalities i.e. they can be sorted and as well ordered by based on the selections.

For example from the report output i.e in the xml format given above you will see all the 50 columns exist on the xml format but I am also including the extra fiedls which are generally hided on the report.

The col1 is included on the report and is ordered (seqID) as the 2nd column on the csv file. The col2 is also included on the report and is ordered as the 1st column on the csv file. The col3 is not included on the report and the order selection is empty, so this is not included on the csv file. ... ... like wise the col50 is included on the report but is ordered in as 3rd column in the csv file.

My main challenge here to create the xslt file for "CSV" and put the columns in the order selection which are selected per user basis.

The output in the CSV file after transformation will look as follows:

Col2    Col1    Col50   Col4
 ...  ...       ...     ....

Any good idea to create this kind of xsl file is much appreciated and I thank you so much for understanding my question and trying to help me in this regard.

1条回答
beautiful°
2楼-- · 2019-07-21 14:47

I. This XSLT 1.0 transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:c="Customer details">
 <xsl:output method="text"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="c:Detail">
     <xsl:apply-templates select=
      "@*[substring(name(), string-length(name())-5)
         = '_SeqID'
        and
          number(.) = number(.)
          ]
     ">
      <xsl:sort data-type="number"/>
     </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="@*">
  <xsl:if test="not(position()=1)">,</xsl:if>
  <xsl:value-of select=
    "../@*
       [name()
       =
        concat('Col',substring-before(substring(name(current()),4),'_'))
        ]"/>
 </xsl:template>
</xsl:stylesheet>

when applied on this XML document (the provided one, made well-formed and unambiguous):

<Report
p1:schemaLocation="Customer details  http://reportserver?%2fCustomer details&amp;rs%3aFormat=XML&amp;rc%3aSchema=True"
Name="Customer details"
xmlns:p1="http://www.w3.org/2001/XMLSchema-instance"
xmlns="Customer details">
    <table2>
        <Detail_Collection>
            <Detail Col1="aaa1" col1_SeqID="2"  col1_Include="1"
            Col2="aaa2" col2_SeqID="1"  col2_Include="1"
            Col3="aaa3" col3_SeqID=""  col3_Include="0"
            Col4="aaa4" col4_SeqID="4"  col4_Include="1"
            Col5="aaa5" col5_SeqID=""  col5_Include="0"
            Col50="aaa50" col50_SeqID="3"  col50_Include="1"
            />
        </Detail_Collection>
    </table2>
</Report>

produces the wanted, correct result:

aaa2,aaa1,aaa50,aaa4

Explanation:

  1. We use that the XPath 1.0 expression:

__

substring($s1, string-length($s1) - string-length($s2) +1) = $s2

is equivalent to the XPath 2.0 expression:

ends-with($s1, $s2))

.2. Appropriate use of <xsl:sort>, substring(), name() and current().

.3. Using the fact that a string $s is castable to number if and only if:

__

number($s) = number($s)

II. XSLT 2.0 solution:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:c="Customer details">
 <xsl:output method="text"/>
 <xsl:strip-space elements="*"/>    

 <xsl:template match="c:Detail">
         <xsl:apply-templates select=
          "@*[ends-with(name(),'_SeqID')
            and . castable as xs:integer]">
          <xsl:sort  select="xs:integer(.)"/>
         </xsl:apply-templates>
 </xsl:template>
 <xsl:template match="@*">
      <xsl:if test="not(position()=1)">,</xsl:if>
      <xsl:value-of select=
        "../@*
           [name()
           eq
            concat('Col',translate(name(current()),'col_SeqID',''))]"/>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the same XML document (above), the same correct result is produced:

aaa2,aaa1,aaa50,aaa4

Update: @desi has asked that the heading should also be generated.

Here is the updated XSLT 1.0 transformation (as indicated, @desi is limited to use XSLT 1.0 only) that does this:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:c="Customer details">
 <xsl:output method="text"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="c:Detail">

     <xsl:for-each select=
      "@*[substring(name(), string-length(name())-5)
         = '_SeqID'
        and
          number(.) = number(.)
          ]
        ">
        <xsl:sort data-type="number"/>

       <xsl:value-of select=
       "concat('Col',
               substring-before(substring(name(current()),4),
                                '_')
               )
       "/>

          <xsl:text>&#9;</xsl:text>
     </xsl:for-each>
     <xsl:text>&#10;</xsl:text>

     <xsl:apply-templates select=
      "@*[substring(name(), string-length(name())-5)
         = '_SeqID'
        and
          number(.) = number(.)
          ]
     ">
      <xsl:sort data-type="number"/>
     </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="@*">
  <xsl:if test="not(position()=1)">,</xsl:if>
  <xsl:value-of select=
    "../@*
       [name()
       =
        concat('Col',substring-before(substring(name(current()),4),'_'))
        ]"/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the same XML document (above), the wanted, correct result is produced:

Col2    Col1    Col50   Col4    
aaa2,aaa1,aaa50,aaa4
查看更多
登录 后发表回答