问题描述:

I think my question here might be a bit overwhelming for people, so I'm trying to solve it in chunks.

<xsl:apply-templates mode="sort">

<xsl:sort select="name()"/>

<xsl:sort select="./*"/>

<xsl:sort data-type="text" select="./*[text()]"/>

</xsl:apply-templates>

The first sort statement <xsl:sort select="name()"/> works and I understand it.

The second sort statement <xsl:sort select="./*"/> seems to do almost exactly what I want, but it does not take the text() of a child node into account. I don't really understand why this sort statement is doing what it is doing. Adding it was just one of many attempts to solve my problem.

The third sort statement <xsl:sort data-type="text" select="./*[text()]"/> is my attempt to pick up where the second sort statement left off. It does not work and it generates the error when using Saxon-HE 9.4:

Caught: net.sf.saxon.trans.XPathException: A sequence of more than one

item is not allowed as the @select attribute of xsl:sort

What I'm attempting to do here is sort a parent element correctly among its siblings based on the text of its child element(s).

Help would be much appreciated as the weight of XSLT is crushing me.

网友答案:
<xsl:apply-templates select="*">
  <xsl:sort select="."/>
</xsl:apply-templates>

The above code will take all children element nodes of the current node, sort them by their text value, and push them at your template rules in text sorted order.

From what I read in your snippet, do you really want to sort the children by their element name?

You should almost never have to address text() nodes ... the value of an element is the concatenation of all its descendent text() nodes.

相关阅读:
Top