嗨,我有以下输入 xml 文件:
<Description>See <XRef href="push">Time</XRef>, <XRef href="back">Late</XRef>, <XRef href="some">Come</XRef></Description>
XSL 我已经尝试过上面的代码:
<xsl:template match="Description">
<def>
<para>
<xsl:value-of select="normalize-space(node()[1])"/>
<xsl:if test="XRef">
<xsl:choose>
<xsl:when test="TEST">
<xref>
<xsl:attribute name="endterm">
</xsl:attribute>
<xsl:attribute name="linkend">
</xsl:attribute>
</xref>
</xsl:when>
<xsl:otherwise>
<link>
<xsl:choose>
<xsl:when test="@destination='yes'">
<xsl:attribute name="xlink:href">
<xsl:text>test.pdf</xsl:text>
</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="mark">
</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="."/>
</link>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</para>
</def>
</xsl:template>
得到这样的输出:
<def>
<para>See<link mark="">See Time, Late, Come</link></para>
</def>
预期输出如下:
<def>
<para>See <link mark="">Time</link>, <link mark="">Late</link>, <link mark="">Come</link></para>
</def>
我正在获取整个外部参照的链接,但我需要单独的链接。请为此建议一个代码。
您应该考虑更改使用模板的方法,因为您可能会发现它更清晰、更容易适应。
因此,要将Description
元素更改为def
and para
,请执行以下操作
<xsl:template match="Description">
<def>
<para>
<xsl:apply-templates />
</para>
</def>
</xsl:template>
因为XRef
您似乎有一些逻辑,这取决于TEST
元素是否存在。您可以使用两个单独的模板处理此问题
<xsl:template match="XRef[TEST]">
<xref endterm="" linkend="" />
</xsl:template>
<xsl:template match="XRef">
<link>
<xsl:choose>
<xsl:when test="@destination='yes'">
<xsl:attribute name="xlink:href">test.pdf</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="mark" />
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="."/>
</link>
</xsl:template>
当然,您可以在其他情况下很容易地扩展此逻辑。
试试这个 XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xlink="..."
exclude-result-prefixes="#all"
version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="Description">
<def>
<para>
<xsl:apply-templates />
</para>
</def>
</xsl:template>
<xsl:template match="XRef[TEST]">
<xref endterm="" linkend="" />
</xsl:template>
<xsl:template match="XRef">
<link>
<xsl:choose>
<xsl:when test="@destination='yes'">
<xsl:attribute name="xlink:href">test.pdf</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="mark" />
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="."/>
</link>
</xsl:template>
</xsl:stylesheet>
请注意,第一个模板是“身份模板”,它将用于原样复制文档中的所有其他节点(尽管问题中的特定 XML 示例不需要它)。
在http://xsltfiddle.liberty-development.net/6rewNxF 上看到它的实际效果
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句