简体   繁体   English

XSLT-遍历节点集

[英]XSLT - Iterate through node-set

I have a xml like this: 我有这样的xml:

<root>
<row>
    <col name="col1">&lt;root&gt;&lt;row&gt;&lt;col name=&quot;COL_NAME_1&quot;&gt;col_value_1&lt;/col&gt;;&lt;/row&gt;&lt;/root&gt;</col>
    <col name="col2">&lt;root&gt;&lt;row&gt;&lt;col name=&quot;COL_NAME_2&quot;&gt;col_value_2&lt;/col&gt;;&lt;/row&gt;&lt;/root&gt;</col>
</row></root>

I need to get escaped collection from col1 and iterate through its rows. 我需要从col1获取转义的集合并遍历其行。 I'm using exsl:node-set function. 我正在使用exsl:node-set函数。 Here is my simplified xsl: 这是我的简化xsl:

<xsl:template match="/">
<xsl:variable name="collection" select="exsl:node-set(./root/row/col[@name='col1'])" disable-output-escaping="yes" />
<xsl:for-each select="($collection)/root/row">
    <!-- ... -->
</xsl:for-each></xsl:template>

I can correctly read value from variable $collection using xsl:value-of function but i can't iterate over it as if there were no rows. 我可以使用xsl:value-of函数从变量$ collection正确读取值,但是我无法像没有行一样遍历它。 Any ideas what am I doing wrong? 有什么想法我做错了吗?

If the author of this XML had wanted you to see elements like root and row inside the col element, they wouldn't have escaped the angle brackets. 如果该XML的作者希望您在col元素内看到诸如root和row之类的元素,则它们不会逃脱尖括号。 Because they are escaped, there's no structure here, only text. 因为它们已转义,所以这里没有结构,只有文本。 The operation of getting structure from the text is called parsing. 从文本获取结构的操作称为解析。 XSLT 3.0 has a function parse-xml() to parse text and create a tree structure; XSLT 3.0具有函数parse-xml()来解析文本并创建树结构。 earlier versions don't, though your vendor might provide extensions or you might be able to write your own. 早期版本没有,尽管您的供应商可能会提供扩展名,或者您可能能够编写自己的扩展名。

The node-set() function doesn't parse. node-set()函数不会解析。 Your $collection variable is a node-set consisting of a temporary document node with one col child. 您的$collection变量是一个节点集,由一个带有一个col子级的临时文档节点组成。

Saxon has an extension function called parse() that does what you want, as do some other XSLT processors. Saxon具有一个称为parse()的扩展功能,它可以满足您的需要,就像其他一些XSLT处理器一样。 I have also seen some attempts at coding this in pure XSLT, but don't know if there's a good implementation. 我也看到过一些尝试在纯XSLT中进行编码,但是不知道是否有一个很好的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM