[英]How do I transform a XML file into HTML using XSLT in PHP?
[英]PHP - Is it possible to transform XML file to XSL file using xslt transformation
我正在嘗試使用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>
任何幫助將不勝感激。 提前致謝!
在XSLT規范本身中,有一個使用XSLT生成樣式表的示例: https ://www.w3.org/TR/xslt/#element-namespace-alias
嘗試類似的方法:
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>
注意事項 :
//
匹配模式中的前導//
完全是多余的;
您確定要在xsl:apply-templates
select
表達式中包含.//
嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.