如何复制元素内容而不仅仅是字符串?
我的 XML 中确实有一些样式。 XslCompiledTransform Transform 的输出结果为不包含这些样式标签的 HTML。 有什么方法可以告诉 XslCompiledTransform 保留它们吗?或者 XslCompiledTransform 应该默认保留它们?
这是我的 xml,
> <codeSnippetFull>
> <span class="kwrd">event</span>
> OnCreate {
> <span class="kwrd">if</span>
> (Count == 0)
> AddE(D); <span class="rem">// comment </span>
> }
> </codeSnippetFull>
我的 xslt 只是提取整个元素
<xsl:value-of select="THelpDocument/HelpBody/example/codeSnippetFull"/>
,这是我的 C#,除了应用的默认值之外,我什么都没有:
// Load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(args[0]);
......
xslt.Transform(xmlFileName, Path.Combine(htmlOutputPath, Path.GetFileNameWithoutExtension(xmlFileName) + ".html"));
我没有从 xslt 进行样式设置的原因是因为块可能具有应用的样式和数量,混合与随机文本。它对段落中的某些单词进行样式化。
My XML does have some styling within it.
The output of the XslCompiledTransform Transform results in HTML which does not have these style tags.
Is there some way to tell the XslCompiledTransform to keep them? or should the XslCompiledTransform keep them by default?
here's my xml
> <codeSnippetFull>
> <span class="kwrd">event</span>
> OnCreate {
> <span class="kwrd">if</span>
> (Count == 0)
> AddE(D); <span class="rem">// comment </span>
> }
> </codeSnippetFull>
my xslt just pulls out the whole element
<xsl:value-of select="THelpDocument/HelpBody/example/codeSnippetFull"/>
here's my C#, I've nothing other than the defaults applied:
// Load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(args[0]);
......
xslt.Transform(xmlFileName, Path.Combine(htmlOutputPath, Path.GetFileNameWithoutExtension(xmlFileName) + ".html"));
The reason I'm not doing the styling from the xslt is because block could have and amount of styles applied, mixed in with random text. It's styling certain words in paragraphs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 XSLT 代码
实际上并不复制
元素的(标记)内容。
用于创建文本节点。 Value-of 将选定的节点转换为字符串。元素的字符串值是作为所选元素后代的文本节点的串联。节点集的字符串值(例如:选择了多个元素)是集合中第一个节点的字符串值。要复制 XSLT 中的元素,您可以使用
来复制所选节点,但不复制其属性或子节点
来复制整个选定的 XML 片段(=还有所有后代和属性)
的递归模板Your XSLT code
doesn't actually copy the (markup) contents of the
<codeSnippetFull>
element.<xsl:value-of>
is used to create text nodes. Value-of converts the selected node to a string. String value of an element is the concatenation of the text nodes that are descendants of the selected element. String value of a node set (for example: multiple elements selected) is the string value of the first node in the set.To copy elements in XSLT you could use
<xs:copy>
that copies the selected node but not its attributes or children<xs:copy-of>
that copies the whole selected XML fragment (=also all descendants and attributes)<xs:copy>