我有一个相当大的xml文档,看起来像:
<products>
<product>
<id>1</id>
<other>y</other>
<notarget>x</notarget>
<target>num1</target>
<target>num2</target>
<target>num3</target>
</product>
</products>
但我需要它看起来像:
^{pr2}$我对xml没有太多的经验,所以我甚至不知道应该使用什么技术来检查文件以进行这些更改。可以有多个产品,但此示例只有一个。我使用python,但也可以使用shell。在
需要从一个XML转换到另一个XML是生成XSLT的原因。在
所以,这是一个纯的xslt解决方案,在command-line中使用saxon:
在文件.xml:
xsl文件:
^{pr2}$命令行:
输出:
检查http://saxon.sourceforge.net/saxon6.5/using-xsl.html
对于任何希望在Mac OS上执行此操作的人,您需要安装一个JDK from Oracle,然后您可以通过homebrew从终端安装Saxon:
在终端中,我使用的命令是:
^{pr2}$工作起来很有魅力!在
这听起来确实像是xslt transformation的工作,但这里有一个特定于python的方法。在
每查找一个元素。在循环目标之后,将一个新的
target
项附加到product
中。在使用^{} 实现:
印刷品:
^{pr2}$如您所见,它适用于您提供的输入。在
相关问题 更多 >
编程相关推荐