XSLTsorting多个for-each

随着Saxon 9.7 xslt v3,我正在创build一个电子表格,内容是在两个单独的xsl:for-each blocks中生成的。 第一个块select树中较高的节点,例如在数据模型层上显示信息。 第二个块包含数据级别模型信息,但会涉及更多关于variables和模型内容的详细信息,创build以下输出:

目前的结果

前四行(1st xsl:for-each )处于模型级别,显示数据模型的注释,前一行(2nd xsl:for-each for- xsl:for-eachselect子节点)处于具有可变注释的variables级别。

我想按数据模型进行sorting,这样数据模型信息加上来自第二个xsl:for-each的variables信息就在一起了。

但到目前为止,我只能xsl:sortxsl:for-each内进行xsl:sort ,而不是全部。 我想整个sorting

 dataList/ordinal dataList/dataListDefault/ordinal dataList/dataListDefault/variableList/variableListDefualt/ordinal dataList/dataListDefault/variableList/variableListDefualt/annotationList/annotationDefault/ordinal 

我想看看: 想要的结果

我的XML

 <?xml version="1.0" encoding="UTF-8"?> <tables> <dataList> <ordinal>1</ordinal> <name>DATA1</name> <label>Data 1</label> <datasetList> <datasetListDefault> <ordinal>1</ordinal> <name>DS1</name> <annotationList> <annotation> Dataset 1</annotation> </annotationList> <variableList> <variableListDefault> <ordinal>1</ordinal> <name>VAR1</name> <label>Variable 1</label> <annotationList> <annotationDefault> <ordinal>2</ordinal> <annotation>This is the 1st variable</annotation> </annotationDefault> <annotationDefault> <ordinal>1</ordinal> <annotation>Variable used for analysis</annotation> </annotationDefault> </annotationList> </variableListDefault> <variableListDefault> <ordinal>2</ordinal> <name>VAR2</name> <label>Variable 2</label> <annotationList> <annotationDefault> <ordinal>1</ordinal> <annotation>This is the 2nd variable</annotation> </annotationDefault> </annotationList> </variableListDefault> </variableList> </datasetListDefault> <datasetListDefault> <ordinal>2</ordinal> <name>DS2</name> <annotationList> <annotation> Dataset 2</annotation> </annotationList> <variableList> <variableListDefault> <ordinal>1</ordinal> <name>VAR1</name> <label>Variable 1</label> <annotationList> <annotationDefault> <ordinal>1</ordinal> <annotation>This is the 1st variable</annotation> </annotationDefault> <annotationDefault> <ordinal>2</ordinal> <annotation>Remove when x</annotation> </annotationDefault> </annotationList> </variableListDefault> <variableListDefault> <ordinal>2</ordinal> <name>VAR2</name> <label>Variable 2</label> <annotationList> <annotationDefault> <ordinal>1</ordinal> <annotation>This is the 2nd variable</annotation> </annotationDefault> </annotationList> </variableListDefault> </variableList> </datasetListDefault> </datasetList> </dataList> <dataList> <ordinal>2</ordinal> <name>DATA2</name> <label>Data 2</label> <datasetList> <datasetListDefault> <ordinal>1</ordinal> <name>DS21</name> <annotationList> <annotation> Dataset 21</annotation> </annotationList> <variableList> <variableListDefault> <ordinal>1</ordinal> <name>VAR1</name> <label>Variable 1</label> <annotationList> <annotationDefault> <ordinal>1</ordinal> <annotation>This is the 1st variable</annotation> </annotationDefault> </annotationList> </variableListDefault> <variableListDefault> <ordinal>2</ordinal> <name>VAR2</name> <label>Variable 2</label> <annotationList> <annotationDefault> <ordinal>1</ordinal> <annotation>This is the 2nd variable</annotation> </annotationDefault> </annotationList> </variableListDefault> </variableList> </datasetListDefault> <datasetListDefault> <ordinal>2</ordinal> <name>DS22</name> <annotationList> <annotation> Dataset 22</annotation> </annotationList> <variableList> <variableListDefault> <ordinal>1</ordinal> <name>VAR1</name> <label>Variable 1</label> <annotationList> <annotationDefault> <ordinal>1</ordinal> <annotation>This is the 1st variable</annotation> </annotationDefault> </annotationList> </variableListDefault> <variableListDefault> <ordinal>2</ordinal> <name>VAR2</name> <label>Variable 2</label> <annotationList> <annotationDefault> <ordinal>1</ordinal> <annotation>This is the 2nd variable</annotation> </annotationDefault> </annotationList> </variableListDefault> </variableList> </datasetListDefault> </datasetList> </dataList> </tables> 

