1

I want to keep element in XML and remove others, in XSLT 1.0, based on an attribute value that mus match a parent attribute

I want to keep only DONNEES Elements where attribute Journee matches the parent attribute Date. It can be any date I cannot put something like ='2015-09-17T06:00:00'.

Here is the XML example

<?xml version="1.0"?>
<Root>
    <JOURNEE Date="2015-09-17T06:00:00">
        <ID>
            <DONNEES Journee="2015-09-17T06:00:00"/>
            <DONNEES Journee="2015-09-18T06:00:00"/>
            <DONNEES Journee="2015-09-19T06:00:00"/>
        </ID>
    </JOURNEE>
    <JOURNEE Date="2015-09-18T06:00:00">
        <ID>
            <DONNEES Journee="2015-09-17T06:00:00"/>
            <DONNEES Journee="2015-09-18T06:00:00"/>
            <DONNEES Journee="2015-09-19T06:00:00"/>
        </ID>
    </JOURNEE>
    <JOURNEE Date="2015-09-19T06:00:00">
        <ID>
            <DONNEES Journee="2015-09-17T06:00:00"/>
            <DONNEES Journee="2015-09-18T06:00:00"/>
            <DONNEES Journee="2015-09-19T06:00:00"/>
        </ID>
    </JOURNEE>
</Root>

Here is the output I want

<Root>
<JOURNEE Date="2015-09-17T06:00:00">
<ID>
<DONNEES Journee="2015-09-17T06:00:00"/>
</ID>
</JOURNEE>
<JOURNEE Date="2015-09-18T06:00:00">
<ID>
<DONNEES Journee="2015-09-18T06:00:00"/>
</ID>
</JOURNEE>
<JOURNEE Date="2015-09-19T06:00:00">
<ID>
<DONNEES Journee="2015-09-19T06:00:00"/>
</ID>
</JOURNEE>
</Root>

Here is the XSLT I have for now that doesn't work it delete all datas

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

<xsl:template match="/*/*/*DONNEES[(@Journee != /*/JOURNEE/@Date)]" />

I tried this and it works but I cannot have data like this

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

<xsl:template match="/*/*/*DONNEES[(@Journee != '2015-09-17T06:00:00')]" />

Thank you :)

CRT
  • 33
  • 3

1 Answers1

1

You should be using a relative path in your expression to get the ancestor date

Try this XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes" />
    <xsl:strip-space elements="*" />

    <xsl:template match="DONNEES[@Journee != ../../@Date]" />

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
Tim C
  • 70,053
  • 14
  • 74
  • 93