输入XML
<root>
<data>
<Data>
<rShop>ArgoElectrnicsShop</rShop>
<report>
<row>
<tableName>new tv</tableName>
<value>Samsung</value>
</row>
<row>
<tableName>new radio</tableName>
<value>Sony</value>
</row>
<row>
<tableName>new WashingMachine</tableName>
<value>Whirlpool</value>
</row>
</report>
</Data>
</data>
</root>
广义XSLT(将从此调用所有xslt)
<?xml version="1.0" encoding="utf-8"?>
<xsl:stlyesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:fn="www.w3.org/2005/xpath-functions" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()"/>
<xsl:strip-space elements="*"/>
<xsl:include href="ArgoElectrnicsShop.xslt"/>
<xsl:template match="/">
<xsl:element name="Documents">
<xsl:element name="DataModel">
<xsl:apply-templates select="/root/data/Data/rShop"/>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stlyesheet>
ArgoElectrnicsShop XSLT
<?xml version="1.0" encoding="utf-8"?>
<xsl:stlyesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:fn="www.w3.org/2005/xpath-functions" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:variable name="lowercase" select="'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:template match="/root/data/Data/rShop[text()='ArgoElectrnicsShop']">
<xsl:element name="T_New">
<xsl:apply-templates select="../report" mode="check"/>
</xsl:element>
</xsl:template>
<xsl:template match="report" mode="check">
<xsl:variable name="EntireValue">
<xsl:value-of select="row[substring-after(tableName, ' ')]/tableName"/>
</xsl:variable>
<xsl:variable name="Field">
<xsl:value-of select="row[substring-after($EntireValue, ' ')]"/>
</xsl:variable>
<xsl:call-template name="sample">
<xsl:with-param name="tableName" select="translate($EntireValue,$lowercase,$uppercase)"/>
<xsl:with-param name="value" select="row[substring-after(tableName, ' ')=$Field]/value"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="sample">
<xsl:param name="tableName"/>
<xsl:param name="value"/>
<xsl:choose>
<xsl:when test="substring-after($tableName, ' ')='TV'">
<xsl:element name="tv">
<xsl:value-of select="$value"/>
</xsl:element>
</xsl:when>
<xsl:when test="substring-after($tableName, ' ')='RADIO'">
<xsl:element name="radio">
<xsl:value-of select="$value"/>
</xsl:element>
</xsl:when>
<xsl:when test="substring-after($tableName, ' ')='WASHINGMACHINE'">
<xsl:element name="washingmachine">
<xsl:value-of select="$value"/>
</xsl:element>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stlyesheet>
输出XML:
<Documents>
<DataModel>
<T_New>
<tv>Samsung</tv>
</T_New>
</DataModel>
</Documents>
通过遍历所有行预期输出XML:
<Documents>
<DataModel>
<T_New>
<tv>Samsung</tv>
<radio>Sony</radio>
<washingmachine>Whirlpool</washingmachine>
</T_New>
</DataModel>
</Documents>
答案 0 :(得分:0)
给出这两个XSLT样式表:
XSLT 1.0(这是将由转化引擎调用的“通用xsl”)
<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:include href="New.xslt"/>
<xsl:template match="/root">
<Documents>
<DataModel>
<xsl:apply-templates select="data/Data"/>
</DataModel>
</Documents>
</xsl:template>
</xsl:stylesheet>
<强> New.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:variable name="lowercase">abcdefghijklmnopqrstuvwxyz</xsl:variable>
<xsl:variable name="uppercase">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
<xsl:template match="Data">
<T_New>
<xsl:apply-templates select="report/row"/>
</T_New>
</xsl:template>
<xsl:template match="row">
<xsl:element name="{translate(substring-after(tableName, ' '), $uppercase, $lowercase)}">
<xsl:value-of select="value"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
结果,当应用于您的输入示例时,将是:
<?xml version="1.0" encoding="UTF-8"?>
<Documents>
<DataModel>
<T_New>
<tv>Samsung</tv>
<radio>Sony</radio>
<washingmachine>Whirlpool</washingmachine>
</T_New>
</DataModel>
</Documents>