user1549583
user1549583

Reputation: 3

XSLT selects only one object

I am trying to select from an XML document a collection of Locations for a Business. This code works except that is only selects one item. So, my question: is there something that I can do to this code to make it work for each Location?

    <xsl:template match="/InstitutionAlias/InstitutionAliasExternalReferenceCollection">
  <InstitutionExternalReferenceCollection>    
    <xsl:for-each select="InstitutionAliasExternalReference">
      <InstitutionExternalReference>
        <ExternalReferenceIdentifier>
          <xsl:value-of select="ExternalReferenceIdentifier"/>
        </ExternalReferenceIdentifier>
      </InstitutionExternalReference>
    </xsl:for-each>
  </InstitutionExternalReferenceCollection>
</xsl:template>

This is what the output should look like

  <InstitutionExternalReferenceCollection>
- <InstitutionExternalReference>
  <ExternalReferenceIdentifier>Test1</ExternalReferenceIdentifier>
</InstitutionExternalReference>

- <InstitutionExternalReference>
  <ExternalReferenceIdentifier>Test2</ExternalReferenceIdentifier>
</InstitutionExternalReference>

Here is a sample xml file.

  - <InstitutionAliasExternalReferenceCollection>
- <InstitutionAliasExternalReference>
  <ExternalReferenceIdentifier>Test1</ExternalReferenceIdentifier>
</InstitutionAliasExternalReference>
- <InstitutionAliasExternalReference>
  <ExternalReferenceIdentifier>Test2</ExternalReferenceIdentifier>
</InstitutionAliasExternalReference>

Upvotes: 0

Views: 157

Answers (1)

Mitya
Mitya

Reputation: 34566

Something like this? It could be condensed if you were sure each business could have only one location identifier (I don't know) but I decided to keep things separated into separate templates.

You can run it at this XMLPlayground (see output source).

    <!-- kick things off -->
    <xsl:template match="BusinessCollection">
        <BusinessCollection>
            <xsl:apply-templates select='Business' />
        </BusinessCollection>
    </xsl:template>

    <!-- each business -->
    <xsl:template match='Business'>
        <Business>
            <xsl:apply-templates select='LocationIdentifier' />
        </Business>
    </xsl:template>

    <!-- each location identifier -->
    <xsl:template match='LocationIdentifier'>
        <LocationIdentifier>
            <xsl:value-of select='.' />
        </LocationIdentifier>
    </xsl:template>

</xsl:stylesheet>

Your expected output mentioned Data nodes, but these didn't appear in your XSL attempt so I'm not sure what the intention was there.

Upvotes: 1

Related Questions