使用 xslt 与<amp-img>



我的网站将内容保存在 xml 文件中,并使用 xsl 样式表显示它,因此客户可以轻松编辑内容,但是如果我使用 amp-img 标记,即使我在样式表中使用了结束标记,xsl 转换也会自行关闭标记。然后,当我使用 w3c html 验证器测试页面时,它会给出错误,指出以下标签未关闭。下面是错误的示例:

用于

非 VOID HTML 元素的自闭合语法 (/>(。忽略斜杠并被视为开始标记。

错误:看到结束标记div,但有打开的元素。

<amp-img width="30" height="30" src="/images/camera.svg" />

谁能建议我如何处理这个问题?

以下是样式表的一部分:

<xsl:template match="/">
<xsl:apply-templates select="//localItem" />
</xsl:template>
<xsl:template match="localItem">
<div itemscope="itemscope" itemtype="http://schema.org/BreadcrumbList">
<div class="breadCrumb" itemprop="itemListElement" itemscope="itemscope" itemtype="http://schema.org/ListItem">
   <a itemprop="item" href="localSights.aspx">
     <h2 class="traffic" itemprop="name">
       <xsl:value-of select="heading" />
     </h2>
     <meta itemprop="position">
       <xsl:attribute name="content">
         <xsl:value-of select="@position"/>
       </xsl:attribute>
     </meta>
     <div class="breadCrumbIcon">
       <amp-img>
     <xsl:attribute name="width">
       <xsl:value-of select="image/@width"/>
     </xsl:attribute>
      <xsl:attribute name="height">
       <xsl:value-of select="image/@height"/>
     </xsl:attribute>
        <xsl:attribute name="src">
       <xsl:value-of select="image"/>
     </xsl:attribute>
     </amp-img>
     </div>
     <div class="description">
           <xsl:value-of select="paragraph"/>
     </div>
   </a>
 </div>
 </div>
</xsl:template>

下面是 xml 文件中的一个元素:

  <page name="nearby">
    <localItem position="1">
      <heading>Local Sights</heading>
      <paragraph>
        Lincoln is a historic city...Click here to see some of the many sights
      </paragraph>
      <image alt="drawing of camera" width="30" height="30">/images/camera.svg</image>
    </localItem>
</page>

以下是 amp 页面中的 vb.net 代码:

        <%
        Dim root As XmlElement
        root = Cache("content").documentElement
        Dim styleFile As String = (EdenHouseContent.webRoot & "nearbydefault.xslt")
        Response.Write(httpFunctions.transformXML(root.OuterXml, styleFile))
    %>

这是上面调用的已使用定义的函数:

    Public Shared Function transformXML(xmlNode As Object, styleSheet As Object) As String
    Dim reader As System.Xml.XmlReader = System.Xml.XmlReader.Create(New IO.StringReader(xmlNode.ToString()))
    reader.MoveToContent()
    'Load the style sheet. 
    Dim xslt As System.Xml.Xsl.XslCompiledTransform = New System.Xml.Xsl.XslCompiledTransform()
    'xslt.OutputSettings.ConformanceLevel = System.Xml.ConformanceLevel.Fragment
    xslt.Load(styleSheet)
    ' Transform the node fragment
    Dim settings As System.Xml.XmlWriterSettings = New System.Xml.XmlWriterSettings
    settings.OmitXmlDeclaration = True
    settings.Indent = True
    settings.ConformanceLevel = System.Xml.ConformanceLevel.Auto
    settings.CloseOutput = False
    Dim sw As New System.IO.StringWriter()
    Dim writer As Object = System.Xml.XmlWriter.Create(sw, settings)
    xslt.Transform(reader, writer)
    Return (sw.ToString())
End Function

这是样式表的顶部:

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">

如果要生成 HTML 解析器将接受的输出,请使用 <xsl:output method="html"/>

最新更新