我需要保存包含某些XML属性中的换行符的内容,而不是文本.应该选择该方法,以便我能够在XSLT 1.0/ESXLT/XSLT 2.0中对其进行解码
什么是最好的编码方法?
请建议/提出一些想法.
当我尝试将HTML文本或CDATA放入我的XML属性中时,我的解析器一直得到"XML解析器失败:未终止的属性".有没有办法做到这一点,还是标准不允许这样做?
我有XML数据,其中包含一个长属性值.该值不包含空格字符.我希望通过拆分XML文本来格式化XML以提高可读性,以便该行不超过最大列数.
有没有意义来实现这个目标?是否有任何中断字符可以放在每一行,以便XML解析器不将其视为空格(如visual basic中的下划线字符)?
例如:
<element attribute="this_is_a_very_long_text_attribute_value_that_I_want_to_split_on multiple_line_for_readability"/>
Run Code Online (Sandbox Code Playgroud)
我希望有类似的东西:
<element attribute="this_is_a_very_long
_text_attribute_value_that_I_want_to_split
_on multiple_line_for_readability"/>
Run Code Online (Sandbox Code Playgroud) 我有一个名为:description的属性,我希望在其中包含以下新行:
这是内容描述部分.
下载说明:
这是如何下载内容的内容.
热线支持:
这是内容的热线.
如何在xml中为它创建一个新行?
免责声明:以下是针对XML的罪行.这就是我试图用XSLT改变它的原因:)
我的XML目前看起来像这样:
<root>
<object name="blarg" property1="shablarg" property2="werg".../>
<object name="yetanotherobject" .../>
</root>
Run Code Online (Sandbox Code Playgroud)
是的,我将所有文本数据放在属性中.我希望XSLT能救我; 我想走向这样的事情:
<root>
<object>
<name>blarg</name>
<property1>shablarg</name>
...
</object>
<object>
...
</object>
</root>
Run Code Online (Sandbox Code Playgroud)
到目前为止,我实际上已经完成了所有这些工作,除了我对XML的罪恶更加特殊.一些标签看起来像这样:
<object description = "This is the first line
This is the third line. That second line full of whitespace is meaningful"/>
Run Code Online (Sandbox Code Playgroud)
我在linux下使用xsltproc,但它似乎没有任何保留空格的选项.我试图使用xsl:preserve-space和xml:space ="preserve"无济于事.我发现的每个选项似乎都适用于在元素本身内保留空格,而不是属性.每一次,以上都改为:
This is the first line This is the third line. That second line full of whitespace is meaningful
所以问题是,我可以保留属性空白吗?
我必须解析外部提供的XML,该XML具有包含换行符的属性.使用SimpleXML,换行似乎丢失了.根据另一个stackoverflow问题,换行符应该对XML有效(即使远远不够理想!).
他们为什么输了?[编辑] 我怎样才能保存它们?[/编辑]
这是一个演示文件脚本(请注意,当换行符不在属性中时,它们会被保留).
带嵌入式XML的PHP文件
$xml = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<Rows>
<data Title='Data Title' Remarks='First line of the row.
Followed by the second line.
Even a third!' />
<data Title='Full Title' Remarks='None really'>First line of the row.
Followed by the second line.
Even a third!</data>
</Rows>
XML;
$xml = new SimpleXMLElement( $xml );
print '<pre>'; print_r($xml); print '</pre>';
Run Code Online (Sandbox Code Playgroud)
print_r的输出
SimpleXMLElement Object
(
[data] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Title] …Run Code Online (Sandbox Code Playgroud) 根据这个问题:
XML属性中的换行符完全有效(尽管可能不推荐):
<xmltag1>
<xmltag2 attrib="line 1
line 2
line 3">
</xmltag2>
</xmltag1>
Run Code Online (Sandbox Code Playgroud)
当我使用LINQ to XML(System.Xml.Linq)解析这样的XML时,这些换行符将以静默方式转换为空格' '字符.
有没有办法告诉XDocument.Load()解析器保留这些换行符?
PS:我正在解析的XML是由第三方软件编写的,因此我无法改变换行符的写入方式.
我正在尝试制作一个 XML 文档。特别是,如下
<spirit:component xmlns:spirit="http://www.spiritconsortium.org/XMLSchema/SPIRIT/1.4"
xmlns:vendorExtensions="$IREG_GEN/XMLSchema/SPIRIT"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="$IREG_GEN/XMLSchema/SPIRIT/VendorExtensions.xsd
http://www.spiritconsortium.org/XMLSchema/SPIRIT/1.4
http://www.spiritconsortium.org/XMLSchema/SPIRIT/1.4/index.xsd">
Run Code Online (Sandbox Code Playgroud)
所以我为此制作了一个 perl 脚本,如下所示
use strict;
use warnings;
use Spreadsheet::ParseXLSX;
use XML::LibXML;
my $doc = XML::LibXML::Document->new('1.0', 'utf-8');
my $root = $doc->createElement('spirit:component');
#$root->appendChild($doc->createComment("JJ"));
$root->setAttribute('xmlns:spirit'=> "http://www.spiritconsortium.org/XMLSchema/SPIRIT/1.4");
$root->setAttribute('xmlns:vendorExtensions'=> "\$IREG_GEN/XMLSchema/SPIRIT");
$root->setAttribute('xmlns:xsi'=> "http://www.w3.org/2001/XMLSchema-instance");
$root->setAttribute('xsi:schemaLocation'=> "http://www.spiritconsortium.org/XMLSchema/SPIRIT/1.4
http://www.spiritconsortium.org/XMLSchema/SPIRIT/1.4
http://www.spiritconsortium.org/XMLSchema/SPIRIT/1.4/index.xsd");
$doc->setDocumentElement($root);
print $doc->toString(1);
Run Code Online (Sandbox Code Playgroud)
但问题是我得到了结果
<spirit:component xmlns:spirit="http://www.spiritconsortium.org/XMLSchema/SPIRIT/1.4" xmlns:vendorExtensions="$IREG_GEN/XMLSchema/SPIRIT" xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.spiritconsortium.org/XMLSchema/SPIRIT/1.4 											http://www.spiritconsortium.org/XMLSchema/SPIRIT/1.4 											http://www.spiritconsortium.org/XMLSchema/SPIRIT/1.4/index.xsd"/>
Run Code Online (Sandbox Code Playgroud)
特别是,这里有两个问题	,index.xsd"/>
我可以删除换行符,然后按如下方式解决它
$root->setAttribute('xsi:schemaLocation'=> "http://www.spiritconsortium.org/XMLSchema/SPIRIT/1.4 http://www.spiritconsortium.org/XMLSchema/SPIRIT/1
.4 http://www.spiritconsortium.org/XMLSchema/SPIRIT/1.4/index.xsd");
Run Code Online (Sandbox Code Playgroud)
特别是,我怎样才能/删除index.xsd"/>?我使用了错误的功能吗?
xml ×9
xslt ×2
.net ×1
c#-4.0 ×1
html ×1
indentation ×1
lines ×1
linq-to-xml ×1
new-operator ×1
newline ×1
perl ×1
php ×1
simplexml ×1
whitespace ×1