我正在尝试使用XSLT重新映射XML文件,但我不熟悉XSLT如何处理此过程。基本上我需要转换以下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似乎没有用于此目的,这些天我也想知道这是否是最佳选择?
答案 0 :(得分: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>