XSLT 1.0 文本节点默认打印 [英] XSLT 1.0 text nodes printing by default

查看:34
本文介绍了XSLT 1.0 文本节点默认打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过 XSL xsl:template match="/" 但匹配模式那里没有提到触发我的问题.

I have looked at XSL xsl:template match="/" but the match pattern that triggered my question is not mentioned there.

我有一个相当复杂的 XML 结构:

I have a rather complex XML structure:

<?xml version="1.0" encoding="UTF-8"?>
<MATERIAL_DATA>
<LOG>
    <USER>Peter</USER>
    <DATE>2011-02-18</DATE>
    <MATERIALS>
        <item>
            <MATNR>636207</MATNR>
            <TEXTS>
                <item>
                    <TEXT>granola bar 40gx24</TEXT>
                </item>
            </TEXTS>
            <PRICES>
                <item>
                    <MATNR>636207</MATNR>
                    <COST>125.78</COST>
                </item>
            </PRICES>
            <SALESPRICES>
                <item>
                    <B01>
                        <MATNR>636207</MATNR>
                        <CURR>CZK</CURR>
                        <DATBI>9999-12-31</DATBI>
                        <DATAB>2010-10-05</DATAB>
                    </B01>
                    <B02>
                        <item>
                            <PRICE>477.60</PRICE>
                            <KUNNR>234567</KUNNR>
                        </item>
                    </B02>
                </item>
            </SALESPRICES>
        </item>
    </MATERIALS>
</LOG>
</MATERIAL_DATA>

现在,如果我应用以下 XSLT,我的输出看起来是正确的:

Now if I apply the following XSLT, my output looks correct:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>

<xsl:template match="node() | @*">
    <xsl:apply-templates select="* | @*" />
</xsl:template>

<xsl:template match="B02">
    <xsl:element name="Mi">
        <xsl:value-of select="item/KUNNR"/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

我得到输出:

<?xml version="1.0" encoding="UTF-8"?>
<Mi>234567</Mi>

但是如果我应用 XSLT:

But if I apply the XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="xml" indent="yes" encoding="UTF-8"/>

<xsl:template match="/*">
    <xsl:element name="MenuItems">
        <xsl:apply-templates select="LOG/MATERIALS/item/SALESPRICES/item"/>
    </xsl:element>
 </xsl:template>

<xsl:template match="B02">
    <xsl:element name="Mi">
        <xsl:value-of select="item/KUNNR"/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

输出如下:

<?xml version="1.0" encoding="UTF-8"?>
<MenuItems>

                        636207
                        CZK
                        9999-12-31
                        2010-10-05
<Mi>234567</Mi>
</MenuItems>

来自元素 的所有值都在输出中!但是为什么 - 我不匹配 !?怎么样

All values from the element <B01> are in the output! But why - I am not matching <B01>!? How does

<xsl:template match="node() | @*">
<xsl:apply-templates select="* | @*" />
</xsl:template>

让输出正确输出?我所做的就是匹配所有节点或属性,并将模板应用于所有内容或所有属性.但在我看来,它不应该对我何时完全匹配 产生影响!有没有人知道为什么会这样?

make the output come out correctly? All I am doing with this is match all nodes or attributes and apply-templates to everything or all attributes. But in my opinion it should not make a difference to when I exactly match <B01>! Does anyone have a clue why this is happening?

感谢您的理解和提示!

最好的问候,彼得

推荐答案

XSLT 包括以下 默认模板(等等):>

XSLT includes the following default templates (among others):

<!-- applies to both element nodes and the root node -->
<xsl:template match="*|/">
  <xsl:apply-templates/>
</xsl:template>

<!-- copies values of text and attribute nodes through -->
<xsl:template match="text()|@*">
  <xsl:value-of select="."/>
</xsl:template>

在您的第一个样式表中,您使用 node() 隐式匹配所有文本节点,从而覆盖默认操作.然后,在 B2 模板中,您输出目标值并且不再应用其他模板,这会停止处理.

In your first stylesheet you're implicitly matching all text nodes with node(), thus overriding the default action. Then, in the B2 template, you output your target value and apply no further templates, which stops processing.

在第二个样式表中,您将模板显式应用于 LOG/MATERIALS/item/SALESPRICES/item 的所有子项,从而导致默认模板处理您未显式处理的节点.因为您显式处理 B2 而不将模板应用于其子节点,所以永远不会为这些节点调用默认模板.但是默认模板应用于B1的子级.

In the second stylesheet, you explicitly apply templates to all children of LOG/MATERIALS/item/SALESPRICES/item, causing the default templates to process the nodes you don't explicitly handle. Because you explicitly handle B2 without applying templates to its children, the default templates are never invoked for those nodes. But the default templates are applied to the children of B1.

将以下模板添加到您的第二个样式表将覆盖文本节点的默认操作:

Adding the following template to your second stylesheet would override the default action for text nodes:

<xsl:template match="text()|@*"></xsl:template>

结果如下:

<?xml version="1.0" encoding="UTF-8"?>
<MenuItems><Mi>234567</Mi></MenuItems>

更多:

这篇关于XSLT 1.0 文本节点默认打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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