用于distinct

编程入门 行业动态 更新时间:2024-10-10 02:22:24
用于distinct-values的XSLT 1.0变体(XSLT 1.0 variant for distinct-values)

我有一个XSLT,我在其中创建(来自输入数据)中间变量,如下所示(硬编码示例,但本质上是动态的):

<xsl:variable name="variableX"> <ValidCode CodePart="CP1" Code="C1"/> <ValidCode CodePart="CP2" Code="C2"/> <ValidCode CodePart="CP1" Code="C3"/> <ValidCode CodePart="CP2" Code="C4"/> <ValidCode CodePart="CP2" Code="C5"/> </xsl:variable>

我希望循环出现不同的CodePart值。 在XSLT 2.0中,它很容易:

<xsl:for-each select="distinct-values($variableX/ValidCode/@CodePart)">...</xsl:for-each>

但是在XSLT 1.0中如何做到这一点呢? 请注意,我不能使用密钥,因为它是动态确定的变量而不是输入文件的一部分。

我的输入文件包含所有可能代码部分的列表,如下所示:

<root> <CodePart><value>CP1</value></CodePart> <CodePart><value>CP2</value></CodePart> <CodePart><value>CP3</value></CodePart> </root>

所以我想到了循环//CodePart/value ,确保了初学者的独特性。 但后来我需要一些包含条件的Xpath表达式

“值出现在所有$ variableX / ValidCode / @ CodePart值的节点集中”

并使用类似的东西

<xsl:for-each select="//CodePart[..condition..]/value">...</xsl:for-each>

我正在寻找一种简单形式的Xpath表达式吗? 或者另一种方法更可取?

I have an XSLT in which I create (from the input data) intermediary variables like the following (hard-coded example, but dynamic in nature):

<xsl:variable name="variableX"> <ValidCode CodePart="CP1" Code="C1"/> <ValidCode CodePart="CP2" Code="C2"/> <ValidCode CodePart="CP1" Code="C3"/> <ValidCode CodePart="CP2" Code="C4"/> <ValidCode CodePart="CP2" Code="C5"/> </xsl:variable>

I wish to loop over the distinct occurrences of CodePart values. In XSLT 2.0 it's easy:

<xsl:for-each select="distinct-values($variableX/ValidCode/@CodePart)">...</xsl:for-each>

But how best do this in XSLT 1.0? Note that I can't use a key, as it's a dynamically determined variable and not part of the input file.

My input file does contain a list of all possible code parts as follows:

<root> <CodePart><value>CP1</value></CodePart> <CodePart><value>CP2</value></CodePart> <CodePart><value>CP3</value></CodePart> </root>

So I thought of looping over //CodePart/value instead, ensuring uniqueness for starters. But then I need some Xpath expression that includes the condition

"value occurs in the node-set of all $variableX/ValidCode/@CodePart values"

and use something like

<xsl:for-each select="//CodePart[..condition..]/value">...</xsl:for-each>

Is there a simple form of the Xpath expression I am looking for? Or is another approach preferable?

最满意答案

我不认为你可以直接使用XPath - 但你应该只能检查这样的有效值:

<xsl:for-each select="//CodePart/value"> <xsl:variable name="CodePart" select="."/> <xsl:if test="$variableX/ValidCode[@CodePart=$CodePart]"> . . . </xsl:if> </xsl:for-each>

或更简单(感谢评论):

<xsl:for-each select="//CodePart/value"> <xsl:if test="$variableX/ValidCode[@CodePart=current()]"> . . . </xsl:if> </xsl:for-each>

如果$variableX不是节点集而是XML片段,则需要将其转换为节点集 - 这是依赖于实现的,使用Microsoft处理器:

<xsl:for-each select="//CodePart/value" xmlns:msxsl="urn:schemas-microsoft-com:xslt"> <xsl:if test="msxsl:node-set($variableX)/ValidCode[@CodePart=current()]"> . . . </xsl:if> </xsl:for-each>

I don't think you can use directly an XPath - but you should be able to check for only the valid values like this:

<xsl:for-each select="//CodePart/value"> <xsl:variable name="CodePart" select="."/> <xsl:if test="$variableX/ValidCode[@CodePart=$CodePart]"> . . . </xsl:if> </xsl:for-each>

or more simply (thanks to the comments):

<xsl:for-each select="//CodePart/value"> <xsl:if test="$variableX/ValidCode[@CodePart=current()]"> . . . </xsl:if> </xsl:for-each>

If $variableX is not a node set but an XML fragment it needs to be converted to a node set - this is implementation-dependent, using Microsoft processors:

<xsl:for-each select="//CodePart/value" xmlns:msxsl="urn:schemas-microsoft-com:xslt"> <xsl:if test="msxsl:node-set($variableX)/ValidCode[@CodePart=current()]"> . . . </xsl:if> </xsl:for-each>

更多推荐

本文发布于:2023-08-07 00:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1458403.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:distinct

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!