我正在尝试读取由xml架构定义的xml文档,其中一个元素的whiteSpace替换限制,但是当我尝试使用PowerShell访问该元素时,所有的空格仍然存在。
根据我的研究,whiteSpace替换限制应该告诉xml阅读器用一个空格(w3schools)替换所有换行符和标签
对于复制,我有:
的test.xml
<DOC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test.xsd">
<Description>
Here I have a long description
that takes multiple lines, but I'd
like it formatted nicely in this document -
even though it should be all one line when
parsed.
</Description>
</DOC>
test.xsd
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="DOC">
<xs:complexType>
<xs:all>
<xs:element name="Description" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="replace" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
然后在PowerShell v5.0中,我尝试以下命令:
$document = New-Object System.Xml.XmlDocument
$readersettings = New-Object -TypeName System.Xml.XmlReaderSettings
$readersettings.ValidationType = [System.Xml.ValidationType]::Schema
$readersettings.ValidationFlags = [System.Xml.Schema.XmlSchemaValidationFlags]::ProcessInlineSchema -bor [System.Xml.Schema.XmlSchemaValidationFlags]::ProcessSchemaLocation
$docPath = (Get-Item 'test.xml').FullName
$reader = [System.Xml.XmlReader]::Create($docPath, $private:readersettings)
$document.Load($private:reader)
$reader.Close()
Write-Output $document.DOC.Description
其中写出了以下内容:
Here I have a long description that takes multiple lines, but I'd like it formatted nicely in this document - even though it should be all one line when parsed.
我想要的是为了返回
Here I have a long description that takes multiple lines, but I'd like it formatted nicely in this document - even though it should be all one line when parsed.
我也尝试过:
xs:whiteSpace value="collapse"
Description
xs:normalizedString
Description
xs:token
如何告诉.NET xml reader这个元素的空格是否应该用一个空格替换?
尽管MSDN在其System.Xml.Schema.XmlTypeCode
枚举中支持xs:token
和xs:normalizedString
类型,但似乎.NET通过替换/折叠空格字符实际上符合标准< / p>
这不是很令人满意,但由于我知道要折叠哪个元素,我可以使用PowerShell的-replace
运算符为我折叠空白,然后使用字符串修剪来清除边缘上的任何额外空白
PS C:\>($document.DOC.Description -replace '(\s)+',' ').Trim()
在$document.Load()
期间有没有其他方法可以有效地扩展.NET类,所以我的空格在xml的加载时崩溃,而不仅仅是当我访问它时故意取代它?