使用 XSLT 2.0 将多个属性的值解析为类似数组的结构 [英] Using XSLT 2.0 to parse the values of multiple attributes into an array-like structure

查看:27
本文介绍了使用 XSLT 2.0 将多个属性的值解析为类似数组的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够选择文档中特定类型的所有属性(例如,//@arch),然后获取该节点集并将值解析为第二个节点集.当我说解析"时,具体来说我的意思是我想像这样转换一个节点集:

I'd like to be able to select all the attributes of a certain type in a document (for example, //@arch) and then take that node set and parse the values out into second node set. When I say "parse", in specific I mean I want to turn a node set like this:

arch="value1;value2;value3"
arch="value1:value4"

进入这样的节点集:

arch="value1"
arch="value2"
arch="value3"
arch="value1"
arch="value4"

或类似的东西;我想将各个值从属性中取出并放入它们自己的节点中.

or something like that; I want to get the individual values out of the attributes and into their own node.

如果我能达到那个状态,我有很多排序和重复删除的方法,之后我将使用完成的节点集进行发布任务.

If I can get it to that state, I've got plenty of methods for sorting and duplicate removal, after which I'd be using the finished node set for a publishing task.

我不是在这里寻找一个整洁的答案作为一种方法.我知道 XSLT 不能做动态数组,但这与不能做like 动态数组或模仿功能的重要部分的东西是不一样的.

I'm not so much looking for an tidy answer here as an approach. I know that XSLT cannot do dynamic arrays, but that's not the same as not being able to do something like dynamic arrays or something that mimics the important part of the functionality.

我想到的一个想法是,我可以计算第一个节点集中的节点和分隔符的数量,计算第二个节点集需要的条目数并创建它(以某种方式),然后使用substring 函数将第一个节点集解析为第二个节点集.

One thought that has occurred to me is that I could count the nodes in the first node set, and the number of delimiters, calculate the number of entries that the second node set would need and create it (somehow), and use the substring functions to parse out the first node set into the second node set.

通常有一种方法可以解决 XSLT 的问题;以前有人解决过这个问题吗?

There's usually a way working around XSLT's issues; has anyone worked their way around this one before?

感谢您的帮助,杰夫.

推荐答案

我认为您正在寻找的是一个序列.序列可以是节点或原子值(参见 http://www.w3.org/TR/xslt20/#constructing-sequences).

I think what you're looking for is a sequence. A sequence can be either nodes or atomic values (see http://www.w3.org/TR/xslt20/#constructing-sequences).

这是一个示例,展示了如何构建一个序列,然后对其进行迭代.序列是来自 @arch 的原子值,但也可以是节点.

Here's an example showing the construction of a sequence and then iterating over it. The sequence is the atomic values from @arch, but it could also be nodes.

XML 输入

<doc>
    <foo arch="value1;value2;value3"/>
    <foo arch="value1:value4"/>
</doc>

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:variable name="archSequence" as="item()*">
        <xsl:for-each select="//@arch">
            <xsl:for-each select="tokenize(.,'[;:]')">
                <xsl:value-of select="."/>                  
            </xsl:for-each>             
        </xsl:for-each>         
    </xsl:variable>

    <xsl:template match="/*">
        <sequence>
            <xsl:for-each select="$archSequence">
                <item><xsl:value-of select="."/></item>
            </xsl:for-each>
        </sequence>
    </xsl:template>

</xsl:stylesheet>

XML 输出

<sequence>
   <item>value1</item>
   <item>value2</item>
   <item>value3</item>
   <item>value1</item>
   <item>value4</item>
</sequence>

元素序列示例(相同输出):

Example of a sequence of elements (same output):

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:variable name="archSequence" as="element()*">
        <xsl:for-each select="//@arch">
            <xsl:for-each select="tokenize(.,'[;:]')">
                <item><xsl:value-of select="."/></item>         
            </xsl:for-each>             
        </xsl:for-each>         
    </xsl:variable>

    <xsl:template match="/*">
        <sequence>
            <xsl:for-each select="$archSequence">
                <xsl:copy-of select="."/>
            </xsl:for-each>
        </sequence>
    </xsl:template>

</xsl:stylesheet>

这篇关于使用 XSLT 2.0 将多个属性的值解析为类似数组的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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