提取&使用XSLT将属性转换为元素

时间:2014-06-22 20:45:16

标签: xml xslt

我有以下XML:

<?xml version="1.0" encoding="UTF-8"?>
<RDATE date="2014-03-14">
    <STUDENT id="A-001" sex="M" group_a="yellow" group_b="green">
        <NAME id="A-001-1" STUDENT_id="A-001" group="yellow">
            <L_NAME>Yuvraj</L_NAME>
            <M_NAME/>
            <F_NAME>Bhatia</F_NAME>
            <O_NAME/>
            <AGE>15</AGE>
            <ETHNICITY>Asian</ETHNICITY>
        </NAME>
        <ORIGIN id="A-001-A-O" region="Asia">
            <BIRTH>1999-01-01</BIRTH>
            <PLACE>Mumbai</PLACE>
        </ORIGIN>
        <IDENT id="A-001-A-O-I" type="paper/photo">
            <PASSPORT/>
            <COUNTRY/>
        </IDENT>
    </STUDENT>
    <STUDENT id="A-002" sex="F" group_a="green" group_b="blue">
        <NAME id="A-002-1" STUDENT_id="A-002" group="green">
            <L_NAME>Singh</L_NAME>
            <M_NAME/>
            <F_NAME>Palwinder</F_NAME>
            <O_NAME>Palwinder Singh</O_NAME>
            <AGE>19</AGE>
            <ETHNICITY>Asian</ETHNICITY>
        </NAME>
        <NAME id="A-002-2" STUDENT_id="A-002" group="green">
            <L_NAME>Singh</L_NAME>
            <M_NAME>Balwant</M_NAME>
            <F_NAME>Palwinder</F_NAME>
            <O_NAME>Balwant Singh</O_NAME>
            <AGE>19</AGE>
            <ETHNICITY>Asian</ETHNICITY>
        </NAME>
    </STUDENT>
</RDATE>

这给了我以下输出:

<?xml version="1.0" encoding="utf-8"?><RDATE><date>2014-03-14</date>
    <STUDENT><id>A-001</id><sex>M</sex><group_a>yellow</group_a><group_b>green</group_b>
        <NAME><id>A-001-1</id><STUDENT_id>A-001</STUDENT_id><group>yellow</group>
            <L_NAME>Yuvraj</L_NAME>
            <M_NAME/>
            <F_NAME>Bhatia</F_NAME>
            <O_NAME/>
            <AGE>15</AGE>
            <ETHNICITY>Asian</ETHNICITY>
        </NAME>
        <ORIGIN><id>A-001-A-O</id><region>Asia</region>
            <BIRTH>1999-01-01</BIRTH>
            <PLACE>Mumbai</PLACE>
        </ORIGIN>
        <IDENT><id>A-001-A-O-I</id><type>paper/photo</type>
            <PASSPORT/>
            <COUNTRY/>
        </IDENT>
    </STUDENT>
    <STUDENT><id>A-002</id><sex>F</sex><group_a>green</group_a><group_b>blue</group_b>
        <NAME><id>A-002-1</id><STUDENT_id>A-002</STUDENT_id><group>green</group>
            <L_NAME>Singh</L_NAME>
            <M_NAME/>
            <F_NAME>Palwinder</F_NAME>
            <O_NAME>Palwinder Singh</O_NAME>
            <AGE>19</AGE>
            <ETHNICITY>Asian</ETHNICITY>
        </NAME>
        <NAME><id>A-002-2</id><STUDENT_id>A-002</STUDENT_id><group>green</group>
            <L_NAME>Singh</L_NAME>
            <M_NAME>Balwant</M_NAME>
            <F_NAME>Palwinder</F_NAME>
            <O_NAME>Balwant Singh</O_NAME>
            <AGE>19</AGE>
            <ETHNICITY>Asian</ETHNICITY>
        </NAME>
    </STUDENT>
</RDATE>

使用以下XSLT进行转换时:

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

  <xsl:template match="*">
    <xsl:element name="{name()}">
      <xsl:for-each select="@*">
        <xsl:element name="{name()}">
          <xsl:value-of select="."/>
        </xsl:element>
      </xsl:for-each>
      <xsl:apply-templates select="*|text()"/>
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>

但我期待以下输出:

<?xml version="1.0" encoding="UTF-8"?>
<DATA>
    <STUDENTS>
        <STUDENT>
            <UNIQUE-ID>A-001</UNIQUE-ID>
            <GENDER>Male</GENDER>
            <NAME_A>Bhatia</NAME_A>
            <NAME_B/>
            <NAME_C>Yuvraj</NAME_C>
            <COMMENTS>
                <GROUP_A>Yellow</GROUP_A>
                <GROUP_B>Green</GROUP_B>
                <STUDENT_id>A-001</STUDENT_id>
                <IDENT_id>A-001-A-O-I</IDENT_id>
                <IDENT_type>paper/photo</IDENT_type>
            </COMMENTS>
            <BORN>
                <DATE>
                    <A>1999-01-01</A>
                </DATE>
            </BORN>
            <IDENTS>
                <IDENT>
                    <PASSPORT/>
                    <COUNTRY/>
                </IDENT>
            </IDENTS>
        </STUDENT>
    </STUDENTS>
</DATA>

这里必须注意的是:

  • 我的XML有超过5000名独特学生。
  • 每个学生可能有一个或多个“NAME”属性/元素(如第二个学生所示)及其相应元素。
  • 每个学生可能有一个或多个“ORIGIN”属性/元素及其相应的元素。
  • 每个学生可能有一个或多个“IDENT”属性/元素及其相应元素。
  • 原始XML中可能存在或不存在其他属性。所有这些都将在转换后纳入“COMMENTS”元素下。

1 个答案:

答案 0 :(得分:1)

我不确定你的困难究竟是什么:这似乎是基本XSLT中的练习。你只需要更明确(和冗长)。看看这是否可以帮助您入门:

<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="/">
    <DATA>
        <STUDENTS>
            <xsl:apply-templates select="RDATE/STUDENT"/>
        </STUDENTS>
    </DATA>
</xsl:template>

<xsl:template match="STUDENT">
    <STUDENT>
        <UNIQUE-ID><xsl:value-of select="@id"/></UNIQUE-ID>
        <GENDER>
            <xsl:choose>
                <xsl:when test="@sex='M'">Male</xsl:when>
                <xsl:otherwise>Female</xsl:otherwise>
            </xsl:choose>
        </GENDER>

        <!-- and so on ... -->

    </STUDENT>
</xsl:template>

</xsl:stylesheet>