roland
roland

Reputation: 7775

Xslt Removing parent element based on child element's value

The schema is as following:

<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>

I'd like to copy the whole content except publication elements that contain url element whose value starts with http://media or http://media3 AND ending with .pdf

Any idea?

My initial code:

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

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

Thansk a lot, I'm lost

Upvotes: 0

Views: 2790

Answers (1)

The general idea is valid, only the empty template is not correct. The following works:

<?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>

In plain words: copy everything except a publication that has a descendant that's an url element whose value (.) starts / ends with the specified strings. This is xslt 1.0.

Upvotes: 3

Related Questions