不同值的 XSLT 1.0 变体 [英] XSLT 1.0 variant for distinct-values

查看:19
本文介绍了不同值的 XSLT 1.0 变体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

我希望遍历 CodePart 值的不同出现.在 XSLT 2.0 中很容易:

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>

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

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>

所以我想到了循环//CodePart/value,以确保初学者的唯一性.但是后来我需要一些包含条件

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

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

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

并使用类似的东西

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

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

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

推荐答案

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

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> 

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

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> 

这篇关于不同值的 XSLT 1.0 变体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