【问题标题】:XSLT Transform not indenting properlyXSLT 转换没有正确缩进
【发布时间】:2011-08-21 00:12:50
【问题描述】:

这是一个 XSLT:

<xsl:stylesheet version="1.0" xmlns:P="http://abc.com/Xyz.xsd" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
    <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="Thing">
        <xsl:element name="div">
            <xsl:attribute name="class">
                <xsl:text>Field</xsl:text>
            </xsl:attribute>

            <xsl:element name="span">
                <xsl:attribute name="class">
                    <xsl:text>Label</xsl:text>
                </xsl:attribute>

                <xsl:value-of select="$displayName"/>
                <xsl:text>:</xsl:text>
            </xsl:element>
            <xsl:element name="span">
                <xsl:attribute name="class">
                    <xsl:text>Input</xsl:text>
                </xsl:attribute>
            </xsl:element>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

这是 XSLT 转换的输出:

<div class="Field"><span class="Label">Name:</span><span class="Input"></span></div>

这是我进行转换的方式:

XslCompiledTransform xslTransform = new XslCompiledTransform();

xslTransform.Load(xsltPath, new XsltSettings(true, true), new XmlUrlResolver());

using (FileStream outputStream = File.Create(outputPath))
{
    using (StringReader stringReader = new StringReader(xml))
    {
        using (XmlReader xmlReader = XmlReader.Create(stringReader))
        {
            xslTransform.Transform(xmlReader, outputStream);
        }
    }
}

为什么格式不缩进?稍后在输出中,有些东西是缩进的。不知道为什么。我正在寻找一种能够遵循 XSLT 中指定的格式设置的解决方案。此代码用于写入任何格式(XML、HTML、文本等),因此我不想要仅适用于 XML 的特定代码。但是如果我的 XSLT 有一个 XML 的输出并且设置为缩进,那么这应该被尊重。

【问题讨论】:

  • 那么你是如何运行转换的? xsl:output 是否被兑现取决于您如何运行转换,如果您使用 msdn.microsoft.com/en-us/library/ms163431.aspx,例如当您写入文件时,则 xsl:output 被兑现。写入流时也是如此。如果您写入 XmlWriter,则不会使用 xsl:output,除非您使用例如创建编写器。 XslCompiledTransform p = new XslCompiledTransform(); p.Load("sheet.xsl"); using (XmlWriter xw = XmlWriter.Create("result.html", p.OutputSettings)) { // call Transform here }.
  • 谢谢,我已经添加了有关如何进行转换的详细信息。
  • 为了解决您的问题,还必须提供 XML 文档(尽可能小)。请。

标签: c# .net xml transform xslt


【解决方案1】:

HTML 不需要缩进,为什么要缩进?

如果您确实需要缩进,请使用 XmlWriterXmlWriterSettings 类写入 FileStream 对象。

【讨论】:

    【解决方案2】:

    默认情况下XmlWriter(这里被XslCompiledTransform隐式使用)不会缩进你的xml,也不会自动使用你的xslt中指定的设置。

    您可以明确地向 XmlWriter 提供设置以指定输出可以缩进,或者更好的方法是让 XmlWriter 使用 xslt 提供的设置:

    XslCompiledTransform xslTransform = new XslCompiledTransform();
    xslTransform.Load(xsltPath, new XsltSettings(true, true), new XmlUrlResolver());
    
    using (XmlWriter writer = XmlWriter.Create(outputPath, xslTransform.OutputSettings))
    {
        using (StringReader stringReader = new StringReader(xml))
        {
            using (XmlReader xmlReader = XmlReader.Create(stringReader))
            {
                xslTransform.Transform(xmlReader, writer);
            }
        }
    }
    

    【讨论】:

    • 谢谢,但是XmlWriter 在哪里发挥作用呢?我看到你已经在 using 声明中声明了它,但它没有在其他地方使用。
    • @Josh 啊,我的错 - 我现在已经解决了这个问题。我不完全确定这会起作用,因为您的输出是 html 而不是 xml,但值得一试。
    • 我无法使用 XmlWriter,因为我正在寻找一种解决方案,该解决方案将遵循 XSLT 中指定的格式设置。此代码用于写入任何格式(XML、HTML、文本等),因此我不想要仅适用于 XML 的特定代码。如果我的 XSLT 有 XML 的输出并且设置为缩进,那么应该遵守。
    • @Josh 你试过这个吗?我刚刚查看了我的一些使用 XSLT(即输出纯文本)的代码生成 - 它也使用 XmlWriter 并且运行良好。我猜XmlWriter 只是用来写文本节点,从不在乎输出不是真的 xml。
    • 我试过了 - 输出是一样的(不缩进,然后突然缩进,然后又回到不缩进)。
    【解决方案3】:

    这已经很老了,但是当我遇到我认为是同一个问题时,我遇到了它,并且没有一个答案对我有帮助。将下面的标签添加到我的 xslt 后,我​​最终在转换后的 xml 上获得了正确的缩进。

    xsl:strip-space elements="*"
    

    我猜是有一些空白区域把东西扔掉了。只是想把它放在这里以防其他人像我一样偶然发现。

    【讨论】:

      猜你喜欢
      • 2011-07-10
      • 2021-11-12
      • 2012-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-21
      • 2017-11-09
      相关资源
      最近更新 更多