xml元素值需要保留space

XmlReaderSettings.IgnoreWhitespace 属性

如果忽略空白,则为 true;否则为 false。 默认值为 false。

示例

下面创建一个设置对象,该对象可用于构造一个读取器,该读取器去除处理指令、注释和微不足道的空白。

    StreamReader textreader = new StreamReader(filterXML);
    XmlReader reader = default(XmlReader);
    reader = XmlReader.Create(textreader, new XmlReaderSettings
    {
        CloseInput = true,
        ConformanceLevel = ConformanceLevel.Document,
        DtdProcessing = DtdProcessing.Ignore,
        IgnoreComments = true,
        IgnoreProcessingInstructions = false,
        IgnoreWhitespace = true,
        ValidationType = ValidationType.None
    });

注解

不被视为重要的空格包括空格、制表符和空白行,用于设置标记,以便提高可读性。 其中一个示例是元素内容中的空白。
此属性设置不会影响混合内容模式下标记之间的空白,也不会影响在属性范围内 xml:space=‘preserve’ 发生的空白。

<idPkg:Story xmlns:idPkg="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging" DOMVersion="17.0">
	<Story Self="u236" AppliedTOCStyle="n" UserText="true" IsEndnoteStory="false" TrackChanges="false" StoryTitle="$ID/" AppliedNamedGrid="n">
		<StoryPreference OpticalMarginAlignment="false" OpticalMarginSize="12" FrameType="TextFrameType" StoryOrientation="Horizontal" StoryDirection="LeftToRightDirection" />
		<InCopyExportOption IncludeGraphicProxies="true" IncludeAllResources="false" />
		<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/10 Notes Text">
			<CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]">
				<Properties>
					<Leading type="unit">13.5</Leading>
					<AppliedFont type="object">CompositeFont/~MHeHK-B+TimLTS-B</AppliedFont>
				</Properties>
				<Content> </Content>
			</CharacterStyleRange>
		</ParagraphStyleRange>
	</Story>
</idPkg:Story>
<?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"  xmlns:space="http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging">
    <xsl:output method="xml" indent="yes"/>
    <!-- This is an identity template - it copies everything
         that doesn't match another template -->
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
  <!-- This is the "other template". It says to use your BBB-DDD elements
       instead of the AAA element -->
   <xsl:template match="Content[text()=' ']">
    <xsl:element name="Content">
        <xsl:attribute name="xml:space">preserve</xsl:attribute>
        <xsl:value-of select="."></xsl:value-of>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

将上面的xml通过xsl转化成带有xml:space=‘preserve’ 的空值元素,新的xml

<Content xml:space="preserve"> </Content>
private string TransformXml(string xmlPath)
{
	string transformedXmlPath = System.IO.Path.GetTempFileName();
	XslCompiledTransform xslt = new XslCompiledTransform();
	string xsltFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "filterSpaceXslt.xslt");
	xslt.Load(xsltFilePath);
	xslt.Transform(xmlPath, transformedXmlPath);
	return transformedXmlPath;
}

你可能感兴趣的:(Technology,xml,java,前端)