XSLT翻译:我有一个带有嵌套数据的XML文件,我需要将它翻译成一个扁平的xml文件

时间:2013-03-07 21:58:59

标签: xml xslt xml-parsing

以下是我正在使用的内容......

<response code="OK">
      <enrollments>
           <enrollment enddate="9999-12-31T00:00:00Z" startdate="1753-01-01T00:00:00Z" status="1" flags="2228225" guid="a469dd60-8350-4749-8281-7772b6798bcc" reference="MajorMinor.109457483" domainid="616401" entityid="6557156" userid="1554956" id="6557282">
-                <data>
-                   <status>
                        <performance code="2" signal="Red"/><pace signal="Green"/>
                    </status>
                </data>
                <user guid="a992267a-a2ca-4e0c-a8d1-447d0243a4d8" reference="109457483" id="1554956" lastlogindate="1753-01-01T00:00:00Z" email="bak05016@thisone.edu" username="rbaker4" userspace="Thisone" lastname="Baker" firstname="Ryan"/>
                   <domain id="616401" name="This one"/>
-             <grades seconds="0" complete="1" passingscore="0.7" letter="F" possible="100" achieved="45">
-               <items>
                   <item title="" categoryid="1" periodid="0" itemid="DEFAULT"/>
                   <item title="Test 1" categoryid="1" periodid="0" itemid="JECIY"/>
                   <item title="This test will be fun" status="261" letter="F" possible="100" achieved="10" categoryid="1" periodid="0" itemid="7I673" duedate="9999-12-31T23:59:59.9999999Z" scoreddate="2013-03-05T23:31:43.653Z" responseversion="4"/>
                   <item title="Do this homework recieve credit" status="261" letter="B" possible="100" achieved="80" categoryid="1" periodid="0" itemid="39AM7" duedate="9999-12-31T23:59:59.9999999Z" scoreddate="2013-03-07T16:54:08.033Z" responseversion="1"/>
             </items>
-        <categories>
             <category id="1" name="Include" letter="F" possible="200" achieved="90"/><category id="0" name="Exclude" possible="0" achieved="0"/>
         </categories>
     </grades>
   </enrollment>
 </enrollments>
</response code>

所以我正在使用以下代码获取此代码并运行XSLT转换。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" />

<xsl:template match="/">

<Enrollments xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="results.xsd">
      <xsl:for-each select="response/enrollments/enrollment">
      <Enrollment>
        <FirstName>
          <xsl:value-of select="user/@firstname"/>
        </FirstName>
        <LastName> 
          <xsl:value-of select="user/@lastname"/>
        </LastName>
        <ID>
          <xsl:value-of select="user/@id"/>
        </ID>
        <Email>
          <xsl:value-of select="user/@email"/>
        </Email>
        <Grade>
          <xsl:value-of select="grades/@achieved"/>
        </Grade>
        <Assignments>
          <xsl:value-of select="grades/items/item/@title"/>
            </xsl:for-each>
        </Assignments>
      </Enrollment>
      </xsl:for-each>
    </Enrollments>
</xsl:template>
</xsl:stylesheet>

我的问题是xslt中的assignments标签返回xml数据的已知结果。我需要一种方法让它返回得分的信息。在某种程度上,对于每个项目,将在表格中创建一个新列,其中列标题是赋值ex的名称。 “做这个功课得到信用”然后表的内容是他们得分的等级。我正在使用的原始xml文档有大约4000行,我们需要将其翻译成可用格式的数据库。

1 个答案:

答案 0 :(得分:0)

也许您正在为Assignments元素中的XSLT寻找类似的东西?

<xsl:for-each select="grades/items/item">
  <Assignment>
    <Title><xsl:value-of select="@title"/></Title>
    <Grade><xsl:value-of select="@achieved"/></Grade>
  </Assignment>
</xsl:for-each>