首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在每两个处理指令之间查找所有XML节点

在每两个处理指令之间查找所有XML节点
EN

Stack Overflow用户
提问于 2016-03-03 10:33:06
回答 1查看 389关注 0票数 1

使用XPath或XSLT1.0,我需要在处理指令<?start?><?end?>之间找到每个节点。以下代码:

代码语言:javascript
复制
//node()[preceding-sibling::processing-instruction()[self::processing-instruction('start')] 
following-sibling::processing-instruction()[ self::processing-instruction('end')]]`, 

工作正常,但很自然,它只在第一个PI和最后一个PI之间选择节点。有没有办法只选择每个开始-结束对之间的节点?

代码语言:javascript
复制
<root>
    abc

<?start?>def<Highlighted
    bold="yes"><Highlighted italic="yes">ghi</Highlighted></Highlighted>jkl
    <?pi?>
    <table>
      <Caption>stu</Caption>
    </table>vw

<?end?>
xy<?start?> 
abc <Caption>def</Caption> ghi<?end?> jkl
</root>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-03 10:57:16

下面是一个XSLT2.0示例,它使用for-each-group group-starting-with/group-ending-with查找并输出这对处理指令之间的节点:

代码语言:javascript
复制
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

<xsl:output indent="yes"/>

<xsl:template match="root">
    <xsl:for-each-group select="node()" group-starting-with="processing-instruction('start')">
        <xsl:if test="self::processing-instruction('start')">
            <group>
                <xsl:for-each-group select="current-group() except ." group-ending-with="processing-instruction('end')">
                    <xsl:sequence select="current-group()[position() ne last()]"/>
                </xsl:for-each-group>
            </group>
        </xsl:if>
    </xsl:for-each-group>
</xsl:template>

</xsl:stylesheet>

你的样本的结果是

代码语言:javascript
复制
<group>def<Highlighted bold="yes">
      <Highlighted italic="yes">ghi</Highlighted>
   </Highlighted>jkl
    <?pi?>
    <table>
        <Caption>stu</Caption>
    </table>vw

    </group>
<group> 
    abc <Caption>def</Caption> ghi</group>

使用XSLT1.0,您可以计算start pi后面和end pi之前的节点的交集:

代码语言:javascript
复制
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:output indent="yes"/>

    <xsl:key name="start" match="root/node()[not(self::processing-instruction('start'))]" use="generate-id(preceding-sibling::processing-instruction('start')[1])"/>
    <xsl:key name="end" match="root/node()[not(self::processing-instruction('end'))]" use="generate-id(following-sibling::processing-instruction('end')[1])"/>

    <xsl:template match="root">
        <xsl:apply-templates select="processing-instruction('start')"/>
    </xsl:template>

    <xsl:template match="processing-instruction('start')">
        <xsl:variable name="end" select="following-sibling::processing-instruction('end')[1]"/>
        <xsl:variable name="following-start" select="key('start', generate-id())"/>
        <xsl:variable name="preceding-end" select="key('end', generate-id($end))"/>
        <xsl:variable name="intersect" select="$following-start[count(. | $preceding-end) = count($preceding-end)]"/>
        <group>
            <xsl:copy-of select="$intersect"/>
        </group>
    </xsl:template>
</xsl:stylesheet>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35769645

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档