我有一个输入 XML 文档,如下所示:
<text>
<p>
Download the software from <link id="blah">
</p>
</text>
<links>
<link id="blah">
<url>http://blah</url>
</link>
</links>
Run Code Online (Sandbox Code Playgroud)
我希望我的输出文档是:
<text>
<p>
Download the software from <a href="http://blah"> http://blah </a>
</p>
</text>
Run Code Online (Sandbox Code Playgroud)
也就是说:我想按原样复制现有的输入文档节点,但也<link>用扩展版本替换某些节点(例如 ):基于输入文档中包含的其他信息。
我尝试<xsl:copy .../>首先复制片段,如下所示:
<xsl:variable name="frag">
<xsl:copy-of select="text"/>
</xsl:variable>
Run Code Online (Sandbox Code Playgroud)
但是当我像这样输出变量时:
<xsl:value-of select="$frag">
Run Code Online (Sandbox Code Playgroud)
输出似乎没有保留段落标签?所以我不确定 xsl-copy 是否已复制节点,或者只是以某种方式复制文本?
如果我仅放入以下内容(去掉<xsl:variable/>“包装器”),它会保留输出文档中的标签吗?
<xsl:copy-of select="text"/>
Run Code Online (Sandbox Code Playgroud)
但当然,我需要首先将“链接”标签重新映射到锚标签......
我什至还没有开始弄清楚如何用链接信息替换变量的内容(当然在新变量中)......
尝试这个 :
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="links"/>
<xsl:template match="*|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="link">
<xsl:variable name="link" select="normalize-space(//links/link[@id = current()/@id]/url)"/>
<a href="{$link}">
<xsl:value-of select="$link"/>
</a>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
使用以下输入:
<?xml version="1.0" encoding="UTF-8"?>
<texts>
<text>
<p>
Download the software from <link id="blah"/>
</p>
</text>
<links>
<link id="blah">
<url>http://blah</url>
</link>
</links>
</texts>
Run Code Online (Sandbox Code Playgroud)
你得到 :
<?xml version="1.0" encoding="UTF-8"?>
<texts>
<text>
<p>
Download the software from <a href="http://blah">http://blah</a>
</p>
</text>
</texts>
Run Code Online (Sandbox Code Playgroud)