xsl:for-each-group help needed

匿名 (未验证) 提交于 2019-12-03 00:52:01

问题:

I went through XSLT Grouping Examples and Using for-each-group for high performance XSLT . I have a problem with for-each-group.

My XML

Introduction

XSLT and XQuery

XSLT

XSLT is used to write stylesheets.

XQuery

XQuery is used to query XML databases.

XQuery and stylesheets

XQuery is used to query XML databases.

XSLT and XQuery

XSLT

My Wanted Output

Introduction

XSLT and XQuery

XSLT

XSLT is used to write stylesheets.

XQuery is used to query XML databases

XQuery is used to query XML databases.

XQuery and stylesheets

XQuery is used to query XML databases.

XSLT and XQuery

XSLT

I tried with this. (not working)

Will someone show me the correct way to get my wanted result?

回答1:

Here is an XSLT 2.0 stylesheet using for-each-group in a recursive function (I prefer that to a named template with XSLT 2.0):

When I apply that stylesheet with Saxon 9 to the input

    

Introduction

XSLT and XQuery

XSLT

XSLT is used to write stylesheets.

XQuery

XQuery is used to query XML databases.

XQuery and stylesheets

XQuery is used to query XML databases.

XSLT and XQuery

XSLT

I get the result

    

Introduction

XSLT and XQuery

XSLT

XSLT is used to write stylesheets.

XQuery

XQuery is used to query XML databases.

XQuery and stylesheets

XQuery is used to query XML databases.

XSLT and XQuery

XSLT



回答2:

This transformation uses keys and handles h1-title to h6-title:

When applied on this XML document (corrected the provided one and usin uniform values for the name attribute):

         

Introduction

XSLT and XQuery

XSLT

XSLT is used to write stylesheets.

XQuery

XQuery is used to query XML databases.

XQuery and stylesheets

XQuery is used to query XML databases.

XSLT and XQuery

XSLT

the wanted, correct result is produced:

Introduction

XSLT and XQuery

XSLT

XSLT is used to write stylesheets.

XQuery

XQuery is used to query XML databases.

XQuery and stylesheets

XQuery is used to query XML databases.

XSLT and XQuery

XSLT

Do note:

This transformation solves the main problem of generating the hierarchy. Only trivial changes are needed if it is required that the top level name attribute has the value "h-title".

If more hierarchy levels are necessary, this requires only mechanical adding the corresponding or clauses to the definition of the keys and appending the pipe-delimited string of all name attributes' values with the corresponding new strings.

Here I have adapted and re-used a solution that Jeni Tennison gave for a similar problem.



回答3:

Each of your grouping steps is taking the original set of elements as input, whereas you need each step to work on the groups produced by the previous grouping step. And there are lots of other errors too, for example h1-title is not an attribute name.

It needs to be something like this:

... similar logic for the next level ...

You can nest that as deeply as you want depending how many levels you want to handle; or if you want to handle an indefinite number, you can put the code in a named template and make a recursive call to handle the next level. At the innermost level, leave out the xsl:choose and just do xsl:copy-of select="current-group().

(I just noticed the trailing spaces in the "name" attribute. If these really exist, you will need to include them in the comparison test, or do normalize-space() to get rid of them.)



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!