对 XML 文件使用内联 XSLT [英] Using inline XSLT for an XML file

查看:31
本文介绍了对 XML 文件使用内联 XSLT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XML 文件和一个外部 XSLT 文件.

目前,在我的 XML 中,我使用 href 引用外部 XSLT 链接:

 <?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/xsl" href="stylesheet.xsl" ?><我的标签><t1></t1><t2></t2><t3><t3><我的标签>

如何改用内联 XSLT?这可能吗?如果是,如何?

解决方案

是的,可以将 XSLT 嵌入到您的 XML 中.

XSLT 是一个 XML 文件,因此您只需确保将它放在 XML 文件的文档元素中,以便 XML 文件仍然是良好的.

事实上,它在 XSLT 规范中有描述:

<块引用>

2.7 嵌入样式表

通常,XSLT 样式表是一个完整的 XML 文档,其中包含xsl:stylesheet 元素作为文档元素.但是,一个 XSLT样式表也可以嵌入到另一个资源中.两种形式嵌入是可能的:

  • XSLT 样式表可以以文本形式嵌入到非 XML 中资源,或
  • xsl:stylesheet 元素可能出现在 XML 文档中,而不是作为文档元素.

为了便于第二种嵌入形式,xsl:stylesheet 元素允许具有指定唯一标识符的 ID 属性.

注意:为了将这样的属性与 XPath id 一起使用函数,它实际上必须在 DTD 中声明为 ID.

下面的例子展示了如何处理xml-stylesheet指令 [XML 样式表] 可用于允许文档包含自己的样式表.URI 引用使用相对 URI用于定位 xsl:stylesheet 元素的片段标识符:

<头><xsl:stylesheet id="style1"版本=1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:fo="http://www.w3.org/1999/XSL/Format"><xsl:import href="doc.xsl"/><xsl:template match="id('foo')"><fo:block font-weight="bold"><xsl:apply-templates/></fo:block></xsl:模板><xsl:template match="xsl:stylesheet"><!-- 忽略--></xsl:模板></xsl:stylesheet><身体><para id="foo">...</doc>

<块引用>

注意: 嵌入到文档中的样式表应用或可能包含或导入到样式表中如此嵌入通常需要包含一个模板规则指定要忽略 xsl:stylesheet 元素.

根据您计划如何利用它,可能不支持嵌入的样式表.例如,在 IE 6/7/8 中. 有一些解决方法.

I have a XML file and an external XSLT file.

Currently, within my XML I refer to an external XSLT link using an href:

 <?xml version="1.0" encoding="utf-8"?>
    <?xml-stylesheet type="text/xsl" href="stylesheet.xsl" ?>
     <mytag>
         <t1> </t1>
         <t2> </t2>
         <t3> <t3>
     <mytag>

How can I use inline XSLT instead? Is this possible? If yes, how?

解决方案

Yes, it is possible to embed the XSLT inside of your XML.

XSLT is an XML file, so you would just need to make sure that you put it inside of the document element of your XML file, so that the XML file is still well-formed.

In fact, it is described in the XSLT specification:

2.7 Embedding Stylesheets

Normally an XSLT stylesheet is a complete XML document with the xsl:stylesheet element as the document element. However, an XSLT stylesheet may also be embedded in another resource. Two forms of embedding are possible:

  • the XSLT stylesheet may be textually embedded in a non-XML resource, or
  • the xsl:stylesheet element may occur in an XML document other than as the document element.

To facilitate the second form of embedding, the xsl:stylesheet element is allowed to have an ID attribute that specifies a unique identifier.

NOTE: In order for such an attribute to be used with the XPath id function, it must actually be declared in the DTD as being an ID.

The following example shows how the xml-stylesheet processing instruction [XML Stylesheet] can be used to allow a document to contain its own stylesheet. The URI reference uses a relative URI with a fragment identifier to locate the xsl:stylesheet element:

<?xml-stylesheet type="text/xml" href="#style1"?>
<!DOCTYPE doc SYSTEM "doc.dtd">
<doc>
<head>
<xsl:stylesheet id="style1"
                version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:import href="doc.xsl"/>
<xsl:template match="id('foo')">
  <fo:block font-weight="bold"><xsl:apply-templates/></fo:block>
</xsl:template>
<xsl:template match="xsl:stylesheet">
  <!-- ignore -->
</xsl:template>
</xsl:stylesheet>
</head>
<body>
<para id="foo">
...
</para>
</body>
</doc>

NOTE: A stylesheet that is embedded in the document to which it is to be applied or that may be included or imported into an stylesheet that is so embedded typically needs to contain a template rule that specifies that xsl:stylesheet elements are to be ignored.

Depending on how you plan to leverage it, embedded stylesheets may not be supported. For instance, in IE 6/7/8. There are some workarounds.

这篇关于对 XML 文件使用内联 XSLT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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