我需要使用XSLT(遗憾的是版本1)我试图通过比较子节点中的两个属性来过滤掉某些节点。
这是XML:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
</SOAP-ENV:Header>
<SOAP-ENV:Body xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<QueriesResponse xmlns="http://schemas.Movies.com/Movies">
<Films xmlns="http://schemas.Movies.com/Movies">
<Film>
<FilmPostings>
<FilmPosting>
<FilmPostingDates>
<FilmPostDate>2017-01-04T19:44:25.9530000-05:00</FilmPostDate>
<FilmActiveDate>2017-01-04T19:44:25.9530000-05:00</FilmActiveDate>
</FilmPostingDates>
</FilmPosting>
</FilmPostings>
</Film>
<Film>
<FilmPostings>
<FilmPosting>
<FilmPostingDates>
<FilmPostDate>2017-01-04T19:50:06.3830000-05:00</FilmPostDate>
<FilmActiveDate>2017-01-04T19:50:06.3100000-05:00</FilmActiveDate>
</FilmPostingDates>
</FilmPosting>
</FilmPostings>
</Film>
<Film>
<FilmPostings>
<FilmPosting>
<FilmPostingDates>
<FilmPostDate>2016-12-05T18:03:14.9830000-05:00</FilmPostDate>
<FilmActiveDate>2017-01-02T00:16:52.7570000-05:00</FilmActiveDate>
</FilmPostingDates>
</FilmPosting>
</FilmPostings>
</Film>
</Films>
</QueriesResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
这是我的转变:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://schemas.Movies.com/Movies"
xmlns:m="http://schemas.Movies.com/Movies"
exclude-result-prefixes="m"
>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<!-- standard identity template -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- This is mean to compare PostDate and ActiveDate, matching if different. It doesn't match any nodes in XML (but should match the final one). -->
<xsl:template match="m:Film[m:FilmPostings/m:FilmPosting/m:FilmPostingDates/m:FilmPostDate[1] [.!= m:FilmPostings/m:FilmPosting/m:FilmPostingDates/m:FilmActiveDate[1]]]">
</xsl:template>
<!-- This code does match the hard coded value (the second node). -->
<!--<xsl:template match="m:Film[m:FilmPostings/m:FilmPosting/m:FilmPostingDates/m:FilmPostDate[1] [.!= '2017-01-04T19:50:06.3830000-05:00']]">
</xsl:template>-->
</xsl:stylesheet>
所以,你会在注释掉的位中看到我可以与硬编码值匹配,所以我显然在正确的区域 - 我知道代码将排除整个电影节点,如果它找到匹配。但它正在比较两个不起作用的节点值。
我在比较的右侧尝试了各种变体,但它似乎无法获取ActiveDate的值。
答案 0 :(得分:1)
要移除Film
和FilmPostDate
FilmActiveDate
元素,您可以在匹配属性中实际嵌套条件
<xsl:template
match="m:Film[m:FilmPostings/m:FilmPosting/m:FilmPostingDates[m:FilmPostDate = m:FilmActiveDate]]" />
这假定每FilmPostDate
只有一组FilmActiveDate
和Film
个元素。如果有多个集合,您可以尝试删除所有出现次数相同的Film
元素(或者更确切地说,不存在不同的出现次数)。
<xsl:template
match="m:Film[not(m:FilmPostings/m:FilmPosting/m:FilmPostingDates[m:FilmPostDate != m:FilmActiveDate])]" />