这个 XSLT 1.0 转换:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:param name="pWanted" select="//img"/>
<xsl:param name="pWantedAttr" select="'url'"/>
<xsl:template match="/">
<xsl:apply-templates select="$pWanted"/>
</xsl:template>
<xsl:template match="*[not(starts-with(@xml:base, '/'))]">
<xsl:apply-templates select="ancestor::*[@xml:base][1]"/>
<xsl:value-of select="concat(@xml:base,@*[name()=$pWantedAttr])"/>
<xsl:if test="not(@xml:base)"><xsl:text>
</xsl:text></xsl:if>
</xsl:template>
<xsl:template match="*[starts-with(@xml:base, '/')]">
<xsl:value-of select="@xml:base"/>
</xsl:template>
</xsl:stylesheet>
应用于此 XML 文档时:
<t>
<a xml:base="/news/" >
<b xml:base="reports/">
<c xml:base="politics/" />
<c xml:base="/sports/" >
<d xml:base="reports/" />
<d xml:base="photos/" >
<img url="A1.jpg" />
<img url="A2.jpg" />
</d>
</c>
<c xml:base="entertainment" />
</b>
</a>
</t>
产生想要的正确结果:
/sports/photos/A1.jpg
/sports/photos/A2.jpg
更新——单个 XPath 2.0 表达式解决方案:
for $target in //img,
$top in $target/ancestor::*[starts-with(@xml:base,'/')][1]
return
string-join(
(
$top/@xml:base
, $top/descendant::*
[@xml:base and . intersect $target/ancestor::*]
/@xml:base
, $target/@url,
'
'
),
''
)
基于 XSLT 2.0 的验证:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:sequence select=
"for $target in //img,
$top in $target/ancestor::*[starts-with(@xml:base,'/')][1]
return
string-join(
(
$top/@xml:base
, $top/descendant::*
[@xml:base and . intersect $target/ancestor::*]
/@xml:base
, $target/@url,
'
'
),
''
)
"/>
</xsl:template>
</xsl:stylesheet>
当此转换应用于提供的 XML 文档时:
<t>
<a xml:base="/news/" >
<b xml:base="reports/">
<c xml:base="politics/" />
<c xml:base="sports/" >
<d xml:base="reports/" />
<d xml:base="photos/" >
<img url="A1.jpg" />
<img url="A2.jpg" />
</d>
</c>
<c xml:base="entertainment" />
</b>
</a>
</t>
对 XPath 表达式求值,并将该求值的结果复制到输出:
/news/reports/sports/photos/A1.jpg
/news/reports/sports/photos/A2.jpg
使用修改后的文档:
<t>
<a xml:base="/news/" >
<b xml:base="reports/">
<c xml:base="politics/" />
<c xml:base="/sports/" >
<d xml:base="reports/" />
<d xml:base="photos/" >
<img url="A1.jpg" />
<img url="A2.jpg" />
</d>
</c>
<c xml:base="entertainment" />
</b>
</a>
</t>
再次产生所需的正确结果:
/sports/photos/A1.jpg
/sports/photos/A2.jpg
更新2:
OP 建议进行这种简化:
原始海报添加的更新:一旦嵌入到完整的应用程序中,完整的 url 替换了相对的,Dimitre 的方法最终变得如此简单
:
<xsl:template match="@url">
<xsl:attribute name="url">
<xsl:apply-templates mode="uri" select=".." />
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="*" mode="uri">
<xsl:if test="not(starts-with(@xml:base, '/'))">
<xsl:apply-templates select="ancestor::*[@xml:base][1]" mode="uri"/>
</xsl:if>
<xsl:value-of select="@xml:base"/>
</xsl:template>