全文链接即将到来

斯科特·唐

嗨,我有以下输入 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>

我正在获取整个外部参照的链接,但我需要单独的链接。请为此建议一个代码。

C队

您应该考虑更改使用模板的方法,因为您可能会发现它更清晰、更容易适应。

因此,要将Description元素更改defand 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

ArrayIndexOutOfBoundsException即将到来

让毕加索预取即将到来的图像

意外的输出值即将到来

使用DateTime捕获即将到来的日期

更改即将到来的数据的频率

未获取未定义的 JavaScript 值即将到来

如何获取即将到来的文件大小,以KB为单位

甲骨文今年即将到来的生日

互联网即将到来时刷新活动

如何检查即将到来的空值(jQuery)?

获取即将到来的夏令时日期

如何获取当前日期和即将到来的日期?

使用python中的重复序列映射即将到来的值

在同一列中过滤即将到来的结果

在 Flutter iconButton 中单击 appBar 但错误即将到来

即将到来的页面的jQuery倒数进度栏

准确的输出即将到来,但出现了一些错误

通过即将到来的页面阻止公众访问我的网站

html内容即将到来时,应用程序崩溃了,如何在android中处理?

如何用Java获取过去的星期日和即将到来的星期日?

如何获取即将到来的特定时间的java.time.LocalDateTime对象?

在Linux上获取即将到来的加拿大日的特定星期几

如何动态打印即将到来的类名和它们的xpath相同?使用Selenium Webdriver

如何将即将到来的提交压缩到先前的提交中?

如何根据即将到来的数据类型动态更改表格内图标的呈现

熊猫 - 如何添加具有指数从DateTimeIndex名单即将到来的新的空行?

Pandas - 计算下一个/即将到来的 x 天数的值频率

我尝试将Android项目与Firebase连接,但JSON文件错误即将到来

如何在字符串中保存即将到来的日期