为嵌套 XML 生成 XSL(非规范化格式)

Generate XSL (Denormalise format) for Nested XML

我需要将我的 XML 展平,因为文件大小非常大且嵌套级别,我正在手动创建 XSL 文件。下面是 XML 文件内容的示例场景-

<StudentDetail>
  <SchoolName>SSHPS</SchoolName>
  <SchoolEstablishedYear>1990</SchoolEstablishedYear>
  <ClassDetails>
    <ClassDetail>
      <ClassStartedYear>1990</ClassStartedYear>
      <Section ID="12345">
        <SectioName>Section A</SectioName>
        <Students>
          <Student ID="1">
            <StudentName>John</StudentName>
            <Address>
              <HomeNumber>10</HomeNumber>
              <StreetName>Avenue</StreetName>
            </Address>
          </Student>
          <Student ID="2">
            <StudentName>Steve</StudentName>
          </Student>
        </Students>
      </Section>
      <Section ID="123456">
        <SectioName>Section B</SectioName>
        <Students>
          <Student ID="100">
            <StudentName>Dia</StudentName>
            <Age>6</Age>
          </Student>
          <Student ID="101">
            <StudentName>Kevin</StudentName>
          </Student>
        </Students>
      </Section>
    </ClassDetail>
    <ClassDetail>
      <ClassStartedYear>1995</ClassStartedYear>
      <Section ID="543466">
        <SectioName>Section A</SectioName>
        <Students>
          <Student ID="200">
            <StudentName>Dia</StudentName>
            <Muncipality>
              <AreaCode>100</AreaCode>
              <Areaname>GRAND</Areaname>
            </Muncipality>
          </Student>
          <Student ID="201">
            <StudentName>Liva</StudentName>
          </Student>
        </Students>
      </Section>
      <Section ID="7543466">
        <SectioName>Section A</SectioName>
        <Students>
          <Student ID="300">
            <StudentName>Zane</StudentName>
          </Student>
          <Student ID="301">
            <StudentName>Susan</StudentName>
          </Student>
        </Students>
      </Section>
    </ClassDetail>
  </ClassDetails>
</StudentDetail>

下面是 XML-

的要求格式
<StudentDetail>
    <Student>
        <SchoolName>SSHPS</SchoolName>
        <SchoolEstablishedYear>1990</SchoolEstablishedYear>
        <ClassStartedYear>1990</ClassStartedYear>
        <SectionID>12345</SectionID>
        <SectioName>Section A</SectioName>
        <StudentID>1</StudentID>
        <StudentName>John</StudentName>
        <Address_HomeNumber>10</Address_HomeNumber>
        <Address_StreetName>Avenue</Address_StreetName>
        <Age> </Age>
        <Muncipality_AreaCode></Muncipality_AreaCode>
        <Muncipality_Areaname></Muncipality_Areaname>
    </Student>
   .
   .
   .
    <Student>
        <SchoolName>SSHPS</SchoolName>
        <SchoolEstablishedYear>1990</SchoolEstablishedYear>
        <ClassStartedYear>1995</ClassStartedYear>
        <SectionID>7543466</SectionID>
        <SectioName>Section A</SectioName>
        <StudentID>100</StudentID>
        <StudentName>Dia</StudentName>
        <Address_HomeNumber></Address_HomeNumber>
        <Address_StreetName></Address_StreetName>
        <Age></Age>
        <Muncipality_AreaCode>100</Muncipality_AreaCode>
        <Muncipality_Areaname>GRAND</Muncipality_Areaname>
    </Student>
</StudentDetail>

我已经生成了 XSL 模板,但我无法加载它,因为其中有一些错误-

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes" method="xml"/>
<xsl:template match="/">
    <StudentDetail>
        <xsl:for-each select="StudentDetail/ClassDetails">
         <Student>
            <SchoolName><xsl:value-of select="StudentDetail/SchoolName"/></SchoolName>
            <SchoolEstablishedYear><xsl:value-of select="StudentDetail/SchoolEstablishedYear"/></SchoolEstablishedYear>
            <ClassStartedYear><xsl:value-of select="StudentDetail/ClassDetails/ClassDetail/ClassStartedYear"/></ClassStartedYear>
            <StudentID><xsl:value-of select="StudentDetail/ClassDetails/Section/@ID"/></StudentID>
            <SectioName><xsl:value-of select="StudentDetail/ClassDetails/ClassDetail/Section/SectionName"/></SectioName>
            <StudentID><xsl:value-of select="StudentDetail/ClassDetails/ClassDetail/Section/Students/Student/@ID"/></StudentID>
            <StudentName><xsl:value-of select="StudentDetail/ClassDetails/ClassDetail/Section/Students/Student"/></StudentName>
            <Address_HomeNumber><xsl:value-of select="StudentDetail/ClassDetails/ClassDetail/Section/Students/Student/Address/HomeNumber"/></Address_HomeNumber>
            <Address_StreetName><xsl:value-of select="StudentDetail/ClassDetails/ClassDetail/Section/Students/Student/Address/StreetName"/></Address_StreetName>
            <Age><xsl:value-of select="StudentDetail/ClassDetails/ClassDetail/Section/Students/Student/Age"/></Age>
            <Muncipality_AreaCode><xsl:value-of select="StudentDetail/ClassDetails/ClassDetail/Section/Students/Student/Muncipality/AreaCode"/></Muncipality_AreaCode>
            <Muncipality_Areaname><xsl:value-of select="StudentDetail/ClassDetails/ClassDetail/Section/Students/Student/Muncipality/Areaname"/></Muncipality_Areaname>
         </Student>
        </xsl:for-each>
    </StudentDetail>       
