xsl方法中的自闭标签:xml

[英]Self closing tags in xsl method: xml


I am working with a site that uses "xsl method:xml" to create html templates. However, I am running into an issue with the tag self-closing when the html page is rendered by the xsl engine.

我正在使用一个使用“xsl method:xml”的网站来创建html模板。但是,当xsl引擎呈现html页面时,我遇到了标签自动关闭的问题。

<div></div> transforms to => <div/>

转换为=>

This problem is compounded by the fact that the method needs to stay xml, or the other components of the page will not render correctly.

由于该方法需要保留xml,或者页面的其他组件无法正确呈现,因此这个问题更加复杂。

Any ideas on how tell xsl to make a special exception for the node <div>?

有关如何告诉xsl为节点

做出特殊异常的任何想法?

This question is similar to this question, except I want to keep the method:xml. XSLT self-closing tags issue

这个问题类似于这个问题,除了我想保留方法:xml。 XSLT自关闭标签问题

2 个解决方案

#1


7  

It is not available by default with method=xml. You can handle it in a couple of ways:

默认情况下,它不能与method = xml一起使用。您可以通过以下几种方式处理它:

Option 1 - switch to method=xhtml

选项1 - 切换到method = xhtml

If you can't switch to method=xml, and you are using XSLT 2.0 parser, maybe you can try method=xhtml?

如果你不能切换到method = xml,并且你正在使用XSLT 2.0解析器,也许你可以尝试method = xhtml?

<xsl:output method="xhtml" indent="yes" />

This will make your closing tags to be rendered.

这将使您的结束标记呈现。

Option 2 - add empty space to 'div' tag

选项2 - 向'div'标签添加空格

Alternatively just add <xsl:text> </xsl:text> (with one space in between tags) to make your <div> not empty (of course if space there is okay with you).

或者只需添加 (标签之间有一个空格),使你的

不为空(当然,如果空间没关系)。

Consider following XML:

考虑遵循XML:

<div></div>

When transformed with:

转换为:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <!-- output is xml -->
  <xsl:output method="xml" indent="yes" />

  <xsl:template match="div">
    <div>
      <!-- note space here -->
      <xsl:text> </xsl:text>
      <xsl:value-of select="text()" />
    </div>
  </xsl:template>
</xsl:stylesheet>

It produces output:

它产生输出:

<?xml version="1.0" encoding="UTF-8"?>
<div> </div>

#2


1  

I have had the same issue. The problem is the XmlTextWriter which only messes up the html. Try the following code:

我遇到了同样的问题。问题是XmlTextWriter只会搞砸html。请尝试以下代码:

public static string Transform(string xmlPath, string xslPath, XsltArgumentList xsltArgumentList)
    {
        string rc = null;
        using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
        {
            XPathDocument myXPathDoc = new XPathDocument(xmlPath);
            XslCompiledTransform myXslTrans = new XslCompiledTransform();
            myXslTrans.Load(xslPath, new XsltSettings(true, true), null);
            myXslTrans.Transform(myXPathDoc, xsltArgumentList, ms);
            ms.Position = 0;
            using (System.IO.TextReader reader = new System.IO.StreamReader(ms, Encoding.UTF8))
            {
                rc = reader.ReadToEnd();
            }
        }
        return rc;
    }

I use the XsltArgumentList to pass information to the xslt. If you do not need to pass arguments to your xslt, you may call the method like this:

我使用XsltArgumentList将信息传递给xslt。如果您不需要将参数传递给xslt,则可以调用以下方法:

string myHtml = Transform(myXmlPath, myXslPath, new XsltArgumentList());
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2013/03/08/6f44781db5ba0b89463b22f0f329ec2f.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告