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

如何用XSLT提取XML中rel="next"的link标签的href属性值

XSLT提取rel="next"链接href属性的解决方案

调整后的完整XSLT代码

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:a="http://www.w3.org/2005/Atom" 
    xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
    xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
    exclude-result-prefixes="a d m">
<xsl:template match="/">
<results>
      <xsl:for-each select="//a:entry/a:content">
        <properties>
            <code><xsl:value-of select="./m:properties/d:code"/></code>
            <name><xsl:value-of select="./m:properties/d:externalName_defaultValue"/></name>
        </properties>
       </xsl:for-each>
       <!-- 提取next页的href属性值 -->
       <xsl:variable name="nextHref" select="//a:link[@rel='next']/@href" />
       <xsl:if test="$nextHref">
           <next_page>
               <!-- 截取href中skiptoken参数部分,匹配预期输出格式 -->
               <xsl:value-of select="concat('skiptoken=', substring-after($nextHref, '$skiptoken='))" />
           </next_page>
       </xsl:if>
</results>
</xsl:template>
</xsl:stylesheet>

调整说明

  • 你已经在XSLT中声明了Atom命名空间http://www.w3.org/2005/Atom的别名为a,XML中的link节点属于该命名空间,直接用a:link即可定位到目标节点
  • 通过谓语[@rel='next']筛选出属性rel值为next的link节点,提取href属性存入变量
  • 使用substring-after函数截取href中$skiptoken=后的内容,拼接后输出符合要求的next_page节点
  • 增加非空判断,当不存在下一页链接时不会生成空的next_page节点

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 02:45:03