cdata-section-elements 不起作用 [英] cdata-section-elements not working
问题描述
我正在尝试通过设置全局参数在通过 XSLT(使用 Saxon-HE v9.7.0.14)生成的 xml 文件中设置密码.
I am trying to set a password in an xml file generated via XSLT (using Saxon-HE v9.7.0.14), by setting a global parameter.
密码可以包含任何字符,因此需要放在 CDATA
部分.
The password can contain any characters so it needs to be put in a CDATA
section.
我试图通过设置我的 xslt 的 xsl:output
元素的 cdata-section-elements
属性来包含密码元素的名称来实现这一点:>
I am trying to achieve this by setting the cdata-section-elements
attribute of my xslt's xsl:output
element to include the name of the password element:
<xsl:output method="xml" indent="yes" cdata-section-elements="password"/>
这不起作用.我在下面包含了示例代码、输入、xslt、当前输出和所需的输出.
This is not working. I have included example code, input, xslt, current output and desired output below.
我需要更改什么才能在 CDATA
部分中获取密码?
What do I need to change to get the password inside a CDATA
section?
计划:
using System;
using System.IO;
using Saxon.Api;
namespace XsltTest {
class Program {
static void Main(string[] args) {
var xslt = new FileInfo(@"transform.xslt");
var input = new FileInfo(@"input.xml");
var output = new FileInfo(@"output.xml");
var processor = new Processor();
var compiler = processor.NewXsltCompiler();
var executable = compiler.Compile(new Uri(xslt.FullName));
var transformer = executable.Load();
var destination = new DomDestination();
using (var inputStream = input.OpenRead()) {
transformer.SetInputStream(inputStream, new Uri(Path.GetTempPath()));
transformer.SetParameter(
new QName("password"),
new XdmAtomicValue("secret"));
transformer.Run(destination);
}
destination.XmlDocument.Save(output.FullName);
}
}
}
Transform.xslt:
<?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" cdata-section-elements="password"/>
<xsl:param name="password" />
<xsl:template match="@* | node()">
<bar>
<username>
<xsl:value-of select="//username"/>
</username>
<password>
<xsl:value-of select="$password"/>
</password>
</bar>
</xsl:template>
</xsl:stylesheet>
Input.xml:
<?xml version="1.0" encoding="utf-8" ?>
<foo>
<username>john</username>
</foo>
输出.xml:
<bar>
<username>john</username>
<password>secret</password>
</bar>
密码没有放在 CDATA
部分.
The password is not put inside a CDATA
section.
预期结果:
<bar>
<username>john</username>
<password><![CDATA[secret]]></password>
</bar>
推荐答案
xsl:output
上的选项会影响序列化器的动作,如果输出没有被序列化,则它们不起作用.您正在写入 DomDestination 而不是序列化程序(然后使用 DOM 方法序列化 DOM,该方法对 XSLT xsl:output 声明一无所知).
The options on xsl:output
affect the actions of the serializer, and if the output is not serialized, they have no effect. You are writing to a DomDestination rather than to a Serializer (and then serializing the DOM using DOM methods, which know nothing about the XSLT xsl:output declaration).
无论如何,您的前提是错误的:密码可以包含任何字符,因此需要将其放入 CDATA 部分."如果没有 cdata-section-elements,特殊字符将使用实体引用序列化,例如 <
和 &
,这应该可以正常工作.
In any case your premise is wrong: "The password can contain any characters so it needs to be put in a CDATA section." Without cdata-section-elements, special characters will be serialized using entity references such as <
and &
, which should work just fine.
这篇关于cdata-section-elements 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!