You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用XSL抓取匹配兄弟节点的元素?附XML示例

How to Grab Sibling Elements with XSLT for Your XML

First, here's your XML structure (cleaned up for clarity):

<?xml version="1.0" encoding="UTF-8"?>
<batch>
  <batchnummer>782</batchnummer>
  <continueonerror>true</continueonerror>
  <metafileversion>1.0</metafileversion>
  <documentset>
    <naam></naam>
    <type></type>
    <subsets>
      <subset>
        <staple>true</staple>
        <subdocuments>
          <subdocument>
            <document>thr6UhEw5bER6Cjt8uKOCg</document>
            <stamp></stamp>
            <!-- Truncated content -->
          </subdocument>
        </subdocuments>
      </subset>
    </subsets>
  </documentset>
</batch>

Depending on exactly which siblings you need to target, here are the most common approaches in XSLT:

Target the immediate next sibling

If you want to get the <stamp> element that directly follows <document>, use the following-sibling axis. You can either target it specifically or grab the first sibling:

<!-- When matching a <document> element, get its <stamp> sibling -->
<xsl:template match="document">
  <!-- Explicitly select the stamp sibling -->
  <xsl:value-of select="following-sibling::stamp"/>
  
  <!-- Or grab the very next sibling regardless of tag name -->
  <!-- <xsl:value-of select="following-sibling::*[1]"/> -->
</xsl:template>

Target the immediate previous sibling

To fetch an element that comes right before another (like getting <document> when you're on <stamp>), use the preceding-sibling axis:

<xsl:template match="stamp">
  <!-- Get the document element that's the immediate prior sibling -->
  <xsl:value-of select="preceding-sibling::document"/>
</xsl:template>

Select all siblings of a specific type

If you need to process all <subdocument> elements under the same <subdocuments> parent (they're siblings of each other), you can directly select them from the parent node:

<xsl:template match="subdocuments">
  <xsl:for-each select="subdocument">
    <!-- Process each subdocument sibling -->
    <div>Document ID: <xsl:value-of select="document"/></div>
  </xsl:for-each>
</xsl:template>

Filter siblings by a condition

You can also narrow down siblings using predicates. For example, get all <subset> elements where the <staple> child is true:

<xsl:template match="subsets">
  <xsl:for-each select="subset[staple='true']">
    <!-- Process only subsets with staple enabled -->
    <xsl:value-of select="subdocuments/subdocument/document"/>
  </xsl:for-each>
</xsl:template>

The key thing to remember is that following-sibling and preceding-sibling axes only target elements at the same hierarchy level as your current context node. Make sure you're positioned correctly in the XML tree when using these!

内容的提问来源于stack exchange,提问作者Patrick Rennings

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:32:47