简体   繁体   中英

XSLT for-each of a node

I have the following XML structure residing within a namespace :

<School>
  <SchoolInfo>
    <SchoolName>The Big School</SchoolName>
    <Opened>2008</Opened>
    <SchoolID>SCH1122</SchoolID>
    <Geograpics>
      <Location>London</Location>
      <PostCode>ZZ11 1ZZ</PostCode>
    </Geographics>
  </SchoolInfo>
  <Pupil>
    <Name>Tom</Name>
    <LastName>Jones</LastName>
    <Class>12B</Class>
    <Age>16</Age>
  </Pupil>
  <Pupil>
    <Name>Steve</Name>
    <LastName>Jobs</LastName>
    <Class>09A</Class>
    <Age>17</Age>
  </Pupil>
  <Pupil>
    <Name>Joe</Name>
    <LastName>Blogs</LastName>
    <Class>13A</Class>
    <Age>15</Age>
  </Pupil>
</School>

Using XSLTl I wish to create a PSV which looks like the following structure: (SchoolID|Location|Name|Class|Age)

SCH1122|London|Tom|12B|16
SCH1122|London|Steve|09A|17
SCH1122|London|Joe|13A|15

Is this possible using XSLT and how would I go about this?

Code so far:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="NamespaceHere"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:cs="urn:cs"
exclude-result-prefixes="cs x msxsl" >

<xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
<xsl:strip-space elements="*"/>

<xsl:template match="x:SchoolID">
    <xsl:value-of select="text()"/>
    <xsl:text>|</xsl:text>
</xsl:template>

<xsl:template match="x:PostCode">
    <xsl:value-of select="text()"/>
    <xsl:text>|</xsl:text>
</xsl:template>

<xsl:template match="x:Name">
    <xsl:value-of select="text()"/>
    <xsl:text>|</xsl:text>
</xsl:template>

<xsl:template match="x:Class">
    <xsl:value-of select="text()"/>
    <xsl:text>|</xsl:text>
</xsl:template>

<xsl:template match="x:Age">
    <xsl:value-of select="text()"/>
    <xsl:text>|</xsl:text>
</xsl:template>

<xsl:template match="text()">
    <xsl:apply-templates select="x:Message"/>
</xsl:template>

<xsl:template match="x:School">
    <xsl:apply-templates select="x:SchoolInfo/x:SchoolID"/>
    <xsl:apply-templates select="x:SchoolInfo/x:Geographics/x:PostCode"/>
    <xsl:apply-templates select="x:Pupil/x:Name"/>
    <xsl:apply-templates select="x:Pupil/x:Class"/>
    <xsl:apply-templates select="x:Pupil/x:Age"/>
    <xsl:text>&#0010;</xsl:text>
</xsl:template>
</xsl:stylesheet>

This works when I have only one pupil however when I get multiple pupils the output becomes like the following:

SCH1122|London|London|Tom|Steve|12B|09A|16|17

How about changing your match="x:School" to:

<xsl:template match="x:School">
  <xsl:variable name="parent" select="."/>
  <xsl:for-each select="x:Pupil">
    <xsl:apply-templates select="$parent/x:SchoolInfo/x:SchoolID"/>
    <xsl:apply-templates select="$parent/x:SchoolInfo/x:Geographics/x:PostCode"/>
    <xsl:apply-templates select="x:Name"/>
    <xsl:apply-templates select="x:Class"/>
    <xsl:apply-templates select="x:Age"/>
    <xsl:text>&#0010;</xsl:text>
  </xsl:for-each>
</xsl:template>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM