我正在尝试使用XSL转换基于XML配置文件生成xsl文件。
这是我的意见。
<front>
<sample1/>
<sample2/>
<sample3>
<item1/>
<item2/>
<item3/>
<item4/>
</sample3>
<sample4/>
</front>我希望能得到类似的xsl文件
<xsl:template match="//div[@class='front']">
<xsl:apply-templates select=".//*[@class='sample1']"/>
<xsl:apply-templates select=".//*[@class='sample2']"/>
<xsl:apply-templates select=".//*[@class='sample3']"/>
<xsl:apply-templates select=".//*[@class='sample4']"/>
</xsl:template>
<xsl:template match="//*[@class='sample3']">
<xsl:apply-templates select=".//*[@class='item1']"/>
<xsl:apply-templates select=".//*[@class='item2']"/>
<xsl:apply-templates select=".//*[@class='item3']"/>
<xsl:apply-templates select=".//*[@class='item4']"/>
</xsl:template> 这可以使用xsl转换完成吗?主要思想是,每次在配置文件中进行更改时,我都不需要编写xsl文件。它应该产生。如果我做出改变,
<front>
<sample1/>
<sample2/>
<sample4/>
<sample3>
<item1/>
<item2/>
<item4/>
<item3/>
</sample3>
</front>xsl应该是,
<xsl:template match="//div[@class='front']">
<xsl:apply-templates select=".//*[@class='sample1']"/>
<xsl:apply-templates select=".//*[@class='sample2']"/>
<xsl:apply-templates select=".//*[@class='sample4']"/>
<xsl:apply-templates select=".//*[@class='sample3']"/>
</xsl:template>
<xsl:template match="//*[@class='sample3']">
<xsl:apply-templates select=".//*[@class='item1']"/>
<xsl:apply-templates select=".//*[@class='item2']"/>
<xsl:apply-templates select=".//*[@class='item4']"/>
<xsl:apply-templates select=".//*[@class='item3']"/>
</xsl:template>任何帮助都将不胜感激。提前感谢!
发布于 2016-08-29 05:30:26
有一个使用XSLT在XSLT规范本身中生成sttylesheet的示例:https://www.w3.org/TR/xslt/#element-namespace-alias
试一试如下:
XSLT1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:axsl="http://www.w3.org/1999/XSL/TransformAlias">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/>
<xsl:template match="/">
<axsl:stylesheet version="1.0">
<xsl:apply-templates/>
</axsl:stylesheet>
</xsl:template>
<xsl:template match="/*">
<axsl:template match="div[@class='{name()}']">
<xsl:apply-templates mode="apply"/>
</axsl:template>
<xsl:apply-templates select="*[*]"/>
</xsl:template>
<xsl:template match="*">
<axsl:template match="*[@class='{name()}']">
<xsl:apply-templates mode="apply"/>
</axsl:template>
<xsl:apply-templates select="*[*]"/>
</xsl:template>
<xsl:template match="*" mode="apply">
<axsl:apply-templates select=".//*[@class='{name()}']"/>
</xsl:template>
</xsl:stylesheet>Note
//是完全冗余的;xsl:apply-templates select表达式中使用该select吗?https://stackoverflow.com/questions/39198727
复制相似问题