-2

我想<a></a>从节点值已知的 XML 中删除所有出现的标记。

例如,XML 包含多次出现的<a>123</a>,如果节点值匹配123,则应从 XML 中删除所有节点。

你能帮我处理 XSLT 代码吗?

输入 XML:

<z>
<b>
   <a>123</a>
   <c>
     <a>123</a>
     <d>text</d>
   </c>
   <e>
      <f>xyz></f>
      <a>123</a>
   </e>
</b>
<f>
    <a>345</a>
</f>
<g>
    <a>123</a>
    <h>
      <a>123</a>
      <i></i>
    </h>
</g>
</z>

预期输出:

<z>
<b>

   <c>

     <d>text</d>
   </c>
   <e>
      <f>xyz&gt;</f>

   </e>
</b>
<f>
    <a>345</a>
</f>
<g>

    <h>

      <i/>
    </h>
</g>
</z>

我已将代码用作

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="yes" encoding="UTF-8" omit-xml-declaration="yes" />
   <xsl:template match="node()|@*" >
       <xsl:copy >
           <xsl:apply-templates select="node()|@*" />
       </xsl:copy >
   </xsl:template >
   <xsl:template match="z/b/a|z/b/c/a|z/b/e/a|z/g/a|z/g/h/a" />
</xsl:stylesheet >

但路径的硬编码并不总是有效,因为输入 XML 可能不同。

您能否建议我一些通用的代码来检查节点的值是否为“123”并仅删除这些节点。

先感谢您。

4

2 回答 2

0

我将从身份转换和覆盖开始<a>123</a>

查看这些以获取更多信息:

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="a[text()='123']"/>

</xsl:stylesheet>
于 2013-11-14T07:20:31.557 回答
0
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match = "a[text() = '123']"/>
</xsl:stylesheet>

输出:

<?xml version="1.0" encoding="UTF-8"?>
<z>
    <b>
        <c>
            <d>text</d>
        </c>
        <e>
            <f>xyz&gt;</f>
        </e>
    </b>
    <f>
        <a>345</a>
    </f>
    <g>
        <h>
            <i/>
        </h>
    </g>
</z>

此代码将删除所有以“123”为值的节点。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match = "node()[text() = '123']"/>
</xsl:stylesheet>
于 2013-11-14T07:22:50.360 回答