在XML中删除冒号(:)之前的所有字符。

huangapple go评论100阅读模式
英文:

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:

&lt;xsl:stylesheet xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; version=&quot;1.0&quot;&gt;
    &lt;xsl:output indent=&quot;yes&quot;/&gt;
        
  &lt;xsl:template match=&quot;@*|node()&quot;&gt;
    &lt;xsl:copy&gt;
      &lt;xsl:apply-templates select=&quot;@*|node()&quot;/&gt;
    &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;

  &lt;xsl:template match=&quot;*&quot;&gt;
      &lt;xsl:element name=&quot;{local-name()}&quot;&gt;
          &lt;xsl:apply-templates select=&quot;@*|node()&quot;/&gt;
      &lt;/xsl:element&gt;
  &lt;/xsl:template&gt;
    
&lt;/xsl:stylesheet&gt;

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 DecoderEncoder进行处理。根据需要修改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 包含翻译后的文档

在 playground 上运行代码

英文:

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 = &quot;&quot;
			for i, a := range e.Attr {
				if a.Name.Space != &quot;&quot; {
					e.Attr[i].Name = xml.Name{Local: a.Name.Space + &quot;:&quot; + a.Name.Local}
				}
			}
			t = e
		case xml.EndElement:
			e.Name.Space = &quot;&quot;
			t = e
		}
		err = e.EncodeToken(t)
		if err != nil {
			return err
		}
	}
	return nil
}

Example use:

var doc = `&lt;doc  &gt;
 &lt;b:Queues i:nil=&quot;true&quot;/&gt;
 &lt;b:ReceivedFrom&gt;VJ&lt;/b:ReceivedFrom&gt;
 &lt;b:SpecialGDSName i:nil=&quot;true&quot;/&gt;
&lt;/doc&gt;`
var buf bytes.Buffer
if err := rmns(&amp;buf, strings.NewReader(doc)); err != nil {
	log.Fatal(err)
}
// buf contains the translated document

Run the code on the playground.

huangapple
  • 本文由 发表于 2021年12月16日 00:10:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/70366857.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定