xslt 从 XML 检索 href 值

xslt to retrieve href value from XML

我是 XSLT 的新手,目前我需要从下面的 xml 中提取外部参照值。

需要选择标签中的外部参照 < link rel="next" href= />

注意:要求是查找 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&amp;$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, '&amp;$')"/>
        </next_page>
    </results>
</xsl:template>

</xsl:stylesheet>