xslt-1.0 - xslt 从 XML 中检索 href 值
问题描述
我是 XSLT 的新手,目前我需要从下面的 xml 中提取外部参照值。
需要选择标签<link rel=" next " href= />中的外部参照
注意:要求是查找 <link -> rel="next" 并提取 href 值以将其存储在变量中。
预期结果是:
<properties>
<code>40000018</code>
<name>+++ ATZ +++</name>
</properties>
<next_page>skiptoken=eyJzdGFydFJvdyI6MTAwMCwiZW5kUm93IjoyMDAwfQ==</next_page>
</results>
以下是 xml 示例:
<feed xml:base="https://api12preview.sapsf.eu/odata/v2/" xmlns="http://www.w3.org/2005/Atom" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">
<title type="text">Position</title>
<id>https://api12preview.sapsf.eu/odata/v2/Position</id>
<updated>2021-11-05T03:51:45Z</updated>
<link rel="self" title="Position" href="Position"/>
<entry>
<id>https://api12preview.sapsf.eu/odata/v2/Position(code='40000018',effectiveStartDate=datetime'2020-03-16T00:00:00')</id>
<title type="text"/>
<updated>2021-11-05T03:51:45Z</updated>
<author>
<name/>
</author>
<link rel="edit" title="Position" href="Position(code='40000018',effectiveStartDate=datetime'2020-03-16T00:00:00')"/>
<category term="SFOData.Position" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
<content type="application/xml">
<m:properties>
<d:code>40000018</d:code>
<d:externalName_defaultValue>+++ ATZ +++</d:externalName_defaultValue>
</m:properties>
</content>
</entry>
<link rel="next" href="https://api12preview.sapsf.eu/odata/v2/Position?$select=code,externalName_defaultValue&$skiptoken=eyJzdGFydFJvdyI6MTAwMCwiZW5kUm93IjoyMDAwfQ=="/>
</feed>
目前我正在使用下面的 xslt 并给出除 href 值之外的结果。
<?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>
</results>
</xsl:template>
</xsl:stylesheet>
解决方案
你的问题定义得不够好。您说您想“提取 href 值”,但预期的结果仅显示了 href 值的一部分——而且我们没有关于如何找到您想要的部分的规则。
以下样式表会产生预期的结果,但它基于猜测,如果猜测对其他示例无效,则可能会失败:
<xsl:stylesheet version="1.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:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/a:feed">
<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_page>
<xsl:value-of select="substring-after(a:link[@rel='next']/@href, '&$')"/>
</next_page>
</results>
</xsl:template>
</xsl:stylesheet>
推荐阅读
- angular - 从 Ionic 4 升级到 Ionic 5 后出现平台浏览器错误
- c# - 或工具 Sat 示例在 C# 表单应用程序中不起作用
- css - Tablepress CSS列格式问题
- python - ValueError:我在抓取网页时找不到表
- matlab - 如何迭代地平均matlab矩阵中的元素连续列以创建一个逐渐变大的中点矩阵?
- spring - 带有 JSON 文件的 Spring Boot MongoDB 数据库播种器
- javascript - 从 bootstrap-datepicker 创建 Vuejs 组件
- testing - 有没有办法从现有的测试套件中创建测试摘要 - 在 Microsoft TFS 中?
- python-3.x - TypeError:只能将列表(不是“str”)连接到列表中,但我已经包含了类型检查
- python - 从熊猫系列的每个值中的子字符串字典中查找第一次出现的子字符串并插入相应的字典值