Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-是否可以使用xslt转换将XML文件转换为XSL文件_Php_Xml_Xslt - Fatal编程技术网

PHP-是否可以使用xslt转换将XML文件转换为XSL文件

PHP-是否可以使用xslt转换将XML文件转换为XSL文件,php,xml,xslt,Php,Xml,Xslt,我正在尝试使用XSL转换基于XML配置文件生成XSL文件 这是我的意见 <front> <sample1/> <sample2/> <sample3> <item1/> <item2/> <item3/> <item4/> </sample3> <sample4/> </front>

我正在尝试使用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规范本身中生成sttylesheet的示例:

尝试以下方法:

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>


注意

  • 前导的
    /
    在匹配模式中是完全冗余的

  • 是否确实要在
    xsl:apply-templates
    选择
    表达式中包含该
    /