【问题标题】:XSLT:在 XML 中包含 Javascript
【发布时间】:2012-08-03 01:46:38
【问题描述】:

我有一个 XML(XHTML) 文件,我正在使用 XSLT 将其转换为另一个 XML 文件。我能够成功转换所有内容,但我需要在输出文件中包含一个我无法包含的 Javascript。谁能告诉我如何使用 XSLT 在输出文件中包含 javascript。

我的输入文件:

<?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-type" content="text/html;  charset=utf-8" />
    </head>  
    <body>
     <div class="TF" id="id8">
      <div class="iDev">
        <div class="q">
              T <input type="radio" name="o0" id="t0" onclick="getFeedback()"/> 
            </div>
      </div>
     </div>
    </body>
    </html>

期望的输出

<?xml version="1.0" encoding="utf-8"?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <meta http-equiv="Content-type" content="text/html;  charset=utf-8" />
        </head>  
        <body>
         <div class="QT" id="id10">
         <script type="text/javascript">
         <!-- //<![CDATA[
            var numQuestions = 4;
            var rawScore = 0;
            var actualScore = 0;
           function getAnswer()
           {
          }
            function calulate()
           {
          }
          //]]> -->
         </script>
          <div class="iDev">
            <div class="q">
              T <input type="radio" name="o0" id="t0" onclick="getFeedback()"/> 
            </div>
          </div>
         </div>
        </body>
       </html>

XSLT 部分:

<xsl:template match="xhtml:div[@id='id8']/@*">
  <xsl:attribute name="class">QT</xsl:attribute>
   <xsl:attribute name="id">id10</xsl:attribute>
   <script type="text/javascript">
   <![CDATA[
            var numQuestions = 4;
            var rawScore = 0;
            var actualScore = 0;
           function getAnswer()
           {
          }
            function calulate()
           {
          }
          ]]>
          </script>
</xsl:template>

我已经创建了标识模板并根据所需的输出更改了所需的属性值,但我仍然不知道如何在 &lt;div class="TF" id="id8"&gt; 这个标签之后包含 javascript。谢谢!

【问题讨论】:

    标签: xml xslt xhtml xslt-1.0


    【解决方案1】:

    请尝试以下 xsl:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
      <xsl:output method="xml" indent="yes"/>
      <xsl:template match="@* | node()">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:template>
      <xsl:template match="div[@id='id8']">
        <xsl:element name="div">
          <xsl:attribute name="class">QT</xsl:attribute>
          <xsl:attribute name="id">id10</xsl:attribute>
        </xsl:element>
        <script type="text/javascript">
          <![CDATA[         
           var numQuestions = 4;             
           var rawScore = 0;             
           var actualScore = 0;            
           function getAnswer()            
           {           
           }             
           function calulate()            
           {           
           }           
           ]]>
        </script>
      </xsl:template>
    </xsl:stylesheet>   
    

    已编辑

    输入的xml文件是这样的:

        <?xml version="1.0" encoding="utf-8"?>
    <html>
      <head>
        <meta http-equiv="Content-type" content="text/html;  charset=utf-8" />
      </head>
      <body>
        <div id="id8">
        </div>
          <div class="iDev">
            <div class="q">
              T <input type="radio" name="o0" id="t0" onclick="getFeedback()"/>
            </div>
          </div>
      </body>
    </html>  
    

    输出xml是这样的:

    <?xml version="1.0" encoding="utf-8"?>
    <html>
    <head>
    <meta http-equiv="Content-type" content="text/html;  charset=utf-8" />
    </head>
    <body>
    <div class="QT" id="id10" /><script type="text/javascript">
    
           var numQuestions = 4;             
           var rawScore = 0;             
           var actualScore = 0;            
           function getAnswer()            
           {           
           }             
           function calulate()            
           {           
           }           
    
        </script>
      <div class="iDev">
        <div class="q">
          T <input type="radio" name="o0" id="t0" onclick="getFeedback()" />
        </div>
      </div>
    </body>
    </html>  
    

    希望对你有帮助……

    【讨论】:

    • 感谢您的回答。它包含一次 Javascript,但从我的 XML 文件中删除了所有其他内容。输出中仅保留 Javascript。
    • 请查看我发布的答案。我稍微修改了一下,你建议的代码,它工作得很好。谢谢。 :)
    【解决方案2】:

    忘记注释,只需将 JavaScript 代码放在 CDATA 部分。如今,很少有浏览器存在裸脚本位于 &lt;script&gt; 节点中的问题。

    【讨论】:

    • 感谢您的回复,我在从 CDATA 部分删除注释后在模板匹配中添加了脚本,并且它在没有 CDATA 部分的所需输出中包含了 javascript,我认为这很好,但问题是它已包含Javascript 两次,一个接着另一个。请在问题中查看我的模板匹配。我已经更新了。
    【解决方案3】:

    我已按照此处给出的建议尝试了以下方法,并且效果很好。如果其他人遇到同样的问题,那么试试这个-

    <xsl:stylesheet version="1.0"
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
       xmlns:xhtml="http://www.w3.org/1999/xhtml"
       xmlns="http://www.w3.org/1999/xhtml"
       exclude-result-prefixes="xhtml">
    <xsl:output method="html" indent="yes" encoding="UTF-8"/>
    <xsl:strip-space elements="*" />
       <xsl:template match="@*|node()">
         <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
         </xsl:copy>
       </xsl:template>
    
        <xsl:template match="xhtml:div[@id='id8']" xmlns="http://www.w3.org/1999/xhtml">
        <div id="id10" class="QT">
            <xsl:apply-templates select="
              (@*[local-name()!='id']
                 [local-name()!='class'])
              | node()"/>
          </div>
            <script type="text/javascript">
              <![CDATA[         
               var numQuestions = 4;             
               var rawScore = 0;             
               var actualScore = 0;            
               function getAnswer()            
               {           
               }             
               function calulate()            
               {           
               }           
               ]]>
            </script>
        </xsl:template>
    <xsl:stylesheet>
    

    【讨论】:

    • 我已按照您的建议执行并观察到 ​​(1) 出现错误:“未定义前缀 'xhtml'。”如果我 remvoe shtml,它工作正常(2)它在 id10 div 和脚本标记中添加命名空间 xmlns="w3.org/1999/xhtml"(与您想要的输出不匹配)。
    • @Nirav 抱歉,我没有提供完整的代码。我添加了一个带有 xmlns 而不是 msxsl 的前缀 xhtml。我已经更新了我的答案。
    • @Nirav 如果您从您提供的 XSLT 中得到答案,那么我在复制时可能做错了什么。因此,我也修改并使用此代码得到了答案,但再次感谢您的回答。这确实是一个很大的帮助。 :)
    • @Nirav 请注意这里
      我还有一个属性 class="TF",我必须将其更改为“QT”。在您的输入文档中,您只提到了 id 属性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-17
    • 2013-09-23
    • 2013-05-16
    • 1970-01-01
    • 2010-12-11
    • 2021-05-24
    • 1970-01-01
    相关资源
    最近更新 更多