2011-01-26 54 views
1

我正在嘗試使用XSLT重新映射XML文件,但我不熟悉XSLT如何執行此過程。基本上我需要以下XML轉換:使用XSLT將XML文檔重新映射到新的XML文件

<rows> 
<row> 
<site id>My Website</site id> 
<pageName>Homepage</pageName> 
<channel>microsite</channel> 
<tree>mywebsite/subsite/homepage</tree> 
<url name>brand name</url name> 
<urltype>2</urltype> 
<spec4>{lang}</spec4> 
<spec6>company:section:homepage</spec6> 
<spec11>ms:plinth</spec11> 
<spec12>2010</spec12> 
<spec14>company-{country}</spec14> 
<spec15>site:brand name</spec15> 
<spec16>Brand Name</spec16> 
<spec18></spec18> 
<spec30>site:brand name</spec30> 
<add4>{lang}</add4> 
<add5>ref:company</add5> 
<add6>company:section:homepage</add6> 
<add11>ms:plinth</add11> 
<add12>2010</add12> 
<add14>company-{country}</add14> 
<add15>site:brand name</add15> 
<add16>Brand Name</add16> 
<add30>site:brand name</add30> 
<actions4>action 1</actions4> 
<actions5>action 3</actions5> 
</row> 
</rows> 

進入結構更好的一個:

<siteSpecific> 
<site id="My Website" urlType="page"> 
    <general> 
    <pageName>ms:sbe:Brand Name:tdr</pageName> 
    <channel>microsite</channel> 
    <tree>mywebsite/subsite/homepage</tree> 
    <urlName>referral:brand</urlName> 
    <urlType>2</urlType> 
    </general> 
    <specs> 
    <spec4>{lang}</spec4> 
    <spec6>company:section:homepage</spec6> 
    <spec11>ms:plinth</spec11> 
    <spec12>2010</spec12> 
    <spec14>company-{country}</spec14> 
    <spec15>site:brand name</spec15> 
    <spec16>Brand Name</spec16> 
    <spec30>site:brand name</spec30> 
    </specs> 
    <adds> 
    <add4>{lang}</add4> 
    <add5>ref:company</add5> 
    <add6>company:section:homepage</add6> 
    <add11>ms:plinth</add11> 
    <add12>2010</add12> 
    <add14>company-{country}</add14> 
    <add15>site:brand name</add15> 
    <add16>Brand Name</add16> 
    <add30>site:brand name</add30> 
    </adds> 
    <actions> 
    <actions4>action 1</actions4> 
    <actions5>action 3</actions5> 
    </actions> 
</site> 
</siteSpecific> 

任何人都可以點我在正確的方向?我看過類似的帖子,但這些帖子都很老舊,而且由於XSLT似乎沒有用於這個目的,所以現在我也在想這是不是最好的選擇?

回答

2

XML中不允許使用像site id這樣的名稱,所以我必須對輸入XML做出一些假設。假設它是

<rows> 
<row> 
<site_id>My Website</site_id> 
<pageName>Homepage</pageName> 
<channel>microsite</channel> 
<tree>mywebsite/subsite/homepage</tree> 
<url_name>brand name</url_name> 
<urltype>2</urltype> 
<spec4>{lang}</spec4> 
<spec6>company:section:homepage</spec6> 
<spec11>ms:plinth</spec11> 
<spec12>2010</spec12> 
<spec14>company-{country}</spec14> 
<spec15>site:brand name</spec15> 
<spec16>Brand Name</spec16> 
<spec18></spec18> 
<spec30>site:brand name</spec30> 
<add4>{lang}</add4> 
<add5>ref:company</add5> 
<add6>company:section:homepage</add6> 
<add11>ms:plinth</add11> 
<add12>2010</add12> 
<add14>company-{country}</add14> 
<add15>site:brand name</add15> 
<add16>Brand Name</add16> 
<add30>site:brand name</add30> 
<actions4>action 1</actions4> 
<actions5>action 3</actions5> 
</row> 
</rows> 

然後樣式表

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

    <xsl:output indent="yes"/> 
    <xsl:strip-space elements="*"/> 

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

    <xsl:template match="rows"> 
    <siteSpecific> 
     <xsl:apply-templates/> 
    </siteSpecific> 
    </xsl:template> 

    <xsl:template match="row"> 
    <site id="{site_id}" urlType="page"> 
     <general> 
     <xsl:apply-templates select="pageName | channel | tree | url_name | urltype"/> 
     </general> 
     <specs> 
     <xsl:apply-templates select="*[starts-with(local-name(), 'spec')]"/> 
     </specs> 
     <adds> 
     <xsl:apply-templates select="*[starts-with(local-name(), 'add')]"/> 
     </adds> 
     <actions> 
     <xsl:apply-templates select="*[starts-with(local-name(), 'action')]"/> 
     </actions> 
    </site> 
    </xsl:template> 

    <xsl:template match="url_name"> 
    <urlName> 
     <xsl:apply-templates/> 
    </urlName> 
    </xsl:template> 

    <xsl:template match="urltype"> 
    <urlType> 
     <xsl:apply-templates/> 
    </urlType> 
    </xsl:template> 

</xsl:stylesheet> 

創建以下輸出:

<siteSpecific> 
    <site id="My Website" urlType="page"> 
     <general> 
     <pageName>Homepage</pageName> 
     <channel>microsite</channel> 
     <tree>mywebsite/subsite/homepage</tree> 
     <urlName>brand name</urlName> 
     <urlType>2</urlType> 
     </general> 
     <specs> 
     <spec4>{lang}</spec4> 
     <spec6>company:section:homepage</spec6> 
     <spec11>ms:plinth</spec11> 
     <spec12>2010</spec12> 
     <spec14>company-{country}</spec14> 
     <spec15>site:brand name</spec15> 
     <spec16>Brand Name</spec16> 
     <spec18/> 
     <spec30>site:brand name</spec30> 
     </specs> 
     <adds> 
     <add4>{lang}</add4> 
     <add5>ref:company</add5> 
     <add6>company:section:homepage</add6> 
     <add11>ms:plinth</add11> 
     <add12>2010</add12> 
     <add14>company-{country}</add14> 
     <add15>site:brand name</add15> 
     <add16>Brand Name</add16> 
     <add30>site:brand name</add30> 
     </adds> 
     <actions> 
     <actions4>action 1</actions4> 
     <actions5>action 3</actions5> 
     </actions> 
    </site> 
</siteSpecific> 
+0

1爲一個很好的解決方案。 – Flack 2011-01-26 11:55:15