Xslt根据子元素的值删除父元素

时间:2012-03-16 15:53:01

标签: xslt-1.0

架构如下:

<root>
<publication>
<otherthanurl>blabla</otherthanurl>
</publication>
<publication>
<url>http://media.blabla.pdf</url>
</publication> 
<publication>
<url>http://media2.blabla.pdf</url>
</publication> 
<publication>
<url>http://otherblabla</url>
</publication> 
</root>

我想复制除了包含url元素的发布元素之外的整个内容,其中url元素的值以http://mediahttp://media3开头,以.pdf结尾

有什么想法吗?

我的初始代码:

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

  <xsl:template match="*[starts-with(name(), 'url')]"/>

Thansk很多,我迷路了

1 个答案:

答案 0 :(得分:3)

一般的想法是有效的,只有空模板不正确。以下作品:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="ISO-8859-1"/>

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

<xsl:template match="publication[descendant::url[(starts-with(.,'http://media.') 
or starts-with(.,'http://media3.')) and ends-with (., '.pdf')]]"/>

</xsl:stylesheet>

简单来说:复制除了具有后代的发布之外的所有内容,后者是一个url元素,其值(。)以指定的字符串开头/结尾。这是xslt 1.0。