• 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
        });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    注解

    不被视为重要的空格包括空格、制表符和空白行,用于设置标记,以便提高可读性。 其中一个示例是元素内容中的空白。
    属性设置不会影响混合内容模式下标记之间的空白,也不会影响在属性范围内 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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    <?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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

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

    <Content xml:space="preserve"> </Content>
    
    • 1
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 相关阅读:
    在Mysql中一条查询SQL的执行过程是怎样的
    for_each和transform算法(C++)
    使用idea 中的rest 将 git 合并部分分支代码到主分支
    pyqt实现简易浏览器
    Android开发中用于方便的记录当前与开始时间的差值
    MAVEN介绍以及安装配置
    工作中Java Stream的简单应用
    Java开发之多线程包含代码调试【面试篇 持续更新】
    机器视觉工程师越学习越谦虚-庆幸自己始终保持清醒
    Python | Web | request模块使用不完全总结
  • 原文地址:https://blog.csdn.net/m0_38013946/article/details/132878809