如何使用XSLT读取这些动态数据字段

时间:2012-08-10 14:53:32

标签: xslt

我的情况是XML中的数据如下所示。我希望这些数据的格式如下面的输入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>

以下是我想要上述XML数据的现有XSLT格式....

<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>

以下是我最终想要的输出XML: -

<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>

先谢谢。希望有人可以帮我解决这个问题...

这是我到目前为止所尝试的...并且只产生了上面预期输出xml中显示的1条PersonPhoto记录。我的目标是捕获每个发生动态的记录

<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>

1 个答案:

答案 0 :(得分:1)

从它的外观来看,你的每个人都对照片元素进行操作,其中只有一个。如果我理解正确,你希望它反过来迭代在EventNumber,EventDate和DOB的子节点中的(在本例中为6,可能是##下一次)条目。尝试迭代photos / EventNumber / string。每次迭代,获取变量($ pos)中的位置,然后分别用<xsl:value-of select="." /><xsl:value-of select="../../EventDate/dateTime[position() = $pos]" /><xsl:value-of select="../../DOB/dateTime[position() = $pos]" />替换出现的值。如果你有多个