user2564684
user2564684

Reputation: 39

XSLT Add incremental id attribute to element

Using XSLT I need an incremental ID number to be added to each feedback element in my output starting from 1. Some segmentpairs may have several feedback elements but the ID number should always be incremented by one.

Desired output:

 <?xml version="1.0" encoding="UTF-8"?>
    <document>
        <feedback id="1">...</feedback>
        <source>...</source>
        <target>...</target>
        <feedback id="2">...</feedback>
        <source>...</source>
        <target>.../target>
    <!-- if mulltiple feedback elements exist in a segment pair -->
        <feedback id="3">...</feedback>
        <feedback id="4">...</feedback>
        <source>...</source>
        <target>.../target>

    </document>

XSLT:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">

    <document>
        <xsl:for-each select="files/file/segmentpair[Comments/Comment]">
            <xsl:apply-templates select="Comments/Comment"/>
            <xsl:copy-of select="source|target"/>

        </xsl:for-each>
    </document>     
</xsl:template>
<xsl:template match="Comment">
    <feedback>
     <xsl:attribute name="id">     
     <xsl:number/> </xsl:attribute>
     <xsl:value-of select="."/>
    </feedback>
</xsl:template> 
</xsl:stylesheet>

XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<files>
  <file originalfilename="C:\Projects\RIA_13049\de-DE\RIA_13049_FPO_AMP-Elitenetzwerk_Master.JULI2013.rtf">
<segmentpair id="46" locked="False" color="255,255,255" match-value="0">
      <source>2. als Vertreter der Universität Bayreuth ein Professor der Materialwissenschaft oder der Engineering Science und</source>
      <target>2. as a representative of the University of Bayreuth, a professor of materials science or engineering science</target>
      <Comments>
        <Comment>[slep10 09.01.2014 15:09:23] why no caps here but in segment above?</Comment>
      </Comments>
    </segmentpair>
    <segmentpair id="47" locked="False" color="255,255,255" match-value="0">
      <source>3. als Vertreter der Universität Würzburg ein Professor der Nanostrukturtechnik.</source>
      <target>3. as a representative of the University of Würzburg, a professor of nanostructure technology</target>
    </segmentpair>
    <segmentpair id="48" locked="False" color="255,255,255" match-value="0">
      <source>(2) Die Mitglieder werden vom Fachbereichsrat der jeweils zuständigen Fakultät auf drei Jahre bestellt; Wiederbestellung ist zulässig.</source>
      <target>(2) The members shall be appointed by the School Council for a term of office of three years; re-appointment shall be permitted.</target>
      <Comments>
        <Comment>[slep10 09.01.2014 15:10:09] problematic term since 'Schools' do not exist at TechFak</Comment>
      </Comments>
    </segmentpair>
    <segmentpair id="49" locked="False" color="255,255,255" match-value="0">
      <source>(3) Die Mitglieder der gemeinsamen Auswahlkommission wählen aus ihrer Mitte den Vorsitzenden und den Stellvertreter.</source>
      <target>(3) The members of the Joint Admissions Committee shall elect a member as chairperson and one as deputy.</target>
    </segmentpair>
    <segmentpair id="50" locked="False" color="255,255,255" match-value="0">
      <source>(4) Die gemeinsame Auswahlkommission entscheidet über die Aufnahme der Bewerber in den Studiengang in Abhängigkeit von der Anzahl der verfügbaren Plätze und gegebenenfalls über ein Ausscheiden.</source>
      <target>(4) The Joint Admissions Committee shall make the decision on the admission of applicants to the degree programme dependent on the available places.  </target>
    </segmentpair>
</file>
</files>

Current output:

  <?xml version="1.0" encoding="UTF-8"?>
    <document>
        <feedback id="1">[slep10 09.01.2014 15:09:23] why no caps here but in segment above?</feedback>
        <source>2. als Vertreter der Universität Bayreuth ein Professor der Materialwissenschaft oder der Engineering Science und</source>
        <target>2. as a representative of the University of Bayreuth, a professor of materials science or engineering science</target>
        <feedback id="1">[slep10 09.01.2014 15:10:09] problematic term since 'Schools' do not exist at TechFak</feedback>
        <source>(2) Die Mitglieder werden vom Fachbereichsrat der jeweils zuständigen Fakultät auf drei Jahre bestellt; Wiederbestellung ist zulässig.</source>
        <target>(2) The members shall be appointed by the School Council for a term of office of three years; re-appointment shall be permitted.</target>
    </document>

Upvotes: 0

Views: 1707

Answers (1)

Rudramuni TP
Rudramuni TP

Reputation: 1278

Try this:

<xsl:template match="Comment">
<feedback>
 <xsl:attribute name="id">     
    <xsl:value-of select="count(preceding::Comments)+1"/>
 </xsl:attribute>
 <xsl:value-of select="."/>
</feedback>

Upvotes: 1

Related Questions