和xslt

 <?xml version="1.0" encoding="UTF-8"?> <?mso-application progid="Excel.Sheet"?> <xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40" > <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <xsl:processing-instruction name="mso-application">progid="Excel.Sheet"</xsl:processing-instruction> <ss:Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40"> <xsl:call-template name="document-properties"/> <xsl:call-template name="document-styles"/> <!-- ****************************************************************************************** --> <!-- CREATE THE TABS IN THE WORKBOOK --> <!-- ****************************************************************************************** --> <xsl:call-template name="getData"/> <!-- ****************************************************************************************** --> </ss:Workbook> </xsl:template> <xsl:template name="getData"> <ss:Worksheet ss:Name="Data"> <Names> <NamedRange ss:Name="_FilterDatabase" ss:RefersTo="=Data!R1C1:R1C7" ss:Hidden="1"/> </Names> <ss:Table ss:ExpandedRowCount="7000" x:FullColumns="1" x:FullRows="1" > <Column ss:Width="100"/> <Column ss:Width="300"/> <Column ss:Width="100"/> <Column ss:Width="250"/> <Column ss:Width="140"/> <Column ss:Width="140"/> <Column ss:Width="400"/> <Row> <Cell ss:StyleID="sHead"> <Data ss:Type="String">Data Model Name </Data> <NamedCell ss:Name="_FilterDatabase"/> </Cell> <Cell ss:StyleID="sHead"> <Data ss:Type="String">Data Model Label </Data> <NamedCell ss:Name="_FilterDatabase"/> </Cell> <Cell ss:StyleID="sHead"> <Data ss:Type="String">Dataset Name</Data> <NamedCell ss:Name="_FilterDatabase"/> </Cell> <Cell ss:StyleID="sHead"> <Data ss:Type="String">Varaible Label</Data> <NamedCell ss:Name="_FilterDatabase"/> </Cell> <Cell ss:StyleID="sHead"> <Data ss:Type="String">Variable Name</Data> <NamedCell ss:Name="_FilterDatabase"/> </Cell> <Cell ss:StyleID="sHead"> <Data ss:Type="String">Annotation</Data> <NamedCell ss:Name="_FilterDatabase"/> </Cell> </Row> <xsl:for-each select="//datasetListDefault"> <xsl:sort select="../../ordinal"/> <xsl:sort select="ordinal"/> <Row ss:AutoFitHeight="1"> <Cell ss:StyleID="sBody"> <Data ss:Type="String"> <xsl:value-of select="../../name"/> </Data> </Cell> <Cell ss:StyleID="sBody"> <Data ss:Type="String"> <xsl:value-of select="../../label"/> </Data> </Cell> <Cell ss:StyleID="sBody"> <Data ss:Type="String"> <xsl:value-of select="name"/> </Data> </Cell> <Cell ss:StyleID="sBody"> <Data ss:Type="String"></Data> </Cell> <Cell ss:StyleID="sBody"> <Data ss:Type="String"></Data> </Cell> <Cell ss:StyleID="sBody"> <ss:Data ss:Type="String"> <xsl:value-of select="annotationList/annotation"/> </ss:Data> </Cell> </Row> </xsl:for-each> <xsl:for-each select="//variableListDefault"> <xsl:sort select="../../../../ordinal"/> <xsl:sort select="../../ordinal"/> <xsl:sort select="ordinal"/> <xsl:sort select="following-sibling::annotationList/annotationDefault/ordinal"/> <Row ss:AutoFitHeight="1"> <Cell ss:StyleID="sBody"> <Data ss:Type="String"> <xsl:value-of select="../../../../name"/> </Data> </Cell> <Cell ss:StyleID="sBody"> <Data ss:Type="String"> <xsl:value-of select="../../../../label"/> </Data> </Cell> <Cell ss:StyleID="sBody"> <Data ss:Type="String"> <xsl:value-of select="../../name"/> </Data> </Cell> <Cell ss:StyleID="sBody"> <Data ss:Type="String"> <xsl:value-of select="label"/> </Data> </Cell> <Cell ss:StyleID="sBody"> <Data ss:Type="String"> <xsl:value-of select="name"/> </Data> </Cell> <Cell ss:StyleID="sBody"> <ss:Data ss:Type="String"> <xsl:apply-templates select="annotationList/annotationDefault/annotation"/> </ss:Data> </Cell> </Row> </xsl:for-each> </ss:Table> <xsl:call-template name="worksheet-options"/> <AutoFilter x:Range="R1C1:R1C7" xmlns="urn:schemas-microsoft-com:office:excel"/> </ss:Worksheet> </xsl:template> <!-- *************************************GENERAL TEMPLATES************************************************** --> <!-- EXCEL WORKSHEET OPTIONS --> <xsl:template name="worksheet-options"> <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel"> <PageSetup> <Header x:Margin="0.3"/> <Footer x:Margin="0.3"/> <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/> </PageSetup> <Selected/> <FreezePanes/> <FrozenNoSplit/> <SplitHorizontal>1</SplitHorizontal> <TopRowBottomPane>1</TopRowBottomPane> <ActivePane>2</ActivePane> <Panes> <Pane> <Number>3</Number> <ActiveRow>18</ActiveRow> <ActiveCol>2</ActiveCol> </Pane> </Panes> <ProtectObjects>False</ProtectObjects> <ProtectScenarios>False</ProtectScenarios> </WorksheetOptions> </xsl:template> <!-- EXCEL DOCUMENT PROPERTIES --> <xsl:template name="document-properties"> <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"> <Author></Author> <LastAuthor></LastAuthor> <Created>2016-08-24T10:40:32Z</Created> <Company></Company> <Version>14.00</Version> </DocumentProperties> <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"> <WindowHeight>14370</WindowHeight> <WindowWidth>27795</WindowWidth> <WindowTopX>480</WindowTopX> <WindowTopY>120</WindowTopY> <ProtectStructure>False</ProtectStructure> <ProtectWindows>False</ProtectWindows> </ExcelWorkbook> </xsl:template> <!-- EXCEL DOCUMENT STYLES --> <xsl:template name="document-styles"> <ss:Styles> <ss:Style ss:ID="Default" ss:Name="Normal"> <ss:Alignment ss:Vertical="Bottom" ss:WrapText="1"/> <ss:Borders/> <ss:Font ss:FontName="Arial" x:Family="Swiss" ss:Size="10" ss:Color="#000000"/> <ss:Interior/> <ss:NumberFormat/> <ss:Protection/> </ss:Style> <ss:Style ss:ID="sHead"> <Alignment ss:Horizontal="Center" ss:Vertical="Top" ss:WrapText="1"/> <ss:Borders> <ss:Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> <ss:Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/> <ss:Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/> <ss:Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/> </ss:Borders> <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="9" ss:Color="#FFFFFF"/> <ss:Interior ss:Color="#538DD5" ss:Pattern="Solid"/> </ss:Style> <ss:Style ss:ID="sBody"> <Alignment ss:Horizontal="Left" ss:Vertical="Top" ss:WrapText="1"/> <ss:Borders> <ss:Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/> <ss:Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/> <ss:Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/> <ss:Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/> </ss:Borders> <ss:Interior ss:Pattern="Solid"/> </ss:Style> </ss:Styles> </xsl:template> </xsl:stylesheet> 

