解析 XML 使用 XSLT 1.0

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

Parse XML with XSLT 1.0

问题

以下是您要的翻译部分:

<?xml version="1.0" encoding="UTF-8"?>
<node> 
    <left-node key="Network0">
        <node key="@FR" value="tom"/>
    </left-node>
    <node key="Network1">
       <node key="PortNumber" left-value="12000" right-value="12001"/>
    </node>
    <node key="Network2">
        <node key="PortNumber" left-value="12000" right-value="12001"/>
        <node key="Location">
            <node key="Office" left-value="IT" right-value="HR"/>
            <right-node key="GO" value="Yellow"/>
        </node>
        <right-node key="Try"/>
    </node>
    <right-node key="Network4">
        <node key="@GB" value="tim"/>
    </right-node>
</node>

您提供的XSLT 1.0代码已经可以成功移除没有带有属性right-value、left-value或value的节点。

英文:

With XSLT 1.0, I would like to remove all nodes tagged node if they have no children with the attribute right-value, left-value or value.

The initial file is :

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;node&gt;
    &lt;left-node key=&quot;Network0&quot;&gt;
        &lt;node key=&quot;@FR&quot; value=&quot;tom&quot;/&gt;
    &lt;/left-node&gt;
    &lt;node key=&quot;Network1&quot;&gt;
       &lt;node key=&quot;PortNumber&quot; left-value=&quot;12000&quot; right-value=&quot;12001&quot;/&gt;
       &lt;node key=&quot;Location&quot;/&gt;
    &lt;/node&gt;
    &lt;node key=&quot;Network2&quot;&gt;
        &lt;node key=&quot;PortNumber&quot; left-value=&quot;12000&quot; right-value=&quot;12001&quot;/&gt;
        &lt;node key=&quot;Location&quot;&gt;
            &lt;node key=&quot;Office&quot; left-value=&quot;IT&quot; right-value=&quot;HR&quot;/&gt;
            &lt;right-node key=&quot;GO&quot; value=&quot;Yellow&quot;/&gt;
        &lt;/node&gt;
        &lt;right-node key=&quot;Try&quot;/&gt;
    &lt;/node&gt;
    &lt;node key=&quot;Network3&quot;&gt;
        &lt;node key=&quot;Location&quot;&gt;
        	 &lt;node key=&quot;Hardware&quot;/&gt;
        	 &lt;node key=&quot;Software&quot;/&gt;
        &lt;/node&gt;
    &lt;/node&gt;
    &lt;right-node key=&quot;Network4&quot;&gt;
        &lt;node key=&quot;@GB&quot; value=&quot;tim&quot;/&gt;
    &lt;/right-node&gt;
&lt;/node&gt;

With the example, I would like to get

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;node&gt; 
    &lt;left-node key=&quot;Network0&quot;&gt;
        &lt;node key=&quot;@FR&quot; value=&quot;tom&quot;/&gt;
    &lt;/left-node&gt;
    &lt;node key=&quot;Network1&quot;&gt;
       &lt;node key=&quot;PortNumber&quot; left-value=&quot;12000&quot; right-value=&quot;12001&quot;/&gt;
    &lt;/node&gt;
    &lt;node key=&quot;Network2&quot;&gt;
        &lt;node key=&quot;PortNumber&quot; left-value=&quot;12000&quot; right-value=&quot;12001&quot;/&gt;
        &lt;node key=&quot;Location&quot;&gt;
            &lt;node key=&quot;Office&quot; left-value=&quot;IT&quot; right-value=&quot;HR&quot;/&gt;
            &lt;right-node key=&quot;GO&quot; value=&quot;Yellow&quot;/&gt;
        &lt;/node&gt;
        &lt;right-node key=&quot;Try&quot;/&gt;
    &lt;/node&gt;
    &lt;right-node key=&quot;Network4&quot;&gt;
        &lt;node key=&quot;@GB&quot; value=&quot;tim&quot;/&gt;
    &lt;/right-node&gt;
&lt;/node&gt;

I am able to get the nodes tagged left-node or right-node but for the nodes tagged node I have problems.

`&lt;xsl:stylesheet version=&quot;1.0&quot; 
xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
&lt;xsl:output method=&quot;xml&quot; version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; indent=&quot;yes&quot;/&gt;
&lt;xsl:strip-space elements=&quot;*&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;node[not(@value!=&#39;&#39; or @right-value!=&#39;&#39; or @left-value!=&#39;&#39;) ]&quot;/&gt;

&lt;/xsl:stylesheet&gt;`

答案1

得分: 0

你正在寻找descendant-or-self轴。因此,请使用以下排除模板:

<xsl:template match="node[not(descendant-or-self::*[@right-value or @left-value or @value])]" />

输出应该符合预期。

英文:

You are looking for the descendant-or-self axis.
So use this excluding template:

&lt;xsl:template match=&quot;node[not(descendant-or-self::*[@right-value or @left-value or @value])]&quot; /&gt;

The output should be as desired.

huangapple
  • 本文由 发表于 2023年5月29日 23:40:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358652.html
匿名

发表评论

匿名网友

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

确定