Cdata-section elements do not work

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.

The password can contain any characters, so you need to put it in the section CDATA.

I am trying to achieve this by setting the attribute of cdata-section-elementsmy xslt element xsl:outputto include the name of the password element:

<xsl:output method="xml" indent="yes" cdata-section-elements="password"/>

This does not work. I have included sample code, input, xslt, the current output, and the desired result below.

What do I need to change to get the password inside the section CDATA?

Program:

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>

Output.xml:

<bar>
  <username>john</username>
  <password>secret</password>
</bar>

Password does not fit inside the section CDATA.

Desired Result:

<bar>
  <username>john</username>
  <password><![CDATA[secret]]></password>
</bar>
+4
3

xsl:output , , . DomDestination, Serializer ( DOM DOM-, XSLT xsl: output).

: " , CDATA". cdata-section , &lt; &amp;, .

+1

, , .

xsltproc --stringparam password "1>2Ä-34" Transform.xslt Input.xml

<?xml version="1.0"?>
<bar>
  <username>john</username>
  <password><![CDATA[1>2Ä-34]]></password>
</bar>

password XSLT- <xsl:param name="password" />?

0

Michael Kay's answer explained the solution, which was that the attribute is cdata-section-elementsapplicable only when writing to Serializernot a DomDestination.

Here is the C # code for this:

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 serializer = new Serializer();
    using (var writer = new StreamWriter(output.FullName))
    using (var inputStream = input.OpenRead()) {
        serializer.SetOutputWriter(writer);
        transformer.SetInputStream(inputStream, new Uri(Path.GetTempPath()));
        transformer.SetParameter(
            new QName("password"),
            new XdmAtomicValue("secret"));
        transformer.Run(serializer);
    }
0
source

Source: https://habr.com/ru/post/1668700/


All Articles