[xsl] Looping

Subject: [xsl] Looping
From: Nathan Tallman <ntallman@xxxxxxxxx>
Date: Wed, 11 Apr 2012 16:01:15 -0400
Hi XSL list,

I'm a fairly new XSL user and am still figuring things out. Right now
I'm having trouble looping through repeated elements.

Here's my source XML snippet:

<ead>
...
    <archdesc>
    ...
        <did>
        ...
            <langmaterial encodinganalog="546">Collection material in
                <language encodinganalog="041$a"
langcode="eng">English</language>,
                <language encodinganalog="041$a"
langcode="yid">Yiddish</language>, and
                <language encodinganalog="041$a"
langcode="rus">Russian</language>.
            </langmaterial>
			
Here's my XSL snippet:

<xsl:if test="/ead/archdesc/did/langmaterial">
    <xsl:for-each select="/ead/archdesc/did/langmaterial/language[1]">
        <marc:datafield tag="041" ind1=" " ind2=" ">
            <marc:subfield code="a">
                <xsl:value-of
select="normalize-space(/ead/archdesc/did/langmaterial/language[1]/@langcode)"
/>
            </marc:subfield>
        </marc:datafield>
    </xsl:for-each>

    <xsl:for-each select="/ead/archdesc/did/langmaterial/language[2]">
        <marc:datafield tag="041" ind1=" " ind2=" ">
            <marc:subfield code="a">
                <xsl:value-of
select="normalize-space(/ead/archdesc/did/langmaterial/language[2]/@langcode)"
/>
            </marc:subfield>
        </marc:datafield>
    </xsl:for-each>

    <xsl:for-each select="/ead/archdesc/did/langmaterial/language[3]">
        <marc:datafield tag="041" ind1=" " ind2=" ">
            <marc:subfield code="a">
                <xsl:value-of
select="normalize-space(/ead/archdesc/did/langmaterial/language[3]/@langcode)"
/>
            </marc:subfield>
        </marc:datafield>
    </xsl:for-each>			
</xsl:if>

Right now, this achieves what I want. Each langcode is represented by
an individual MARC 041 field. But, as you can see, I have hard coded
each node sequentially. In my XSL, I repeat this ten times, in
anticipation of more langcodes. Is there an easy way to code the
output I want, and then have this loop until it has covered each
instance of <language>?

I've tried this:
			   <xsl:if test="/ead/archdesc/did/langmaterial">
			      <xsl:for-each select="/ead/archdesc/did/langmaterial/language">
			         <marc:datafield tag="041" ind1=" " ind2=" ">
			            <marc:subfield code="a">
			               <xsl:value-of
select="normalize-space(/ead/archdesc/did/langmaterial/language/@langcode)"
/>
			            </marc:subfield>
			         </marc:datafield>
			      </xsl:for-each>
			   </xsl:if>
But it only repeats the first @langcode three times, instead of having
three distinct outputs.

Many thanks!

Current Thread