我有一个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>更多推荐
发布评论