Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
xml:如何缩进子元素但将属性保留在新行上_Xml_Perl_Xslt_Tidy_Xmllint - Fatal编程技术网

xml:如何缩进子元素但将属性保留在新行上

xml:如何缩进子元素但将属性保留在新行上,xml,perl,xslt,tidy,xmllint,Xml,Perl,Xslt,Tidy,Xmllint,我有一个xml,如下所示 <!DOCTYPE parent [ <!ENTITY entity1 "value"> ]> <main> <parent attr1="str1" attr2="str2"> <child childattr1="str3" childattr2="&entity1;" /> <child childattr1="str4" childattr2=

我有一个xml,如下所示

<!DOCTYPE parent [
<!ENTITY entity1 "value"> 
]>
<main>
<parent attr1="str1"
        attr2="str2">
<child childattr1="str3"
       childattr2="&entity1;" />
<child childattr1="str4"
       childattr2="&entity1;" />
</parent>
</main>

我需要缩进子元素,保持xml文件的其余部分不变(即dtd部分和实体不应被删除,属性应位于新行)。xml最终应该是这样的:

<!DOCTYPE parent [
<!ENTITY entity1 "value"> 
]>
<main>
    <parent attr1="str1"
            attr2="str2">
        <child childattr1="str3"
               childattr2="&entity1;" />
        <child childattr1="str4"
               childattr2="&entity1;" />
    </parent>
</main>

我试过使用
xmllint
tidy
xmllint
正在缩进子元素,但它没有将属性保留在新行中<另一方面,code>tidy可以选择将属性保留在新行中,但无法缩进子元素。我也尝试过使用perl正则表达式。这可能是通过XSLT实现的,但我对它不太熟悉。

在我看来,它的实用程序几乎可以通过它的
缩进选项实现您想要的功能:

$ xml_pp -s indented_a foo.xml
<!DOCTYPE parent [
<!ENTITY entity1 "value">
]>
<main>
  <parent
      attr1="str1"
      attr2="str2">
    <child
        childattr1="str3"
        childattr2="&entity1;"
    />
    <child
        childattr1="str4"
        childattr2="&entity1;"
    />
  </parent>
</main>
$xml\u pp-s缩进\u a foo.xml

很少有XML处理器会维护属性之间的布局,因为其中的空白不重要。您可能需要编写一个自定义格式程序。我想缩进子元素,因为它更易于人类阅读,并且将属性放入新行是版本控制友好的。