AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 79577369
Accepted
Jens
Jens
Asked: 2025-04-16 22:08:36 +0800 CST2025-04-16 22:08:36 +0800 CST 2025-04-16 22:08:36 +0800 CST

如何在 XSLT 中选择兄弟姐妹的范围?

  • 772

假设我有以下 XML:

<?xml version="1.0" encoding="utf8"?>
<test>
  <list>
    <li>a</li>
    <li>a</li>
    <li begin="true">b</li>  <!-- begin of the "b" list -->
    <li>b</li>
    <li>b</li>
    <li end="true">b</li>  <!-- end of the "b" list -->
    <li>c</li>
    <li>c</li>
  </list>
</test>

我想使用 XSLT 1.0 根据begin和end属性将此列表拆分为三个列表,结果如下:

  <list>
    <li>a</li>
    <li>a</li>
  </list>

  <list>
    <li>b</li>
    <li>b</li>
    <li>b</li>
    <li>b</li>
  </list>

  <list>
    <li>c</li>
    <li>c</li>
  </list>

我设法选择了第一个和第三个列表,但在匹配中间列表的范围时遇到了困难:

<xsl:template match="list">
  <list>
    <xsl:apply-templates select="li[@begin = 'true']/preceding-sibling::li" />
  </list>
  <list>
    <xsl:apply-templates select="li[...]" />  <!-- Hmm 🤔 -->
  </list>
  <list>
    <xsl:apply-templates select="li[@end = 'true']/following-sibling::li" />
  </list>
</xsl:template>

position()我尝试使用和进行各种表达,count()但都不能完全正确。

附录:属性begin和end是唯一的,即原始列表中恰好有一个这样的组。

xml
  • 4 4 个回答
  • 60 Views

4 个回答

  • Voted
  1. Best Answer
    michael.hor257k
    2025-04-16T22:39:54+08:002025-04-16T22:39:54+08:00

    在给定的示例中,只有 1 个标记组(因此总共只有 3 个组),您可以使用:

    <list>
        <xsl:apply-templates select="li[@begin='true']" />
        <xsl:apply-templates select="li[@begin='true']/following-sibling::li[not(preceding-sibling::li[@end='true'])]"/>
    </list>
    

    (为了便于阅读,我将其分成了两个单独的说明。)


    额外:

    如果您正在使用libxslt处理器,您可以利用它支持的一些扩展功能并执行以下操作:

    <xsl:template match="list">
        <xsl:variable name="head" select="set:leading(li, li[@begin = 'true'])"/>
        <xsl:variable name="tail" select="set:trailing(li, li[@end = 'true'])"/>
        <list>
            <xsl:apply-templates select="$head"/>
        </list>
        <list>
            <xsl:apply-templates select="set:difference(li, $head | $tail)"/>
        </list>
        <list>
            <xsl:apply-templates select="$tail"/>
        </list>
    </xsl:template>
    

    只要您声明:

    xmlns:set="http://exslt.org/sets"
    extension-element-prefixes="set"
    

    在xsl:stylesheet开始标签中。

    • 0
  2. LMC
    2025-04-16T23:31:11+08:002025-04-16T23:31:11+08:00

    此 xslt 返回预期结果

    ?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:output method="xml" indent="yes"/>
      <xsl:template match="list">
        <list>
          <xsl:for-each select="li[@begin = 'true']/preceding-sibling::li">
            <li>
              <xsl:value-of select="text()"/>
            </li>
          </xsl:for-each>
        </list>
    
        <list>
          <xsl:for-each select="li[@begin = 'true' or @end = 'true'] | li[following-sibling::li[@end = 'true'] and preceding-sibling::li[@begin = 'true']]">
            <li>
              <xsl:value-of select="text()"/>
            </li>
          </xsl:for-each>
        </list>
    
        <list>
          <xsl:for-each select="li[@end = 'true']/following-sibling::li">
            <li>
              <xsl:value-of select="text()"/>
            </li>
          </xsl:for-each>
        </list>
      </xsl:template>
    </xsl:stylesheet>
    
    <list>
      <li>a</li>
      <li>a</li>
    </list>
    <list>
      <li>b</li>
      <li>b</li>
      <li>b</li>
      <li>b</li>
    </list>
    <list>
      <li>c</li>
      <li>c</li>
    </list>
    
    • 0
  3. Michael Kay
    2025-04-16T23:38:03+08:002025-04-16T23:38:03+08:00

    在一般情况下,使用 XSLT 1.0 会相当棘手,如果可以的话,最好转到 XSLT 2.0+,其中有类似的说明xsl:for-each-group group-starting-with....。

    很大程度上取决于问题的普遍性,例如,是否可以在一系列兄弟元素中拥有多个“开始”和“结束”,或者甚至可以嵌套开始和结束。(永远记住,一个例子并不能定义你的问题:我们需要了解问题的普遍性,而不是仅仅了解其中的一个例子。)

    一个很有用的技巧(不仅在 1.0 中,在后续版本中也适用)是“兄弟递归”,即对第一个子节点执行 apply-templates 操作,然后对下一个子节点也执行 apply-templates 操作,以此类推。这里有一个例子:

    XSLT 1.0 选择“标志”节点之间的所有节点

    您可以通过搜索“XSLT 兄弟递归”找到其他内容。

    您向我们展示的标记是“重叠标记”的一个例子,并且有大量关于这个主题的文献:如果您有很多这样的 XML,那么熟悉这个理论是值得的。

    • 0
  4. y.arazim
    2025-04-17T05:05:56+08:002025-04-17T05:05:56+08:00

    我尝试使用position()和count()的各种表达式,但无法完全正确。

    可以这样做:

    <xsl:template match="list">
        <xsl:variable name="begin" select="count(li[@begin='true']/preceding-sibling::li) + 1" />
        <xsl:variable name="end" select="count(li[@end='true']/preceding-sibling::li) + 1" />
        <list>
            <xsl:apply-templates select="li[position() &lt; $begin]"/>
        </list>
        <list>
            <xsl:apply-templates select="li[$begin &lt;= position() and position() &lt;= $end]"/>
        </list>
        <list>
            <xsl:apply-templates select="li[$end &lt; position()]"/>
        </list>
    </xsl:template>
    
    • 0

相关问题

  • XSL 替换单个元素的命名空间 URI,但保留前缀

  • 使用 xslt 替换 xml 文件中元素中的值

  • XML - 将列转换为单独的行?

  • 如何在 Oxygen 中通过 Saxon 正确运行 SQL 扩展

  • XML 架构:元素 A、元素 B 或两者

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve