XslCompiledTransform忽略.NET Framework 4.6及更高版本上的DTD架构

问题描述 投票:0回答:1

目标是通过运行XSL转换将XML标签与DTD中指定的键(短标签与完整标签/参考标签名称)交换:

  • XSL确定XML是哪种格式:短格式或完整格式
  • XSL尝试从DTD中找到XML标记的相反密钥(完整的简称,完整的简称)
  • XSL用找到的相反密钥替换XML标签

所以XML

<!DOCTYPE Note SYSTEM "note.dtd">
<Note>
    <To>Tove</To>
    <From>Jani</From>
    <Heading>Reminder</Heading>
    <Body>Don't forget me this weekend</Body>
</Note>

成为

<note>
    <x1>Tove</x1>
    <x2>Jani<x2/>
    <x3>Reminder</x3>
    <x4>Don't forget me this weekend</x4>
<note>

为此任务,使用了以下DTD(仅指定了一个替换项以使其具有代表性)

<!ELEMENT Note (To,From,Heading,Body)>
<!ELEMENT To (#PCDATA)>
<!ATTLIST To
  refname (To) #FIXED "To"
  shortname (to) #FIXED "x1">
<!ELEMENT From (#PCDATA)>
<!ELEMENT Heading (#PCDATA)>
<!ELEMENT Body (#PCDATA)>

正在运行以下XSL

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" version="1.0">
  <xsl:variable name="target">
    <xsl:choose>
      <xsl:when test="local-name(/*)='Note'">short</xsl:when>
      <xsl:otherwise>reference</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:template match="*">
    <xsl:variable name="target-name">
      <xsl:choose>
        <xsl:when test="($target='short') and not(@shortname)">
          <xsl:value-of select="name()"/>
        </xsl:when>
        <xsl:when test="$target='short'">
          <xsl:value-of select="@shortname"/>
        </xsl:when>
        <xsl:when test="not(@refname)">
          <xsl:value-of select="name()"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="@refname"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:element name="{$target-name}">
      <xsl:copy-of select="@*[not(name()='refname' or name()='shortname')]"/>
      <xsl:apply-templates select="*|text()"/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="text()">
    <xsl:copy/>
  </xsl:template>
</xsl:stylesheet>

.NET Framework 4.6.NET Framework 4.5

它可以在.NET Framework 4.5(或MSBuild 14)及更低版本中完美运行,但是一旦代码在更高版本上运行,则替换就不会发生!在这个问题上花了几个小时之后,我可以说较高版本中完全忽略了DTD:我故意将错误的DTP路径放置到XML顶部:

  • 。NET Framework 4.5及更低版本-预期错误:An error has occurred while opening external DTD 'file:///.../1note.dtd': Could not find file 'C:\...\1note.dtd'.
  • 。NET Framework 4.6及更高版本-无需转换,将返回输入XML。

我编写了以下C#代码进行演示

using System;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Xsl;

using Xunit;

public class Tests
{
    private const string DtdDirectoryPath = @"c:\source\...\Resources";
    private const string DtdFileName = "note.dtd";

    private const string RootTag = "Note";
    private string xml = @"
<Note>
  <To>Tove</To>
  <From>Jani</From>
  <Heading>Reminder</Heading>
  <Body>Don't forget me this weekend</Body>
</Note>";

    [Fact]
    public void Test()
    {
        // copy the DTD to the expected location
        var dtdFilePath = Path.Combine(Environment.CurrentDirectory, DtdFileName);
        File.Copy(Path.Combine(DtdDirectoryPath, DtdFileName), dtdFilePath, overwrite: true);

        XslCompiledTransform xslt;
        using (var stringReader = new StringReader(Resources.SwitchTagNamesXSL))
        using (var xmlReader = new XmlTextReader(stringReader))
        {
            xslt = new XslCompiledTransform(enableDebug: true);
            xslt.Load(xmlReader);
        }

        // add the DTD definition at the XML top in order the XSL transformation pass
        this.xml = this.xml.Insert(
            0,
            new XDocumentType(RootTag, publicId: null, systemId: DtdFileName, internalSubset: null).ToString());

        Console.WriteLine("Initial XML with DTD at the top");
        Console.WriteLine(this.xml);

        var output = new XDocument();
        using (var writer = output.CreateWriter())
        using (var stringReader = new StringReader(this.xml))
        using (var reader = XmlReader.Create(
            stringReader,
            new XmlReaderSettings { DtdProcessing = DtdProcessing.Parse })) // this flag is important to force DTD Processing
        {
            xslt.Transform(reader, writer);
        }

        Console.WriteLine("Output");
        Console.WriteLine(output);
    }
}

好像该功能在最新的.NET Framework中以某种方式丢失了,因为我一直在考虑编写自定义代码的变通方法

  • XSL中的脚本或
  • 在C#中进行转换

但是我希望有人能对不当行为有所了解。

PS在我低估了DTD被完全忽略之前,我花了很多时间搜索该问题并进行了许多没有发生的实验:

  • 不同的测试框架:XUnin,NUnit
  • 在不同的机器上运行
  • x64 x86
  • 已在XslCompiledTransform中启用脚本
  • 为XslCompiledTransform设置不同的设置
c# .net xslt .net-4.5
1个回答
0
投票

我认为您的XmlReaderSettings需要使用例如明确定义XmlResolver

© www.soinside.com 2019 - 2024. All rights reserved.