2017-02-28 51 views
0

你能告诉我如何访问xslt中的变量吗? 。我让变量js。我要打印它的价值。所以我在XSL指定变量值:变量 当我尝试打印变量值,它给了我string,而不是在这里评价 是我的代码 http://xsltransform.net/pPJ8LWb如何在xslt中访问变量?

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" /> 

    <xsl:template match="/"> 
     <hmtl> 
     <head> 
      <title>New Version!</title> 
     </head> 
     <script> 
      console.log('starte'); 
      var start = 5; 
      '<xsl:variable name="start">{start}</xsl:variable>' 
      console.log('<xsl:value-of select="$start"/>') 

     </script> 
     </hmtl> 
    </xsl:template> 


</xsl:transform> 

预计输出 :: 5 任何更新

+0

我对你刚才的问题(http://stackoverflow.com/questions/42498064/why-value-of-not-working-in-xslt-in-loop),这是与此有关做出评论。 XSLT无法访问JavaScript变量,因为在创建HTML和Javascript时,XSLT(和XML)早已消失,无法访问。 XSLT用于生成HTML(和JavaScript),但不用于处理该HTML。你应该首先尝试手动编写你想要的HTML,把XSLT放在一边,直到你确切地知道你想要什么样的HTML和JavaScript。 –

+0

可以请你回答这个问题http://stackoverflow.com/questions/42505285/how-to-change-the-tag-name-in-xslt – user5711656

回答

0

好吧,我可以建议你这样做:

演示 - https://jsfiddle.net/skyr9999/behf2h7t/

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" /> 

    <xsl:template match="/"> 
     <hmtl> 
     <head> 
      <title>New Version!</title> 
     </head> 
     <script> 
      console.log('starte'); 
      var start = 5; 
      document.write('<xsl:variable name="start">'+start+'</xsl:variable>'); 
      console.log("<xsl:value-of select='"+start+"'/>") 

     </script> 
     </hmtl> 
    </xsl:template> 


</xsl:transform>