如何将XSL嵌入到XML中

如何将XSL嵌入到XML中,xml,xslt,Xml,Xslt,我正在寻找一种将XSL嵌入到XML中的解决方案,这样只有一个XML文件被发送到浏览器。我在这里尝试了Dimitre Novatchev提出的解决方案: 你好,我是福 问题是,使用这种解决方案,我无法找到在头部中包含样式元素的方法。在建议的解决方案中,头部和身体标记似乎没有任何效果,因为浏览器将在解析过程中自动添加它们,并且解决方案甚至可以在不包含这些标记的情况下工作 所以问题是:在上述解决方案中,如何在头部包含样式元素,如下所示: <head><style>body

我正在寻找一种将XSL嵌入到XML中的解决方案,这样只有一个XML文件被发送到浏览器。我在这里尝试了Dimitre Novatchev提出的解决方案:


你好,我是福
问题是,使用这种解决方案,我无法找到在头部中包含样式元素的方法。在建议的解决方案中,头部和身体标记似乎没有任何效果,因为浏览器将在解析过程中自动添加它们,并且解决方案甚至可以在不包含这些标记的情况下工作

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

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

此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>
    <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>

正文{字体大小:10pt;填充:20pt}
你好,我是福
包含XSLT样式表。起始PI指示浏览器将此样式表应用于自身

这样指定的转换将产生所需的结果

<?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>
    <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>

正文{字体大小:10pt;填充:20pt}
你好,我是福

那么,确切的结果是什么?请编辑问题并提供重要的、缺失的信息。嗨,迪米特,谢谢你的回复。我希望在头部包含样式元素,如下所示:body{font size:10pt;padding:20pt}。。。谢谢你让我知道如何接受答案!这是我在这个论坛上的第一个问题,所以我一直在寻找一个按钮,上面写着“接受”,但找不到它。我从未想过,这个复选标记不仅仅是一张图片:)