我怎样才能在xml文件中取消行

时间:2010-11-17 20:16:18

标签: xml xslt

考虑以下XML文件内容:

<catalog>
  <cd>
        <title>Red</title>
        <artist>The Communards</artist>
  </cd>
  <cd>
        <title>Unchain my heart</title>
        <artist>Joe Cocker</artist>
  </cd>
</catalog>

如何使用任何现有工具或XSLT获得以下内容?

<catalog>
  <cd><title>Red</title><artist>The Communards</artist></cd>
  <cd><title>Unchain my heart</title><artist>Joe Cocker</artist></cd>
</catalog>

我想进行此转换,因为我想从xml文件中快速删除一些记录(在本例中为“cd”)。使用单行格式对我有帮助。

谢谢!

5 个答案:

答案 0 :(得分:4)

  

我想做这个转变   因为我想删除一些记录   (在这种情况下,'cd')很快从一个   xml文件。使用单行格式   会帮助我。

对不起,这是一种错误的做法。您希望使用XSLT来操作文档中的空格,以便更容易使用不是XSLT的东西删除错误的行?只需首先删除XSLT不需要的行!

基本示例(未经测试但99%确定这适用于给定的要求):

<xsl:stylesheet version="1.0">

  <!-- this is called the identity transform - it will copy the input wholesale -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <!-- this template will provide the exception case for "cd" nodes and effectively remove them -->
  <xsl:template match="cd">
    <!-- do nothing with it! -->
  </xsl:template>

</xsl:stylesheet>

答案 1 :(得分:2)

奇怪的requeriment(不是语义差异)......这个样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="no" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="@*|node()" name="identity">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="cd">
        <xsl:text>&#xA;&#x9;</xsl:text>
        <xsl:call-template name="identity"/>
        <xsl:value-of select="substring('&#xA;', 1 div (position()=last()))"/>
    </xsl:template>
</xsl:stylesheet>

输出:

<catalog>
    <cd><title>Red</title><artist>The Communards</artist></cd>
    <cd><title>Unchain my heart</title><artist>Joe Cocker</artist></cd>
</catalog>

注意:缩进规则,xsl:strip-space(修剪所有文本节点),为cd添加新行和标签,为最后cd添加新行。< / p>

答案 2 :(得分:1)

测试:

这是一个解决方案:

<xsl:stylesheet 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output indent="no" method="xml"/>
<xsl:strip-space elements="cd title artist"/>

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

</xsl:stylesheet>

关键是确保输出不会自动缩进,然后指定哪些元素应删除空格,例如cdtitleartist

答案 3 :(得分:0)

如果你想格式化任意大的流(xslt使用内存模型),你可以使用stax事件阅读器读取它,然后再将它们写出来丢弃空格事件并在遇到一个换行事件时插入换行事件目录或cd结束元素事件。您甚至可以删除此步骤,并在写出结果时忽略您不感兴趣的CD事件。

答案 4 :(得分:0)

  

我想做这个转变   因为我想删除一些记录   (在这种情况下,'cd')很快从一个   xml文件。使用单行格式   会帮助我。

首先使用XSLT从XML中删除想要的“记录”会不会更容易?这比使用XSLT修改中断/空白区域然后使用其他内容删除您不想要的内容更容易。 (您打算使用什么?是否要删除以<cd>开头的所有行?)

如果使用身份转换,在XSLT中删除不需要的数据很简单。您可以为不想保留的任何内容添加空模板。

<?xml version="1.0" encoding="UTF-8"?>
<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="cd"/>

</xsl:stylesheet>

使用XML输入,此样式表将生成以下输出:

<?xml version="1.0" encoding="UTF-8"?>
<catalog/>