如何使用 XSL 在 XML 文档中打印元素标签 [英] How to print element tags in XML document using XSL

查看:35
本文介绍了如何使用 XSL 在 XML 文档中打印元素标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常适合 XSL 的初学者.我正在尝试将 2 个 XML 文档转换为一个新的 XML 文档.我似乎无法将标签打印到新文档.仅打印元素的内容.

Very much a beginner with XSL. I'm trying transform 2 XML documents into a new XML document. I can't seem to get the tags to print to the new document. Only the content of the elements is printing.

XML

<books>
  <book>
    <name>Cat in the Hat</name>
    <author>Me</name>
    ...
    ...
  </book>
  ...
  ...
</book>

XSL

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

    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:element name="books">
        <xsl:template match="books">
            <xsl:element name="book">
                <xsl:for-each select="book">
                    <xsl:element name="name"><xsl:value-of select="name"/></xsl:element>
                    <xsl:element name="author"><xsl:value-of select="author"/></xsl:element>
                </xsl:for-each>
            </xsl:element>
        </xsl:template>
    </xsl:element>

</xsl:stylesheet>

输出到 XML 文件戴帽子的猫我

OUTPUT to XML file Cat in the Hat Me

我需要输出:

<books>
  <book>
    <name>Cat in the Hat</name>
    <author>Me</author>
  </book>
</books>

我做错了什么?

推荐答案

严格来说,当你运行输出时,你实际上会得到一个错误,因为 MathiasMuller 在评论中说,xsl:element must不是顶级元素(即它不能是 xsl;stylesheet 的直接子元素).我猜 Ecplise 可能只是忽略了这一点.

Strictly speaking, you actually be getting an error when you run the output, as because MathiasMuller says in comments, xsl:element must not be a top-level element (i.e it cannot be a direct child of xsl;stylesheet). I am guessing Ecplise may just be ignoring that.

如果您只获得文本输出,那是因为 XSLT 的 内置模板规则,当您的 XML 中没有任何匹配的模板时,这些规则将用于匹配 XML 中的节点.实际上,这些只会跳过元素,并最终输出它们找到的任何文本节点.

If you are, however, getting only text output, it is because of XSLT's built-in template rules, which will be used to match nodes in the XML when there are not any matching templates in your XML. Effectively these will just skip over elements, and end up outputting any text nodes they find.

如果您确实希望从与输入相同的输出开始(如果最终您只想更改部分 XML,这实际上是一种明智的方法),您应该从 XSLT 身份模板,它自己按原样复制所有节点和属性(这意味着内置模板不会习惯了).

If you do want to start off with the same output as the input (which is actually a sensible approach if ultimately you wish only to only change part of the XML), you should start off with the XSLT identity template, which on its own copies all nodes and attributes as-is (and will mean the built-in templates will not get used).

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 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:transform>

然后,当您想对输出进行更改时,您只需为要更改的节点/属性编写模板.例如,假设您希望从 book 元素下删除一个 date 元素,那么您只需添加此模板

Then, when you want to make changes to the output, you only need to write templates for the nodes/attributes you wish to change. For example, suppose you had a date element you wished to remove from under the book element, then you would just add this template

<xsl:template match="date" />

或者你想将 name 重命名为 title 那么你可以这样做

Or maybe you want to rename name to title then you would do this

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

请注意,如果您只是输出固定的元素名称,则实际上没有必要使用 xsl:element.

Note there is no real need to use xsl:element if you are just outputting a fixed element name.

这篇关于如何使用 XSL 在 XML 文档中打印元素标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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