Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.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
Html 如何在一个xml节点上应用多个xsl模板_Html_Xml_Xslt - Fatal编程技术网

Html 如何在一个xml节点上应用多个xsl模板

Html 如何在一个xml节点上应用多个xsl模板,html,xml,xslt,Html,Xml,Xslt,我的问题是: 我有带有CSS样式的HTML文档。我需要将这些样式移动到主体中的元素中,并将它们从样式标记中移除 我制作了一个帮助器,它为每个css样式和值生成xpath,这些样式和值需要放在那里。然后生成XSL文档并将其应用于HTML 问题是,有时需要将多个xsl模板应用于同一节点(相同匹配)。只有第一个被应用,其他的被忽略 我怎样才能将它们全部应用 以下是HTML的一个示例: <html lang="en"> <head> <META HTTP-EQUIV=

我的问题是:

我有带有CSS样式的HTML文档。我需要将这些样式移动到主体中的元素中,并将它们从样式标记中移除

我制作了一个帮助器,它为每个css样式和值生成xpath,这些样式和值需要放在那里。然后生成XSL文档并将其应用于HTML

问题是,有时需要将多个xsl模板应用于同一节点(相同匹配)。只有第一个被应用,其他的被忽略

我怎样才能将它们全部应用

以下是HTML的一个示例:

<html lang="en">
<head>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=utf-8"/>
   <style type="text/css">
      .right-aligned { text-align: right !important; }
      .full-width { width: 100% !important; display: inline-block; }
   </style>
</head>
<body>
   <table class="header">
      <tr>
         <td class="icon">
            <img alt="Minor" src="Picture_SRC" />
         </td>
            <td>
               <div class="secondary full-width right-aligned">Blah: Test</div>
            </td>
         </tr>
      </table>
   </body>
</html>

.右对齐{文本对齐:右!重要;}
.full width{width:100%!重要;显示:内联块;}
废话:测试
对于此HTML,需要进行XSL转换:

<xsl:template match="//*[contains(@class,'right-aligned') and @style]">
   <xsl:attribute name="style">
      <xsl:value-of select="concat(., 'text-align: right !important;')"/>
   </xsl:attribute>
</xsl:template>
<xsl:template match="//*[contains(@class,'right-aligned') and not(@style)]">
   <xsl:copy>
      <xsl:attribute name="style">text-align: right !important;</xsl:attribute>
      <xsl:apply-templates select="@*| node()"/>
   </xsl:copy>
</xsl:template>
<xsl:template match="//*[contains(@class,'full-width') and @style]">
   <xsl:attribute name="style">
      <xsl:value-of select="concat(., 'width: 100% !important; display: inline-block;')"/>
    </xsl:attribute>
</xsl:template>
<xsl:template match="//*[contains(@class,'full-width') and not(@style)]">
   <xsl:copy>
      <xsl:attribute name="style">width: 100% !important; display: inline-block;</xsl:attribute>
      <xsl:apply-templates select="@*| node()"/>
   </xsl:copy>
</xsl:template>

文本对齐:对!重要的;
宽度:100%!重要的;显示:内联块;

重要的是:HTML不是静态的,它会改变。。。因此,我无法创建静态XSL。

这里有一种方法,您可以使用
模式
属性一个接一个地应用每个模板

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="@class">
    <xsl:attribute name="style">
      <xsl:if test="@style">
        <xsl:value-of select="@style" />
        <xsl:text>;</xsl:text>
      </xsl:if>
      <xsl:apply-templates select="." mode="right-aligned" />
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="@style" />

  <xsl:template match="@class" mode="right-aligned">
    <xsl:if test="contains(.,'right-aligned')">
      <xsl:text>text-align: right !important;</xsl:text>
    </xsl:if>
    <xsl:apply-templates select="." mode="full-width" />
  </xsl:template>

  <xsl:template match="@class" mode="full-width">
    <xsl:if test="contains(.,'full-width')">
      <xsl:text>width: 100% !important; display: inline-block;</xsl:text>
    </xsl:if>
  </xsl:template>  
</xsl:stylesheet>

顺便说一句,如果你想在一个标签上使用多个模板,我发现这是不可能的。因为所有模板都应用于您拥有的源(XML),而不是第一个模板的输出。所以我找到的解决方案是:在多个文件中创建多个模板,然后在文档中逐个运行它们。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="@class" priority="10">
    <xsl:attribute name="style">
      <xsl:if test="@style">
        <xsl:value-of select="@style" />
        <xsl:text>;</xsl:text>
      </xsl:if>
      <xsl:next-match />
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="@style" />

  <xsl:template match="@class[contains(.,'right-aligned')]" priority="9">
    <xsl:text>text-align: right !important;</xsl:text>
    <xsl:next-match />
  </xsl:template>

  <xsl:template match="@class[contains(.,'full-width')]" priority="8">
    <xsl:text>width: 100% !important; display: inline-block;</xsl:text>
    <xsl:next-match />
   </xsl:template>  

  <xsl:template match="@class" /> <!-- Stop the built-in template applying -->
</xsl:stylesheet>