xsl-list
[Top] [All Lists]

Re: [xsl] Generating attributes from the position of an element

2010-06-01 02:33:13
I would have done this as following,

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

<xsl:template match="@ID">
   <xsl:attribute name="Id">
      <xsl:value-of select="count(../preceding-sibling::w) + 1" />
   </xsl:attribute>
</xsl:template>

PS: this is not tested.

On Tue, Jun 1, 2010 at 12:22 PM, Gábor Tóth <roysy(_at_)g0n0z(_dot_)hu> wrote:
Dear All,

I have the following xml file and wish to generate ID-s based on the
position to the <w> elements with the following XSL file, however, the
end result of the ID attribute is always 1, I have no idea why.

The xml file is

<?oxygen 
RNGSchema="http://www.tei-c.org/release/xml/tei/custom/schema/relaxng/tei_all.rng";
type="xml"?>
<?xml-stylesheet type="text/xsl" href="template.xsl"?>
<text>
   <w ID=" " >car</w>
   <w ID=" ">dog</w>
   <w ID=" ">cat</w>
   <w ID=" ">house</w>

</text>

The XSL file is

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
   xmlns:tei="http://www.tei-c.org/ns/1.0"; xmlns="http://www.tei-c.org/ns/1.0";
   version="2.0" exclude-result-prefixes="tei"
   xpath-default-namespace="http://www.tei-c.org/ns/1.0";
   >


   <xsl:template match="@*|node()|comment()|processing-instruction()"
priority="-5">
       <xsl:copy><xsl:apply-templates
select="@*|node()|comment()|processing-instruction()"/></xsl:copy>
       <xsl:apply-templates select="/text/w/@ID"/>

   </xsl:template>

<xsl:template match="@ID">
        <xsl:attribute name="Id">
           <countNo><xsl:value-of select="position()"/></countNo>

       </xsl:attribute>

   </xsl:template>

Result:

<?xml version="1.0" encoding="UTF-8"?><?oxygen
RNGSchema="http://www.tei-c.org/release/xml/tei/custom/schema/relaxng/tei_all.rng";
type="xml"?><?xml-stylesheet type="text/xsl"
href="template.xsl"?><text>
   <w Id="1">car</w>
   <w Id="1">dog</w>
   <!-- Why does not increment the number of the ID?  -->
<w Id="1">cat</w>
   <w Id="1">house</w>

</text>

Thanks,

Gabor



-- 
Regards,
Mukul Gandhi

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--