XSLT Select node at current level different attribute

135 Views Asked by At

I'm trying to find the most elegant way to create date range based on begin and end dates. I have the following XML:

<mods:originInfo>
    <mods:dateCreated point="start">2006</mods:dateCreated>
    <mods:dateCreated point="end">2007</mods:dateCreated>
    <mods:dateCaptured point="start">2009</mods:dateCaptured>
    <mods:dateCaptured point="end">2010</mods:dateCaptured>
</mods:originInfo>

I have an XSLT that includes

<xsl:template match="mods:originInfo">
    <xsl:for-each select="child::*[@point='start']">
        <dc:date>
            <xsl:value-of select="."/>-<xsl:value-of select="../*[local-name()][@point='end']"/>
        </dc:date>
    </xsl:for-each>
</xsl:template>

When I run this, I get the following output:

<dc:date>2006-2007</dc:date>
<dc:date>2009-2007</dc:date>

I'm trying to find a way to have this code choose the correct "end" node value. Any help is appreciated.

1

There are 1 best solutions below

1
On BEST ANSWER

One single modification: replace ../ with following-sibling:: and it should work:

<xsl:template match="mods:originInfo">
  <xsl:for-each select="child::*[@point='start']">
    <dc:date>
      <xsl:value-of select="."/>-<xsl:value-of select="following-sibling::*[local-name()][@point='end'][1]"/>
    </dc:date>
  </xsl:for-each>
</xsl:template>

Output:

<dc:date xmlns:dc="dc">2006-2007</dc:date>
<dc:date xmlns:dc="dc">2009-2010</dc:date>