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

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

问题描述

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

I have a XML file and an external XSLT file.

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

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>

如何使用内联XSLT代替?这可能吗?如果是,怎么办?

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

推荐答案

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

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

XSLT 是XML文件 ,因此您只需要确保将其放在XML文件的document元素中即可,这样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.

实际上,它在XSLT规范中进行了描述:

2.7嵌入样式表

通常,XSLT样式表是具有以下内容的完整XML文档: xsl:stylesheet元素作为document元素.但是,XSLT 样式表也可以嵌入到另一个资源中.两种形式 可以嵌入:

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:

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

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

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

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

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.

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

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>

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

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.

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

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

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