我不知道是否可以使用正则表达式,但我想替换此xml中的某些内容:
<custom-attribute name="Attribute_1" dt:dt="string">Danny Boyle</custom-attribute>
<custom-attribute name="DVD-releasedatum" dt:dt="string">06/10/1999</custom-attribute>应该变成
<Attribute_1>Danny Boyle</Attribute_1>
<DVD-releasedatum>06/10/1999</DVD-releasedatum>从第一个标记中删除它并不难,但如何关闭新形成的标记?
发布于 2010-03-25 17:08:12
例如,使用gvim,可以做到这一点:
:%s/.*name="\([^"]*\)"[^>]*>\([^<]*\)<.*/<\1>\2<\/\1>/cg这是匹配的部分:
.*name="\([^"]*\)"[^>]*>\([^<]*\)<.*这是替换部分:
<\1>\2<\/\1>发布于 2010-03-25 17:23:20
这看起来像是XSLT的工作:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="custom-attribute">
<xsl:element name="{@name}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>这将为您提供所需的输出,并且对于将来的修改和扩展非常灵活。
发布于 2010-03-25 17:05:29
如果您想这样做一次,Regex替换可以是一个选项。除此之外,还有更好的方法来转换XML,例如XSLT。
对于使用正则表达式,您可以替换
\<custom-attribute.*?name="(\w+)".*?\>(.*?)\</custom-attribute\>使用
<$1>$2</$1>将$1和$2替换为程序中调用的任何引用。先保存备份,不过=)
https://stackoverflow.com/questions/2514115
复制相似问题