[英]PHP - Is it possible to transform XML file to XSL file using xslt transformation
I am trying to generate XSL file based on the XML config file using xsl transformation. 我正在尝试使用xsl转换基于XML配置文件生成XSL文件。
Here is my input. 这是我的意见。
<front>
<sample1/>
<sample2/>
<sample3>
<item1/>
<item2/>
<item3/>
<item4/>
</sample3>
<sample4/>
</front>
I am expecting to get xsl file like, 我期望得到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>
Is this can be done using xsl transformation? 这可以使用xsl转换完成吗? The main idea is each and every time I make changes in the config file, I no need to write xsl file. 主要思想是每当我在配置文件中进行更改时,都无需编写xsl文件。 It should be generated. 应该生成它。 If I make changes like, 如果我进行更改,
<front>
<sample1/>
<sample2/>
<sample4/>
<sample3>
<item1/>
<item2/>
<item4/>
<item3/>
</sample3>
</front>
The xsl should be, 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>
Any help will be appreciated. 任何帮助将不胜感激。 Thanks in advance! 提前致谢!
There is an example of using XSLT to generate a sttylesheet in the XSLT specification itself: https://www.w3.org/TR/xslt/#element-namespace-alias 在XSLT规范本身中,有一个使用XSLT生成样式表的示例: https ://www.w3.org/TR/xslt/#element-namespace-alias
Try something like: 尝试类似的方法:
XSLT 1.0 XSLT 1.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 : 注意事项 :
A leading //
is entirely redundant in a match pattern; //
匹配模式中的前导//
完全是多余的;
Are you sure you want to have that .//
in your xsl:apply-templates
select
expressions? 您确定要在xsl:apply-templates
select
表达式中包含.//
吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.