2016-03-28 86 views
0

我有在文字部分HTML和自定义的标签的XML,XML片断如下 -保留标签

<Text> 
    <English> 
     <p>Some text goes here with html tags like 
     <span class='classname'>more text</span> and 
     custom tags <CustomTag uid='value'>and 
     text</CustomTag></p> 
    </English> 
</Text> 

现在的问题有三个方面 -

  1. 保留HTML标记文字:孩子
  2. 流程自定义标签文本::孩子
  3. Expedition在输出字符串双引号

使用的XSL片段是这样的 -

这就是文字::孩子(英语或其他方式)是用来

<xsl:variable name='stepText'><xsl:apply-templates select='Text/child::*[name() = $language]'/></xsl:variable> 
<!-- This is the problem statement --> 
<xsl:variable name='stepTextNew'><xsl:copy-of select="cs:EscapeDoubleQuotes($stepText)"/></xsl:variable> 
jQuery('#stepText').html("<xsl:copy-of select='$stepTextNew'/>"); 

上面的代码片段在XSL逃脱双引号调用C#方法EscapeDoubleQuotes来自apply-templates返回的字符串。

这是Text ::孩子中的自定义标签 -

<xsl:template match="CustomTag"> 
    <xsl:element name="a"> 
     <xsl:attribute name="href">javascript:doSomething(1, '<xsl:value-of select="@uid"/>');</xsl:attribute> 
     <xsl:attribute name="onmouseover">javascript:onHover(1, '<xsl:value-of select="@uid"/>', true);</xsl:attribute> 
     <xsl:attribute name="onmouseout">javascript:onLeave(1, '<xsl:value-of select="@uid"/>', false);</xsl:attribute> 
    </xsl:element> 
</xsl:template> 

这是Text ::孩子中的HTML标签 -

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

现在,我面临的问题是传递给cs:EscapeDoubleQuotes的字符串中的标签会自动剥离。如果我尝试直接输出stepText,它确实包含标签,但stepTextNew不包含。我尝试调试到C#方法,并看到标签在输入参数本身中被剥离。

如果有人能够解释这个问题或提供解决上述三个问题的任何提示,这将会非常有帮助。

+0

张贴的所有代码是XML(未HTML)。 XML中的属性格式为name =“value”(或单引号)。一个属性有一个名称和一个VALUE。该值是c#中的字符串对象,因此您不需要在字符串对象内使用双引号。 – jdweng

+0

@jdweng请在回复前仔细阅读整篇文章。您的评论没有解决上述问题。如果您需要更多信息来了解问题陈述,请询问。 –

+0

为什么你认为你需要XSLT,C#和JQuery的混合?你能向我们展示你想为你发布的输入片段创建的HTML结果吗?如果您确实希望我们能够使用该C#方法,那么我们需要查看它的实现,以及您使用哪种XSLT处理器的精确描述。 –

回答

1

您似乎想用XSLT创建一些HTML节点,然后将它们序列化为字符串,您可以将它们传递给JQuery方法html。它似乎更容易我仅仅用纯XSLT来创建或填充stepText元素:

<div id="stepText"> 
    <xsl:copy-of select="$stepText"/> 
</div> 

如果你真的想转嫁的XSLT生成HTML到C#的扩展功能,在标记替换某些双引号那么我认为你需要确保你处理你通过的XPathNavigatorOuterXml

<xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
    xmlns:cs="http://example.com/cs" 
    exclude-result-prefixes="msxsl cs"> 

    <xsl:param name="language" select="'English'"/> 

    <xsl:output method="html" indent="yes"/> 

    <xsl:template match="/"> 
    <html lang="en"> 
     <head> 
     <title>Test</title> 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script> 
     <script> 
      $(document).ready(function() { 

      <xsl:variable name="stepText"> 
      <xsl:apply-templates select="Text/child::*[name() = $language]/node()"/> 
      </xsl:variable> 
      $('#stepText').html("<xsl:value-of select="cs:EscapeCrLf(cs:EscapeDoubleQuotes($stepText))"/>"); 
      }); 
     </script> 
     <script> 
      function doSomething(n, id) { 
      alert(n + ': ' + id); 
      } 
     </script> 

     </head> 
     <body> 
     <div id="stepText"></div> 
     </body> 
    </html> 
    </xsl:template> 

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

    <xsl:template match="CustomTag"> 
    <a href="#" onclick="doSomething(1, '{@uid}'); return false;"> 
     <xsl:apply-templates/> 
    </a> 
    </xsl:template> 

    <msxsl:script implements-prefix="cs" language="C#"> 
    public string EscapeDoubleQuotes(XPathNavigator input) { 
    return input.OuterXml.Replace("\"", "\\\""); 
    } 

    public string EscapeCrLf(string input) { 
    return input.Replace("\r", "\\r").Replace("\n", "\\n"); 
    } 
    </msxsl:script> 

</xsl:stylesheet> 

相对于输入

<Text> 
    <English> 
    <p> 
     Some text goes here with html tags like 
     <span class='classname'>more text</span> and 
     custom tags <CustomTag uid='value'> 
     and 
     text 
     </CustomTag> 
    </p> 
    </English> 
</Text> 

运行产生输出

<html lang="en"> 
    <head> 
    <META http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <title>Test</title><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script><script> 
      $(document).ready(function() { 


      $('#stepText').html("\r\n <p>\r\n  Some text goes here with html tags like\r\n  <span class=\"classname\">more text</span> and\r\n  custom tags <a href=\"#\" onclick=\"doSomething(1, 'value'); return false;\">\r\n  and\r\n  text\r\n  </a>\r\n </p>\r\n "); 
      }); 
     </script><script> 
      function doSomething(n, id) { 
      alert(n + ': ' + id); 
      } 
     </script></head> 
    <body> 
    <div id="stepText"></div> 
    </body> 
</html> 
+0

这有用,谢谢!我只是在分配给stepText html元素的时候添加了disable-output-escaping。但我仍然不明白为什么在传递给c#方法时将标签从值中剥离。当我尝试使用xslt模板replace-string修复双引号时,我得到的结果相同。 –