XSLT按任意顺序排序输入 [英] XSLT sort input in arbitrary order
问题描述
我对XSLT非常陌生。我在网上做了大量的研究,但是我找不到适合我的案例的足够信息。
I'm pretty new to XSLT. I did my fair amount of research on the web however I could not find enough information that suit my case.
这就是我想要达到的效果:
我有一个包含10到15个部分的大型XML文档。结构或多或少如下所示:
This is what I'm trying to achieve: I have a large XML document with 10 to 15 sections. The structure more or less looks like the following:
<headerSection>
<!-- lots of stuff in here -->
</headerSection>
<containerSection>
<bodySection>
<container1>
<container2>
<identifier code="12345"/>
<!-- lots of stuff in here -->
</container2>
</container1>
<container1>
<container2>
<identifier code="98765"/>
<!-- lots of stuff in here -->
</container2>
</container1>
</bodySection>
</containerSection>
我有一个XSL文件,我将其应用于输出样式化的HTML页面。
现在,我想要做的是以任意顺序呈现< bodySection>
的子项。确定订单的标准是< identifier>
元素中的代码
属性。
让我再澄清一下,订单应该是任意的。
I have an XSL file that I apply to this in order to output a styled HTML page.
Now, what I want to do is to render the children of the <bodySection>
in an arbitrary order. The criteria for determining the order is the code
attribute in the <identifier>
element.
Let me clarify once more that the ordering should be arbitrary.
我明白我可以用 xsl :sort
指令,但是我是一个巨大的XSL新手,我不完全知道如何从我在网上找到的例子中使用它。重点是我生成一个样式化的HTML页面,而不是仅仅转换XML输入。
I understand that I can do this with an xsl:sort
directive, however I'm a huge XSL newbie and I don't quite get how to use it from the examples I've found online.
The main point is that I'm generating a styled HTML page, not "just" transforming the XML input.
看起来最接近我想要做的是这个片段:
What looks like to be the closest to what I'm trying to do is this snippet:
<xsl:param name="pOrder" select="'professionalsection,educationalsection'"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*">
<xsl:sort select="string-length(
substring-before(
concat(',',$pOrder,','),
concat(',',name(),',')))"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
不幸的是,我不太清楚如何在我的情况下使用它。
直觉上,我会尝试改变 select ='professionalsection,educationalsection'
中指出的值,但这是正确的方法吗?
任何帮助都非常感谢!
Unfortunately I'm not very sure about how to used it in my case.
Intuitively, I would try and change the values pointed at in select="'professionalsection,educationalsection'"
but is this the right approach?
Any help is very appreciated!
更新
在评论中发布的例子中,我提出了这个问题。我希望它能更清楚地表明我想要做什么:
By looking at the example posted in the comment, I came up with this. I hope it gives a more clear picture of what I'm trying to do:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" omit-xml-declaration="yes" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="bodySection">
<xsl:variable name="sort-order">|98765|12345|</xsl:variable>
<xsl:copy>
<xsl:apply-templates select="@*|node()">
<xsl:sort select="string-length(substring-before($sort-order, concat('|', container1/container2/identifier/@code, '|')))" data-type="number" order="descending"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
上面的代码段不起作用。我试图实现的结果如下所示:
The above snippet doesn't work yet. The result I'm trying to achieve would look like:
<headerSection>
<!-- lots of stuff in here -->
</headerSection>
<containerSection>
<bodySection>
<container1>
<container2>
<identifier code="98765"/>
<!-- lots of stuff in here -->
</container2>
</container1>
<container1>
<container2>
<identifier code="12345"/>
<!-- lots of stuff in here -->
</container2>
</container1>
</bodySection>
</containerSection>
...元素 container1
(
...where the elements container1
(along with their children) are swapped.
推荐答案
尝试:
<xsl:sort select="string-length(substring-before($sort-order, concat('|', container2/identifier/@code, '|')))" data-type="number" order="ascending"/>
这篇关于XSLT按任意顺序排序输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!