问题 :是否有一种解决scheme,用于对xsl:for-each块中的结果数据xsl:for-each整体/或/ for / each / for / post初始处理?

进一步更多的sorting兄弟姐妹为//variableListDefault/annotationList/annotationDefault/ordinal我已经尝试通过与anaylze-string进行sorting,现在<xsl:sort select="following-sibling::annotationList/annotationDefault/ordinal"/> ,没有成功。 哪个sorting选项可以单独处理这些兄弟节点并进行sorting?

看起来你应该在这里嵌套你的xsl:for-each语句。

而不是这样做:

 <xsl:for-each select="//datasetListDefault"> <xsl:sort select="../../ordinal"/> <xsl:sort select="ordinal"/> <Row ss:AutoFitHeight="1"> <!-- snip --> </Row> </xsl:for-each> <xsl:for-each select="//variableListDefault"> <xsl:sort select="../../../../ordinal"/> <xsl:sort select="../../ordinal"/> <xsl:sort select="ordinal"/> <Row ss:AutoFitHeight="1"> <!-- snip --> </Row> </xsl:for-each> 

做这个….

 <xsl:for-each select="//datasetListDefault"> <xsl:sort select="../../ordinal"/> <xsl:sort select="ordinal"/> <Row ss:AutoFitHeight="1"> <!-- snip --> </Row> <xsl:for-each select=".//variableListDefault"> <xsl:sort select="../../../../ordinal"/> <xsl:sort select="../../ordinal"/> <xsl:sort select="ordinal"/> <Row ss:AutoFitHeight="1"> <!-- snip --> </Row> </xsl:for-each> </xsl:for-each> 

请注意使用.//variableListDefault以便它仅select当前datasetListDefault节点的后代。 (我认为在这种情况下也可以使用<xsl:for-each select="variableList/variableListDefault"> )。