</xsl:template>

我是处理 XML 的新手,我一直无法处理嵌套的 XML

首先,令我惊讶的是,竟然有人想将这种结构良好的输入转化为这种结构糟糕的输出。但我们不是来讨论这个的。

其次,我对您“手动生成 XSL”的说法感到困惑。我本以为您是手动创建它,或者您是通过编程方式生成它,但不清楚是哪种情况。

第三,您告诉我们生成的 XSL 中存在“一些错误”,但您没有告诉我们错误是什么。我能看到的唯一错误是 xsl:stylesheet 元素缺少关闭标记,这可能是一个拼写错误。如果遇到错误,请告诉我们错误是什么。

第四,似乎有一个更简单的方法。据我所知,您可以通过应用三个规则来获得所需的输出:

  • 如果一个元素有子元素,只处理它的子元素

  • 如果元素有ID属性,将<X ID="x"/>改为<XID>x</XID>,然后处理它的子元素

  • 如果元素有文本节点子节点,则原样复制。

第一条规则对应默认的XSLT处理规则;其他两个规则可以在 XSLT 中简单地表达为:

<xsl:template match="*[@ID]">
  <xsl:element name="{name()}ID">
    <xsl:value-of select="@ID"/>
  </xsl:element>
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="*[text()]">
  <xsl:copy-of select="."/>
</xsl:template>

您的样式表的主要问题是您正在为每个 ClassDetails 而不是每个 Student.

创建一个 Student

而不是:

<xsl:for-each select="StudentDetail/ClassDetails">
    <Student>
        <!-- data -->
    </Student>
</xsl:for-each>

你应该做的:

<xsl:for-each select="StudentDetail/ClassDetails/ClassDetail/Section/Students/Student">
    <Student>
        <!-- data -->
    </Student>
</xsl:for-each>

然后,在 Student 元素中,您需要从祖先 SchoolClassDetailSection 部分以及子元素中检索数据当前 Student 节点。

为了尽量减少重复上下导航树的需要,我建议将祖先部件的详细信息放在变量中并从那里访问它们:

XSLT 1.0

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

<xsl:template match="/StudentDetail">
    <xsl:copy>
        <xsl:variable name="school-details" select="SchoolName | SchoolEstablishedYear"/>
        <xsl:for-each select="ClassDetails/ClassDetail">
            <xsl:variable name="class-details" select="ClassStartedYear"/>
            <xsl:for-each select="Section">
                <xsl:variable name="section-details">
                    <SectionID>
                        <xsl:value-of select="@ID"/>
                    </SectionID>
                    <xsl:copy-of select="SectioName"/>
                </xsl:variable>
                <xsl:for-each select="Students/Student">
                    <xsl:copy>
                        <xsl:copy-of select="$school-details | $class-details"/>
                        <xsl:copy-of select="$section-details"/>
                        <StudentID>
                            <xsl:value-of select="@ID"/>
                        </StudentID>
                        <xsl:copy-of select="StudentName"/>
                        <Address_HomeNumber>
                            <xsl:value-of select="Address/HomeNumber"/>
                        </Address_HomeNumber>
                        <Address_StreetName>
                            <xsl:value-of select="Address/StreetName"/>
                        </Address_StreetName>
                        <Age>
                            <xsl:value-of select="Age"/>
                        </Age>
                        <Muncipality_AreaCode>
                            <xsl:value-of select="Muncipality/AreaCode"/>
                        </Muncipality_AreaCode>
                        <Muncipality_Areaname>
                            <xsl:value-of select="Muncipality/Areaname"/>
                        </Muncipality_Areaname>
                    </xsl:copy>
                </xsl:for-each>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>