使用 sed 处理 XML 通常不是一个好主意,因为 sed 以基于行的方式工作,而 XML 并不真正关心换行符。例如,您可以拥有
<foo bar=
"baz"/>
在完全有效的 XML 中,这将很难用 sed(或其他纯文本工具)处理。
我建议使用 XSLT 样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:variable name="vLower" select="'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:variable name="vUpper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*">
<xsl:variable name="capname"
select="concat(translate(substring(name(.),1,1), $vLower, $vUpper), substring(name(.), 2))"/>
<xsl:attribute name="{$capname}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
把它放在一个文件中,比如foo.xsl,然后使用一个XSLT处理器比如xsltproc来运行它:
xsltproc foo.xsl foo.xml
foo.xml 是您的 XML 文件。或者,使用xalan:
xalan -xsl foo.xsl -in foo.xml
任何 XSLT 处理器都可以;对于其他人,请查看他们的联机帮助页。
它的工作原理如下:
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
是适用于所有节点的identity transform(在这种情况下不是属性,因为它们在其他地方处理)并递归地应用模板。这使得转换的输出是输入的副本,没有其他模板适用。肉在
<xsl:variable name="vLower" select="'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:variable name="vUpper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:template match="@*">
<xsl:variable name="capname"
select="concat(translate(substring(name(.),1,1), $vLower, $vUpper), substring(name(.), 2))"/>
<xsl:attribute name="{$capname}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
这适用于所有属性 (@*)。
concat(translate(substring(name(.),1,1), $vLower, $vUpper), substring(name(.), 2))
是属性名称的大写版本,它被分配给变量capname。那么
<xsl:attribute name="{$capname}">
<xsl:value-of select="."/>
</xsl:attribute>
插入具有大写名称和旧值的新属性来代替旧的非大写属性。
这适用于所有有效的 XML 输入。