英文:
Remove all chars before ":" in XML
问题
<Queues i:nil="true"/>
<ReceivedFrom>VJ</b:ReceivedFrom>
<SpecialGDSName i:nil="true"/>
我想要删除所有冒号(包括冒号本身)之前的数据。
我想要得到这样的结果:
<Queues i:nil="true"/>
<ReceivedFrom>VJ</b:ReceivedFrom>
<SpecialGDSName i:nil="true"/>
英文:
<b:Queues i:nil="true"/>
<b:ReceivedFrom>VJ</b:ReceivedFrom>
<b:SpecialGDSName i:nil="true"/>
I want to remove all data before ":" including ":"
I want to make like this:
<Queues i:nil="true"/>
<ReceivedFrom>VJ</b:ReceivedFrom>
<SpecialGDSName i:nil="true"/>
</details>
# 答案1
**得分**: 2
将XML通过XSLT运行,如下所示,这将删除与这些元素相关联的命名空间:
```xml
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
您可以使用更专门的模板,仅匹配那些b:*元素,并使用一个模板从属性中删除命名空间。
英文:
Run the XML through an XSLT like this, which will drop the namespace associated with those elements:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
You could use more specialized templates matching only those b:* elements, and do the same thing with a template to strip the namespace from attributes as well.
答案2
得分: 1
将文档通过xml Decoder和Encoder进行处理。根据需要修改tokens上的命名空间:
func rmns(w io.Writer, r io.Reader) error {
e := xml.NewEncoder(w)
defer e.Flush()
d := xml.NewDecoder(r)
for {
t, err := d.RawToken()
if err == io.EOF {
return nil
} else if err != nil {
return err
}
switch e := t.(type) {
case xml.StartElement:
e.Name.Space = ""
for i, a := range e.Attr {
if a.Name.Space != "" {
e.Attr[i].Name = xml.Name{Local: a.Name.Space + ":" + a.Name.Local}
}
}
t = e
case xml.EndElement:
e.Name.Space = ""
t = e
}
err = e.EncodeToken(t)
if err != nil {
return err
}
}
return nil
}
示例用法:
var doc = `<doc>
<b:Queues i:nil="true"/>
<b:ReceivedFrom>VJ</b:ReceivedFrom>
<b:SpecialGDSName i:nil="true"/>
</doc>`
var buf bytes.Buffer
if err := rmns(&buf, strings.NewReader(doc)); err != nil {
log.Fatal(err)
}
// buf 包含翻译后的文档
英文:
Run the document through the xml Decoder and Encoder. Modify the namespace on tokens as needed:
func rmns(w io.Writer, r io.Reader) error {
e := xml.NewEncoder(w)
defer e.Flush()
d := xml.NewDecoder(r)
for {
t, err := d.RawToken()
if err == io.EOF {
return nil
} else if err != nil {
return err
}
switch e := t.(type) {
case xml.StartElement:
e.Name.Space = ""
for i, a := range e.Attr {
if a.Name.Space != "" {
e.Attr[i].Name = xml.Name{Local: a.Name.Space + ":" + a.Name.Local}
}
}
t = e
case xml.EndElement:
e.Name.Space = ""
t = e
}
err = e.EncodeToken(t)
if err != nil {
return err
}
}
return nil
}
Example use:
var doc = `<doc >
<b:Queues i:nil="true"/>
<b:ReceivedFrom>VJ</b:ReceivedFrom>
<b:SpecialGDSName i:nil="true"/>
</doc>`
var buf bytes.Buffer
if err := rmns(&buf, strings.NewReader(doc)); err != nil {
log.Fatal(err)
}
// buf contains the translated document
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论