2

我想学习如何在 xml 文件中搜索单词并使用 xslt 删除整行

示例:abc.xml

<server>
  <mbean code="org.jboss.varia.property.SystemPropertiesService" 
     name="abc.props:type=Service,name=abcprop">
  <attribute name="Properties">
         abc.def.ghi=123
         ghi.klm.nop=123
         qrst.tuv.wxy=123
         zab.cde.fgh=123
         ijk.lmn.opq=remove
         rst.uvw.xyz=123
         abc.tuv.nop=123
         ajc.dzf.goi=123
   </attribute>
 </mbean>
</server>

从上面的示例中,我想搜索一个单词“ remove ”并删除整行:ijk.lmn.opq=remove

预期输出为:

<server>
      <mbean code="org.jboss.varia.property.SystemPropertiesService" 
         name="abc.props:type=Service,name=abcprop">
      <attribute name="Properties">
             abc.def.ghi=123
             ghi.klm.nop=123
             qrst.tuv.wxy=123
             zab.cde.fgh=123
             rst.uvw.xyz=123
             abc.tuv.nop=123
             ajc.dzf.goi=123
       </attribute>
     </mbean>
    </server>

更新:

我尝试了以下代码

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*[(@* != 'DELETE')]"/>
</xsl:stylesheet>

有些它不工作,它删除了 xml 文件中的所有内容并显示一个空文件。

4

3 回答 3

4

如果你被 XSLT 1.0 卡住了,你可以使用这个:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <!--String replace template-->
 <xsl:template name="removeline">
    <xsl:param name="string" />
    <xsl:choose>
    <xsl:when test="contains($string,'remove')">
        <xsl:variable name="before">
        <xsl:value-of select="substring-before($string,'remove')"/>
        </xsl:variable>
        <xsl:variable name="after">
        <xsl:value-of select="substring-after($string,'remove')"/>
        </xsl:variable>
        <xsl:value-of select="substring($before,1, string-length($before) - 12)"/>
        <xsl:call-template name="removeline">
            <xsl:with-param name="string" select="$after" />
        </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="$string" />
    </xsl:otherwise>
    </xsl:choose>
 </xsl:template>

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

  <!-- copy text nodes, and apply removeline template  -->
  <xsl:template match="text()" >

     <!-- create empty string for match -->
     <xsl:variable name="empty_string"/>

     <xsl:if test="normalize-space(.) != $empty_string">
            <xsl:call-template name="removeline">
        <xsl:with-param name="string" select="normalize-space(.)"/>
        </xsl:call-template>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

您可以在此处看到三个模板。第一个是 XSLT 1.0 的字符串函数替换,它没有。它基于在网络的几个地方发现的一个。您可以在https://stackoverflow.com/a/7523245/2657945中找到更通用的实现。

第二个和第三个模板在获取文档时输出文档中的所有节点和属性,形成文本节点的特殊情况,使用第一个模板处理。这部分基于https://stackoverflow.com/a/427983/2657945中的内容。

输出是这样的(在linux中使用xsltproc):

<server>
  <mbean code="org.jboss.varia.property.SystemPropertiesService" name="abc.props:type=Service,name=abcprop">
    <attribute name="Properties">abc.def.ghi=123 ghi.klm.nop=123 qrst.tuv.wxy=123 zab.cde.fgh=123  rst.uvw.xyz=123 abc.tuv.nop=123 ajc.dzf.goi=123</attribute>
  </mbean>
</server>

这是我能够用您的 xml 示例生成的最近似结果,因此根据您的文件和格式,您可能需要对其进行更多调整。

于 2013-08-06T18:32:58.250 回答
2

使用 XSLT 2.0,您可以replace在要操作的文本节点上使用该函数:

<xsl:stylesheet version="2.0" exclude-result-prefixes="xs"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xsl:output method="xml" indent="yes"/>

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

    <xsl:template match="attribute[@name = 'Properties']/text()">
      <xsl:value-of select="replace(., '^.*remove.*$\n*', '', 'm')"/>
    </xsl:template>

</xsl:stylesheet>
于 2013-08-06T17:11:03.680 回答
0

XSLT 1.0 只有非常有限的字符串处理功能,所以没有简单的方法可以用纯 XSLT 完成您所要求的事情。

最好的方法是使用一些用不同语言编写的扩展函数——大多数 XSLT 处理器可以定义和调用这样的扩展函数:Xalan-JavaSaxon.NET

作为替代方案,考虑根本不使用 XSLT - 考虑到您的问题基本上是字符串匹配而不是与 XML 相关:只需加载 XML 并使用您最喜欢的编程语言处理字符串。

于 2013-08-06T14:34:44.300 回答