【问题标题】:PHP - Is it possible to transform XML file to XSL file using xslt transformationPHP - 是否可以使用 xslt 转换将 XML 文件转换为 XSL 文件
【发布时间】:2017-01-05 00:58:36
【问题描述】:

我正在尝试使用 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>

任何帮助将不胜感激。提前致谢!

【问题讨论】:

    标签: php xml xslt


    【解决方案1】:

    在 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>
    

    注意

    1. 前导// 在匹配模式中完全是多余的;

    2. 您确定要在您的xsl:apply-templates select表达式中包含.// 吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-15
      • 2012-04-21
      • 2011-12-25
      相关资源
      最近更新 更多