user3668920
user3668920

Reputation: 11

xslt first 10 characters of an element

I'm trying to create a xslt code to reduce the text of an element (Fetchdate) to its first 10 characters. I believe that the correct function is the "substring", but unfortunately I can not make it work!

My origin xml is:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<Staff>
<File>staff.php</File>
<Fetchdate>2014-06-13 10:10:11</Fetchdate>
<IdTeam>614</IdTeam>
<StaffMechanicians>20</StaffMechanicians>
<StaffTechnics>6</StaffTechnics>
<StaffPR>12</StaffPR>
<StaffObservers>2</StaffObservers>
<StaffFitnessCoaches>4</StaffFitnessCoaches>
<StaffRepairman>10</StaffRepairman>
<StaffTeamSpirit>20</StaffTeamSpirit>
</Staff>

Expected xml output:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<Staff>
<File>staff.php</File>
<Fetchdate>2014-06-13</Fetchdate>
<IdTeam>614</IdTeam>
<StaffMechanicians>20</StaffMechanicians>
<StaffTechnics>6</StaffTechnics>
<StaffPR>12</StaffPR>
<StaffObservers>2</StaffObservers>
<StaffFitnessCoaches>4</StaffFitnessCoaches>
<StaffRepairman>10</StaffRepairman>
<StaffTeamSpirit>20</StaffTeamSpirit>
</Staff>

Who can help me to generate the xslt code? thanks.

Upvotes: 0

Views: 2020

Answers (1)

ThW
ThW

Reputation: 19512

The expression should be:

substring(/Staff/Fetchdate, 1, 10)

You have to keep in mind, that indizes in Xpath start with 1 not 0.

Putting it into a template match:

<xsl:template match="Staff/Fetchdate">
  <xsl:value-of select="substring(., 1, 10)"/>
</xsl:template>

Another possibility in this case would be substring-before()

<xsl:template match="Staff/Fetchdate">
  <xsl:value-of select="substring-before(., ' ')"/>
</xsl:template>

Upvotes: 4

Related Questions