带模式的 XSLT 应用模板 - 没有匹配模式的错误结果 [英] XSLT apply-template with mode - Incorrect result with no matching mode

查看:23
本文介绍了带模式的 XSLT 应用模板 - 没有匹配模式的错误结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的案例.

这是我的 XML:

<?xml version="1.0" encoding="utf-8" ?>
<dogs>
    <dog type="Labrador">
        <Name>Doggy</Name>
    </dog>
    <dog type="Batard">
        <Name>Unknown</Name>
    </dog>
</dogs>

此 XML 与两个 Xslt 一起使用.这是常见的:

This XML is used with two Xslt. This is the common one:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:output method="text"/>

    <xsl:template match="dogs">
        <xsl:text>First template&#13;&#10;</xsl:text>
        <xsl:apply-templates select="." mode="othertemplate" />
    </xsl:template>
</xsl:stylesheet>

这是孩子之一:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:include href="transform.xslt"/>

    <xsl:template match="dogs" mode="othertemplate">
        <xsl:text>&#9;&#9;Other template</xsl:text>
    </xsl:template>
</xsl:stylesheet>

子项包括通用的(称为transform.xslt).

The child includes the common one (called transform.xslt).

当我执行孩子时,我得到了预期的结果:

When I execute the child, I get the expected result:

First template
        Other template

当我执行普通的时,我得到了这个奇怪的结果:

When I execute the common one, I get this strange results:

First template


        Doggy


        Unknown

常见的应用模式为othertemplate"的模板.此模式有时仅包含在子 xslt 中.

The common one applies a template with the mode "othertemplate". This mode is only included, some times, in the child xslt.

我想要那个,如果没有模式为othertemplate"的模板,那么什么都不应该输出.

I want that, if there's no template with mode "othertemplate", then nothing should be outputted.

我不想为所有不必使用此模板模式的 xslt 文件包含一个模式为othertemplate"且正文为空的模板...

I don't want to include a template with mode "othertemplate" with empty body for all xslt files that does not have to use this template mode...

我该怎么办?

谢谢

推荐答案

每种模式定义和选择了内置的XSLT模板.因此,选择了文本节点的内置模板并(根据定义)输出文本节点.

The built-in XSLT templates are defined and selected for every mode. So, the built-in template for text nodes is selected and (by definition) it outputs the text node.

要抑制这种情况,您需要使用空模板以所需模式覆盖文本节点的内置模板(也可能用于元素):

<xsl:template match="text()" mode="othertemplate"/>

在导入的样式表中包含以上内容.

这篇关于带模式的 XSLT 应用模板 - 没有匹配模式的错误结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