XSLT 转换未正确缩进
这是一个 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 输出并且设置为缩进,那么应该遵守这一点。
Here is an 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>
Here is the output of the XSLT Transformation:
<div class="Field"><span class="Label">Name:</span><span class="Input"></span></div>
Here is how I'm doing the transformation:
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);
}
}
}
Why is the format not indented? Later on in the output, some things are indented. Not sure why. I'm looking for a solution that will honor the format settings as specified in the XSLT. This code is used to write to any format (XML, HTML, text, etc.) so I don't want specific code that will only work with XML, for example. But if my XSLT has an output of XML and is set to indent, then that should be honored.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
默认情况下,
XmlWriter
(此处由XslCompiledTransform
隐式使用)不会缩进 xml,也不会自动使用 xslt 中指定的设置。您可以显式向
XmlWriter
提供设置,指定输出可以缩进,或者更好的方法是让XmlWriter
使用 xslt 提供的设置:By default
XmlWriter
(which here is being used implicitly byXslCompiledTransform
) does not indent your xml, and won't automatically use the settings speicifed in your xslt.You can either explicitly supply settings to an
XmlWriter
that specify that the output can be indented, or the better approach is to haveXmlWriter
use the settings supplied by the xslt:这已经很老了,但是当我遇到我认为是同一问题时遇到了它,并且没有一个答案对我有帮助。将下面的标签添加到我的 xslt 后,我最终在转换后的 xml 上获得了正确的缩进。
我猜想有一些空白会导致事情发生。只是想把它放在这里,以防其他人像我一样偶然发现。
This is pretty old, but I ran across it when I was experiencing what i believe to be the same issue and none of the answers helped me. I ended up getting the correct indentation on my transformed xml after adding the tag below to my xslt.
I guess there was some white space throwing things off. Just wanted to put it here in case someone else stumbles upon like I did.
HTML 不需要缩进,那为什么要缩进呢?
如果您确实需要缩进,请使用 XmlWriter 和 < a href="http://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.aspx" rel="nofollow">XmlWriterSettings 类写入 FileStream 对象。
HTML does not require indentation, so why do you?
If you really need indentation, use the XmlWriter and XmlWriterSettings classes to write to the FileStream object.