通过XSLT将XML转换为XML

时间:2014-01-13 07:58:23

标签: xml xslt

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="stylexslt3.xslt"?>
<team>
    <teaminfo>
        <logo source="logomym.jpg"/>
        <teampage address="http://www.site.com/"/>
        <name> Team name </name>
        <team_photo source="teamphoto.jpg"/>
        <game> Game1 </game>
        <achievements>
            <premiere_tournaments>
                <tournament>
                    <logoevent source="event1.jpg"/>
                    <tournament_name> tournament name </tournament_name>
                    <placing> 1 </placing>
                    <prize_pool>
                        <earned_prize> 10600 </earned_prize>
                        <average_prize> 4454 </average_prize>
                    </prize_pool>
                </tournament>
                <tournament>
                    <logoevent source="event2.jpg"/>
                    <tournament_name> tournament name </tournament_name>
                    <placing> 3 </placing>
                    <prize_pool>
                        <earned_prize> 10600 </earned_prize>
                        <average_prize> 4454 </average_prize>
                    </prize_pool>
                </tournament>

            </premiere_tournaments>

        </achievements>
    </teaminfo>
    <captain>
        <nick>Player1</nick>
        <role> Def </role>
        <captain_since>2011-01-14</captain_since>
        <Country>Country</Country>
    </captain>

    <player>
        <player_level>A-</player_level>
        <player_info streamer="y">
            <photo source="playerphoto.jpg"/>
            <nick>Playerino</nick>
            <rola>Att</rola>
            <style>roamer</style>
            <opinion_patch>Positive</opinion_patch>
            <fanpage adres="http://facebook.com/playerFanPage"/>
            <playergrade grade="Good"/>
        </player_info>
        <personal>
            <name>Matt</name>
            <surname>Kuk</surname>
            <age>22</age>
            <country>Counter</country>
        </personal>
    </player>
    <player>
        <player_level>A-</player_level>
        <player_info streamer="y">
            <photo source="playerphoto.jpg"/>
            <nick>Playerino</nick>
            <rola>Att</rola>
            <style>roamer</style>
            <opinion_patch>Positive</opinion_patch>
            <fanpage adres="http://facebook.com/playerFanPage"/>
            <playergrade grade="Good"/>
        </player_info>
        <personal>
            <name>Matt</name>
            <surname>Kuk</surname>
            <age>22</age>
            <country>Counter</country>
        </personal>
    </player>
    <sponsors>
        <sponsor>
            <companyname>Compname</companyname>
            <support_type>Periph, ads</support_type>
            <sponsor_page official_page="http://link.com"/>
            <fanpage fan_strona="http://lin43k.com"/>
        </sponsor>
    </sponsors>
    <perip>
        <mouse> mouse </mouse>
        <keyboard> keyboard </keyboard>
        <headphones> headphones </headphones>
    </perip>
</team>

我整晚都在为这件糟糕的事情而战,不幸的是,网上的材料数量并没有为我提供解决问题的满意方法。我想通过xslt对xml文件进行“转换”。我想这样做,以便改变它的结构和可能的属性或元素的名称。

我希望实现类似的目标:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="stylexslt3.xslt"?>
<team>
    <teaminfo>
        <logo source="logomym.jpg"/>
        <teampage address="http://www.site.com/"/>
        <name> Team name </name>
        <team_photo source="teamphoto.jpg"/>
        <game> Game1 </game>
    </teaminfo>
    <achievements>
        <premiere_tournaments>
            <tournament>
                <logoevent source="event1.jpg"/>
                <tournament_name> tournament name </tournament_name>
                <placing> 1 </placing>
                <prize_pool>
                    <earned_prize> 10600 </earned_prize>
                    <average_prize> 4454 </average_prize>
                </prize_pool>
            </tournament>
            <tournament>
                <logoevent source="event2.jpg"/>
                <tournament_name> tournament name </tournament_name>
                <placing> 3 </placing>
                <prize_pool>
                    <earned_prize> 10600 </earned_prize>
                    <average_prize> 4454 </average_prize>
                </prize_pool>
            </tournament>

        </premiere_tournaments>

    </achievements>

    <player>
        <nick>Player1</nick>
        <role> Def </role>
        <captain_since>2011-01-14</captain_since>
        <Country>Country</Country>
    </player>
    <player>
        <player_level>A-</player_level>
        <player_info streamer="y">
            <photo source="playerphoto.jpg"/>
            <nick>Playerino</nick>
            <rola>Att</rola>
            <style>roamer</style>
            <opinion_patch>Positive</opinion_patch>
            <fanpage adres="http://facebook.com/playerFanPage"/>
            <playergrade grade="Good"/>
        </player_info>
        <personal>
            <name>Matt</name>
            <surname>Kuk</surname>
            <age>22</age>
            <country>Counter</country>
        </personal>
    </player>
    <player>
        <player_level>A-</player_level>
        <player_info streamer="y">
            <photo source="playerphoto.jpg"/>
            <nick>Playerino</nick>
            <role>Att</role>
            <style>roamer</style>
            <opinion_patch>Positive</opinion_patch>
            <fanpage adres="http://facebook.com/playerFanPage"/>
            <playergrade grade="Good"/>
        </player_info>
        <personal>
            <name>Matt</name>
            <surname>Kuk</surname>
            <age>22</age>
            <country>Counter</country>
        </personal>
    </player>
</team>

如果可能,请包括更改元素名称的方法。我尝试用多种不同的方法做到这一点,但似乎我发现的方法不正确或者我无法弄清楚如何正确使用它们。我现在不会发布我的xslt,因为它被疯狂地屠杀了。

1 个答案:

答案 0 :(得分:0)

你未能明确说明第一个和第二个例子之间发生了什么变化(即使它被疯狂地屠杀,你也应该展示你的作品),但是我已经能够辨别出来了:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
  <xsl:strip-space elements="*" />

  <!-- Identity transform -->
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="teaminfo">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()[not(self::achievements)]" />
    </xsl:copy>
    <!-- Move achievements to after teaminfo-->
    <xsl:apply-templates select="achievements" />
  </xsl:template>

  <!-- Rename captain to player -->
  <xsl:template match="captain">
    <player>
      <xsl:apply-templates select="@* | node()" />
    </player>
  </xsl:template>

  <!-- Omit sponsors and perip -->
  <xsl:template match="sponsors | perip" />
</xsl:stylesheet>

我错过了什么吗?