Domanda

I have the following xml / xsl:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="./test.xsl"?>

<a>
  <b>
    <c >
      <no>1</no>
      <d>
        <e>
          <f>20060603190000</f>
        </e>
      </d>
    </c>
    <c >
      <no>1</no>
      <d>
        <e>
          <f>20060603190000</f>
        </e>
      </d>
    </c>
    <c>
      <no>2</no>
      <d>
        <e>
          <f>20060603190000</f>
        </e>
      </d>
    </c>
    <c >
      <no>1</no>
      <d>
        <e>
          <f>20060819200000</f>
        </e>
        <e>
          <f>20060902200000</f>
        </e>
      </d>
    </c>
    <c >
      <no>1</no>
      <d>
        <e>
          <f>20070819200000</f>
        </e>
        <e>
          <f>20070819200003</f>
        </e>
        <e>
          <f>20070819200001</f>
        </e>
        <e>
          <f>20060903100000</f>
        </e>
      </d>
    </c>
    <c >
      <no>1</no>
      <d>
        <e>
          <f>20060819200000</f>
        </e>
        <e>
          <f>20060902200000</f>
        </e>
      </d>
    </c>
  </b>
</a>


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

  <xsl:template match="/">

  <table>
    <tr>
    <xsl:for-each select="a/b/c[no=1]/d/e/f[not(.=preceding::*)]" ><xsl:sort select="." data-type="number"/>&#160;
      <td><xsl:value-of select="."/></td>
    </xsl:for-each>
    </tr>
  </table>

  </xsl:template>

</xsl:stylesheet>

when applied, the result looks like this:

20060603190000 20060819200000 20060902200000 20060903100000 20070819200000 20070819200001 20070819200003

which is partly correct, since:

  • i want to display distinct values of f (which works)

  • i want to sort the values of f (which works too)

but:

i want to display values that are distinct for the first 8 digits only (year, month, day), so that the result would be:

20060603190000 20060819200000 20060902200000 20060903100000 20070819200000

ie values with the same date but different time are seen as equal.

I have tried to use substring(values, 8) in different places, but i can't get it to work that way nor using key/generate-id. Can anyone please give me advise on that?

edited:

i have solved the problem on my own.

the following template create a string containing all dates sorted by time and truncated to the first 8 digits (the date withaout the time):

<xsl:template match="/">
  <xsl:variable name="text">
    <xsl:for-each select="a/b/c[no=1]/d/e/f[not(.=preceding::*)]" ><xsl:sort select="." data-type="number"/>
      <xsl:value-of select="substring(.,0,9)"/>-</xsl:for-each>
  </xsl:variable>

  text:<xsl:value-of select="$text"/>
  <xsl:call-template name="filter"><xsl:with-param name="text"><xsl:value-of select="$text"/></xsl:with-param><xsl:with-param name="previousElement"/></xsl:call-template >
</xsl:template>

output example: 20060603-20060819-20060902-20060902-20060902-20060903-20070819-20070819-20070819-20110902-20110902-

then a second template is called which outputs the distinct values by recursivly calling itself:

<xsl:template name="filter">
  <xsl:param name="text"/>
  <xsl:param name="previousElement"/>

  <xsl:variable name="firstElement"><xsl:value-of select="substring-before($text, '-')"/></xsl:variable>
  <xsl:variable name="rest"><xsl:value-of select="substring-after($text, '-')"/></xsl:variable>

<!--    firstElement:<xsl:value-of select="$firstElement"/>
  previousElement:<xsl:value-of select="$previousElement"/>
  rest:<xsl:value-of select="$rest"/>
-->
  <xsl:if test="string-length($firstElement) > 0">
    <xsl:choose>
      <xsl:when test="$firstElement = $previousElement">
        <xsl:call-template name="filter"><xsl:with-param name="text"><xsl:value-of select="$rest"/></xsl:with-param><xsl:with-param name="previousElement"><xsl:value-of select="$firstElement"/></xsl:with-param></xsl:call-template >
      </xsl:when>
      <xsl:otherwise>
        output:<xsl:value-of select="$firstElement"/>
        <xsl:call-template name="filter"><xsl:with-param name="text"><xsl:value-of select="$rest"/></xsl:with-param><xsl:with-param name="previousElement"><xsl:value-of select="$firstElement"/></xsl:with-param></xsl:call-template >
      </xsl:otherwise>
    </xsl:choose>
  </xsl:if>
</xsl:template>
È stato utile?

Soluzione

i answered my question myself as seen above. according to the advice of Marcus Rickert i will answer it this way and accept it to make it closable and maybe helpful for others.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top