帮助XSLT转换

时间:2011-08-16 20:41:57

标签: xslt xslt-1.0

我希望能通过XSLT转换获得一些帮助。我似乎无法做对。

以下是源xml文档的示例:

    <?xml version="1.0" encoding="UTF-8"?>
    <Locations>
    <header>
       <location>Location Field</location>
       <job_function>Job Function Field</job_function>
       <count>Count</count>
    </header>
   <data>
      <location>2177</location>
      <job_function>ADM</job_function>
      <count>1</count>
   </data>
   <data>
        <location>2177</location>
      <job_function>OPS</job_function>
      <count>1</count>
   </data>
   <data>
      <location>2177</location>
      <job_function>SLS</job_function>
      <count>5</count>
   </data>
   <data>
      <location>2179</location>
      <job_function>ADM</job_function>
      <count>1</count>
   </data>
   <data>
      <location>2179</location>
      <job_function>SEC</job_function>
      <count>1</count>
   </data>
  </Locations>

我想将其转换为以下格式:

<Locations>
   <data>
    <PeopleSoftID>2177</PeopleSoftID>
    <ADM>1</ADM>
    <OPS>1</OPS>
    <SLS>5</SLS>
    <TotalCount>7</TotalCount>
   </data>
   <data>
    <PeopleSoftID>2179</PeopleSoftID>
    <ADM>1</ADM>
    <SEC>1</SEC>
    <TotalCount>2</TotalCount>
   </data>
 </Locations>

基本上,正如您在示例源文档中看到的那样,有多个元素具有相同的值。在目标文档中,源文档中每个<PeopleSoftID>元素值现在应该只有一个记录(<location>元素)。由于有3个<location>元素,其值为2177,因此目标文档现在只有1个包含该值的<PeopleSoftID>元素。源文档中<job_function>元素的值将成为目标文档中的元素。该新元素的值最终是源文档中<count>元素的兄弟值。目标文档中的<TotalCount>元素是从源<job_function>元素生成的所有新元素的值的总和。

我希望解释不会混淆任何人=)。

我对XSLT来说还是一个新手,所以我无法在这方面获得正确的逻辑。

我也只能使用XSLT 1.0。

如果我没有提供足够的信息让我知道,我会尽快提供更多信息。

谢谢你们!

1 个答案:

答案 0 :(得分:1)

阅读xsl:key并使用Muenchian Method 分组

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" />

    <!--Group the data elements by their location values -->
    <xsl:key name="data-by-location" match="data" use="location" />

    <xsl:template match="Locations">
        <xsl:copy>
            <!--Get a distinct list of location values, 
                using the Muenchian Method -->
            <xsl:for-each
                select="data[generate-id() =
                        generate-id(key('data-by-location', location)[1])]">
                <xsl:copy>
                    <PeopleSoftID>
                        <xsl:value-of select="location"/>
                    </PeopleSoftID>

                    <!--For every data element matching this location... -->
                    <xsl:for-each select="key('data-by-location',location)">
                        <!--Create an element using the job_function 
                            as the element name -->
                        <xsl:element name="{job_function}">
                            <!--The value of the count element
                                as the value of the generated element-->
                            <xsl:value-of select="count"/>
                        </xsl:element>
                    </xsl:for-each>

                    <TotalCount>
                        <!--calculate the sum of all the count element values 
                            for this location -->
                        <xsl:value-of select="sum(key('data-by-location',
                                                          location)/count)"/>
                    </TotalCount>
                </xsl:copy>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>