简体   繁体   English

如何在XSLT中保留xml元素的空白

[英]How to preserve white spaces of an xml element in XSLT

When I transform an xml document into another xml document, I want to preserve white spaces. 当我将xml文档转换为另一个xml文档时,我想保留空白。

input XML: 输入XML:

<Item>
    <type>4</typr>
    <name>         test      </name>
</Item>

XSLT: XSLT:

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:preserve-space elements="name"/>

    <xsl:template match="Item">
       <xsl:element name="Itemtransformed">
           <xsl:element name="itemname"><xsl:value-of select="//name></xsl:element>
       </xsl:element>
    </xsl:template>
</xsl:stylesheet>

Expected output: 预期产量:

<Itemtransformed>
    <itemname>         test      </itemname>
</Itemtransformed>

When I tried to transform this in .Net using System.Xml.Xsl.XslCompiledTransform.Transform(IXPathNavigable input, XsltArgumentList arguments, XmlWriter results), I get below error 当我尝试使用System.Xml.Xsl.XslCompiledTransform.Transform(IXPathNavigable输入,XsltArgumentList参数,XmlWriter结果)在.Net中进行转换时,出现以下错误

"White space cannot be stripped from input documents that have already been loaded. Provide the input document as an XmlReader instead." “不能从已经加载的输入文档中剥离空格。而应将输入文档作为XmlReader提供。”

Any idea how to fix it or any other possible way to preserve white space. 任何想法如何解决它或任何其他可能的方式来保留空白。

Thank you for your valuable time 谢谢您的宝贵时间

The issue is nothing to do with the leading and trailing whitespace around "test", which XSLT will preserve automatically whatever you say in xsl:strip-space and xsl:preserve-space . 问题与“ test”周围的前后空格无关,XSLT会自动保留您在xsl:strip-spacexsl:preserve-space所说的内容。 Rather the issue here is a limitation in the Microsoft System.Xml.Xsl implementation of xsl:strip-space - a restriction which is explained (not especially well) in the question cited by @RomanSS. 相反,这里的问题是xsl:strip-space的Microsoft System.Xml.Xsl实现中的限制-@RomanSS引用的问题中对此限制的解释(不是特别好)。

Basically, if you supply a pre-built DOM tree as input to an XSLT processor, then the requirement in the XSLT spec to strip whitespace text nodes is rather challenging, and it looks as if Microsoft have responded to the challenge by imposing a restriction. 基本上,如果您提供一个预先构建的DOM树作为XSLT处理器的输入,那么XSLT规范中剥离空白文本节点的要求就相当具有挑战性,并且看起来Microsoft对此施加了限制来应对挑战。 Other ways to meet this requirement include: 满足此要求的其他方法包括:

(a) Modify the supplied DOM tree in-situ to delete the whitespace text nodes (a)就地修改提供的DOM树以删除空白文本节点

(b) Wrap the supplied DOM tree in a wrapper that makes the whitespace text nodes invisible when navigating the tree (b)用包装器包装提供的DOM树,使在导航树时空白文本节点不可见

(c) Bulk copy the supplied DOM tree to a new tree in which the whitespace text nodes are omitted. (c)将提供的DOM树批量复制到新树,其中省略了空白文本节点。

Saxon uses all three strategies under difference circumstances; 撒克逊人在不同情况下使用这三种策略。 all of them are expensive. 所有这些都很昂贵。 It's far more efficient to do the whitespace stripping while parsing the document and building the tree, which is what Microsoft are forcing you to do by imposing a restriction. 在解析文档并构建树时进行空格剥离要高效得多,这是Microsoft通过施加限制来强制您执行的操作。

But none of this has anything to do with your problem. 但这与您的问题无关。 If you get rid of the xsl:strip-space and xsl:preserve-space declarations, the whitespace around "test" will be preserved as you want it to be. 如果您摆脱了xsl:strip-spacexsl:preserve-space声明,“ test”周围的空白将按照您希望的那样保留。 The xsl:strip-space and xsl:preserve-space declarations only affect text nodes that consist entirely of whitespace. xsl:strip-spacexsl:preserve-space声明仅影响完全由空格组成的文本节点。

Maybe this is what you are looking for. 也许这就是您想要的。 There doesn't seem to exist problem in the code shown. 所示的代码中似乎没有问题。 White space cannot be stripped from input documents that have already been loaded. 不能从已加载的输入文档中删除空格。 Provide the input document as an XmlReader instead 而是将输入文档提供为XmlReader

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM