Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.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
使用XSLT将文档中的所有脚本标记移动到head标记中_Xslt_Xpath - Fatal编程技术网

使用XSLT将文档中的所有脚本标记移动到head标记中

使用XSLT将文档中的所有脚本标记移动到head标记中,xslt,xpath,Xslt,Xpath,我正在使用XSLT和几个XML文件来生成我的网页。我希望能够在XML中添加一个小部件标记,将小部件插入html中相应的位置。这个小部件还由一个XML文件定义,该文件定义了它的html内容和其他信息 这进行得很顺利,但是现在小部件需要一个或多个脚本,它的定义如下 <scripts> <script>jquery<script> <script>animation</script> </scripts> 这将导

我正在使用XSLT和几个XML文件来生成我的网页。我希望能够在XML中添加一个小部件标记,将小部件插入html中相应的位置。这个小部件还由一个XML文件定义,该文件定义了它的html内容和其他信息

这进行得很顺利,但是现在小部件需要一个或多个脚本,它的定义如下

<scripts>
    <script>jquery<script>
    <script>animation</script>
</scripts>
这将导致以下模板:

<xsl:template match="head" mode="collect-resources">
    <xsl:for-each select="..//scripts">
        <xsl:apply-templates select="current()/*" mode="scripts" /> <!-- Convert the scripts defined in this scripts tag into proper HTML script tags. -->
    </xsl:for-each>
</xsl:template>

<xsl:template match="scripts" mode="collect-resources" /> <!-- Remove all scripts tags. -->

<xsl:template match="node()" mode="collect-resources">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates select="./node()" mode="collect-resources"/>
    </xsl:copy>
</xsl:template>
附录II-template.xsl

<xsl:template match="widget" mode="content">
    <xsl:variable name="path">../widgets/<xsl:value-of select="@name" />/widget.xml</xsl:variable> <!-- The path to the widget xml -->
    <xsl:variable name="widget" select="document($path)"/>
    <xsl:element name="div">
        <xsl:attribute name="class">widget</xsl:attribute>
        <xsl:apply-templates select="$widget/widget/scripts" mode="content" /> <!-- Copy the widget script information. -->
        <xsl:apply-templates select="$widget/widget/content/*" mode="content" /> <!-- Copy the widget contents. -->
    </xsl:element>
</xsl:template>

<xsl:template match="node()" mode="content"> <!-- Copy anything that is not a widget. -->
    <xsl:copy>
        <xsl:copy-of select="@*" />
        <xsl:apply-templates select="./node()" mode="content" />
    </xsl:copy>
</xsl:template>
它似乎在你的内心

        <head>
            <title><xsl:value-of select="page/title" /></title>
            <xsl:apply-templates select="/page/scripts" mode="content" />
            <xsl:apply-templates select="/page/sheets" mode="content" />
        </head>

在另一个模板中,您将不会输出脚本

<xsl:template match="widget" mode="content">
    <xsl:variable name="path">../widgets/<xsl:value-of select="@name" />/widget.xml</xsl:variable> <!-- The path to the widget xml -->
    <xsl:variable name="widget" select="document($path)"/>
    <xsl:element name="div">
        <xsl:attribute name="class">widget</xsl:attribute>
        <xsl:apply-templates select="$widget/widget/content/*" mode="content" /> <!-- Copy the widget contents. -->
    </xsl:element>
</xsl:template>

您必须衡量其性能是否更好。

您说您有一个处理的小部件元素,但您的代码没有显示这一点,因此很难理解您第一次真正处理这些元素的位置以及脚本元素的结束位置。这实际上与实现细节无关。我可以把你淹没在我完整的代码库中,但这与问题无关。你可以相信我,脚本元素,以及小部件HTML的其余部分,最终会在我的HTML中的body元素中达到几个层次。但是我会在问题中添加相关的小部件代码。谢谢!虽然没有我想要的那么整洁,但整体上要好得多,而且速度可能也快得多。非常感谢你!
<xsl:template match="widget" mode="content">
    <xsl:variable name="path">../widgets/<xsl:value-of select="@name" />/widget.xml</xsl:variable> <!-- The path to the widget xml -->
    <xsl:variable name="widget" select="document($path)"/>
    <xsl:element name="div">
        <xsl:attribute name="class">widget</xsl:attribute>
        <xsl:apply-templates select="$widget/widget/scripts" mode="content" /> <!-- Copy the widget script information. -->
        <xsl:apply-templates select="$widget/widget/content/*" mode="content" /> <!-- Copy the widget contents. -->
    </xsl:element>
</xsl:template>

<xsl:template match="node()" mode="content"> <!-- Copy anything that is not a widget. -->
    <xsl:copy>
        <xsl:copy-of select="@*" />
        <xsl:apply-templates select="./node()" mode="content" />
    </xsl:copy>
</xsl:template>
        <head>
            <title><xsl:value-of select="page/title" /></title>
            <xsl:apply-templates select="/page/scripts" mode="content" />
            <xsl:apply-templates select="/page/sheets" mode="content" />
        </head>
        <head>
            <title><xsl:value-of select="page/title" /></title>
            <xsl:apply-templates select="/page/scripts" mode="content" />
            <xsl:apply-templates select="//widget" mode="script"/>
            <xsl:apply-templates select="/page/sheets" mode="content" />
        </head>
<xsl:template match="widget" mode="script">
    <xsl:variable name="path">../widgets/<xsl:value-of select="@name" />/widget.xml</xsl:variable> <!-- The path to the widget xml -->
    <xsl:variable name="widget" select="document($path)"/>
    <xsl:apply-templates select="$widget/widget/scripts" mode="scripts" /> 
</xsl:template>
<xsl:template match="widget" mode="content">
    <xsl:variable name="path">../widgets/<xsl:value-of select="@name" />/widget.xml</xsl:variable> <!-- The path to the widget xml -->
    <xsl:variable name="widget" select="document($path)"/>
    <xsl:element name="div">
        <xsl:attribute name="class">widget</xsl:attribute>
        <xsl:apply-templates select="$widget/widget/content/*" mode="content" /> <!-- Copy the widget contents. -->
    </xsl:element>
</xsl:template>