Harsh
Harsh

Reputation: 45

How to read these Dynamic Data fields using XSLT

I have situation where the Data from the XML is as below. I want this data in the format shown after below Input XML

**Input XML:-**

<JLINKMetadata>
    <photos>
        <EventNumber>
            <string>120423007237</string>
            <string>120602009897</string>
            <string>071030-3242</string>
            <string>071022-2374</string>
            <string>071010-2484</string>
            <string>071018-2894</string>
        </EventNumber>
            <EventDate>
                <dateTime>2012-04-23T06:27:00</dateTime>
                <dateTime>2012-06-02T18:53:00</dateTime>
                <dateTime>2007-10-30T20:35:00</dateTime>
                <dateTime>2007-10-22T16:45:00</dateTime>
                <dateTime>2007-10-10T16:50:00</dateTime>
                <dateTime>2007-10-18T19:40:00</dateTime>
            </EventDate>
        <DOB>
            <dateTime>1965-07-08T00:00:00</dateTime>
            <dateTime>1965-07-08T00:00:00</dateTime>
            <dateTime>1965-07-08T00:00:00</dateTime>
            <dateTime>1965-07-08T00:00:00</dateTime>
            <dateTime>1965-07-08T00:00:00</dateTime>
            <dateTime>1965-07-08T00:00:00</dateTime>
        </DOB>
    </photos>
</JLINKMetadata>

Following is the Existing XSLT Format in which I want the above XML data....

<PersonPhoto>                 
    <!--PersonPhotos.EventNumber-->
    <EventIdentification>
        <IdentificationID>EVT12345</IdentificationID>
    </EventIdentification>

    <EventDate>
        <!--PersonPhotos.EventDate-->
        <Date>2007-02-20</:Date>
    </EventDate>                    

    <PersonBirthDate>
        <!--PersonPhotos.BirthDate-->
        <Date>1981-02-20</Date>
    </PersonBirthDate>

</PersonPhoto>

Here is the Output XML how I wanted finally:-

<Photos>
    <PersonPhoto>
        <EventIdentification>
            <IdentificationID>120423007237</IdentificationID>
        </EventIdentification>
        <EventDate>
            <Date>04/23/2012</Date>
        </EventDate>
        <PersonBirthDate>
            <Date>7/8/1965</Date>
        </PersonBirthDate>
    </PersonPhoto>

    <PersonPhoto>
        <EventIdentification>
            <IdentificationID>120602009897</IdentificationID>
        </EventIdentification>
        <EventDate>
            <Date>10/22/2007</Date>
        </EventDate>
        <PersonBirthDate>
            <Date>11/6/1945</Date>
        </PersonBirthDate>
    </PersonPhoto>

    <PersonPhoto>
        <EventIdentification>
            <IdentificationID>120602009897</IdentificationID>
        </EventIdentification>
        <EventDate>
            <Date>5/12/2011</Date>
        </EventDate>
        <PersonBirthDate>
            <Date>1/3/1955</Date>
        </PersonBirthDate>
    </PersonPhoto>

</Photos>

Thanks in Advance.. Hope someone could help me with this situation...

Here is what I tried so far.. and yielded only 1 record of PersonPhoto shown in expected output xml above .. My aim is to capture every record whose occurrence is dynamic

<xsl:variable name="Photos" select="photos"/>

<xsl:for-each select="$Photos"> 
  <PersonPhoto>                 
    <!--PersonPhotos.EventNumber-->
        <EventIdentification>
      <IdentificationID>
    <xsl:value-of select="EventNumber/string" />
      </IdentificationID>
        </EventIdentification>

    <EventDate>
        <!--PersonPhotos.EventDate-->
        <Date>
        <xsl:value-of select="EventDate/dateTime" />
        </Date>
    </EventDate>                    

    <PersonBirthDate>
        <!--PersonPhotos.BirthDate-->
        <Date>
        <xsl:value-of select="DOB/dateTime" />
        </Date>
    </PersonBirthDate>

</PersonPhoto>

Upvotes: 0

Views: 253

Answers (1)

AlwaysWrong
AlwaysWrong

Reputation: 526

From the look of it, your for-each operates on the photos element, of which there is only one. If I understand correctly, you want it to instead iterate over the (6 in this example, could be ## next time) entries in the children of EventNumber, EventDate, and DOB. Try iterating over photos/EventNumber/string. Each iteration, grab the position in a variable ($pos), then replace the value-of occurrences with <xsl:value-of select="." />, <xsl:value-of select="../../EventDate/dateTime[position() = $pos]" />, and , <xsl:value-of select="../../DOB/dateTime[position() = $pos]" /> respectively. If you have multiple

Upvotes: 1

Related Questions