如何将 XSL 嵌入到 XML 中 [英] How to Embed XSL into XML

查看:32
本文介绍了如何将 XSL 嵌入到 XML 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种将 XSL 嵌入到 XML 中的解决方案,因此只有 1 个 XML 文件被发送到浏览器.我在这里尝试了 Dimitre Novatchev 提出的解决方案:Embed xsl into an XML file

I am looking for a solution to embed XSL into XML so there is only 1 XML file that is sent to the browser. I tried the solution proposed by Dimitre Novatchev here: Embed xsl into an XML file

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes"/>    <xsl:variable name="vEmbDoc">
    <doc>
        <head></head>
        <body>
            <para id="foo">Hello I am foo</para>
        </body>
    </doc>
</xsl:variable>
<xsl:template match="para">
  <h1><xsl:value-of select="."/></h1>
</xsl:template>
<xsl:template match="xsl:template"/></xsl:stylesheet>

问题是,使用此解决方案,我找不到在头部内包含样式元素的方法.似乎在提议的解决方案中 head 和 body 标签没有任何效果,因为浏览器会在解析过程中自动添加它们,即使不包含这些标签,解决方案也能正常工作.

The problem is that with this solution I can not find a way to include style element inside the head. Seems like in the proposed solution head and body tags do not have any effect since the browser will add them automatically during parsing and the solution works even without having those tags included.

所以问题是:如何在上述解决方案的头部包含样式元素,如下所示:

So the question is: How to include style element in the head in the above mentioned solution that will look like this:

<head><style>body {font-size:10pt;padding:20pt} </style></head>

推荐答案

此 XML 文档:

<?xml-stylesheet type="text/xsl" href="myEmbedded.xml"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 exclude-result-prefixes="xsl">
 <xsl:output omit-xml-declaration="yes"/>
    <xsl:variable name="vEmbDoc">
        <doc>
            <head>
              <style>body {font-size:10pt;padding:20pt}</style>
              </head>
            <body>
                <para id="foo">Hello I am foo</para>
            </body>
        </doc>
    </xsl:variable>
    <xsl:template match="para">
      <h1><xsl:value-of select="."/></h1>
    </xsl:template>

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

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

    <xsl:template match="xsl:template"/>

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

包含一个 XSLT 样式表.起始 PI 指示浏览器将此样式表应用于自身.

如此指定的转换,产生了想要的结果:

    <html>

   <head xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

              <style>body {font-size:10pt;padding:20pt}</style>
              </head>

   <body xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

      <h1>Hello I am foo</h1>

   </body>

</html>

这篇关于如何将 XSL 嵌入到 XML 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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