简体   繁体   English

在xslt转换过程中自动关闭xml标签

[英]Autoclose xml tag during xslt transformation

I have : 我有 :

<input type="checkbox" name="idsProduct" value="{@id}" 
       id="form_checkbox_product_{@id}">
    <xsl:if test="$x=$y">
        <xsl:attribute name="checked" >checked</xsl:attribute>
    </xsl:if>
</input>

and I get : 我得到:

  <input type="checkbox" name="idsProduct" value="26294"
         id="form_checkbox_product_26294" checked="checked"></input>

I want an input tag like : 我想要一个输入标签,例如:

<input type="checkbox" name="idsProduct" value="26294" 
       id="form_checkbox_product_26294" checked="checked" />

my xsl output is : 我的xsl输出是:

 <xsl:output 
      omit-xml-declaration="yes" 
      method="xml"
      encoding="utf-8"
      indent="no" />

How can I autoclose this tag? 如何自动关闭此标签?

This is similar to this question (although your problem is the direct inverse): 这类似于以下问题(尽管您的问题是直接逆):

Using xsl:if doesn't include closing tag 使用xsl:if不包含结束标记

There's discussion of a 'trick' here that causes the longer form of a closed element to be used, which you appear to be inadvertently using here, in a slightly different form. 这里讨论的是一个“技巧”,该技巧将导致使用较长形式的封闭元素,而您似乎在这里无意中使用了一种稍有不同的形式。 I suspect your problem is because you're asking the xslt to output directly to text. 我怀疑您的问题是因为您要求xslt直接输出到文本。 Outputting to an xml document first, and then serializing that should solve your problem. 首先输出到xml文档,然后进行序列化可以解决您的问题。

Here's an extension method I used for transforming to an XmlDocument rather than a string, which you can then simply read the .OuterXml property of if you want the string equivalent; 这是我用于转换为XmlDocument而不是字符串的扩展方法,然后,如果需要等效的字符串,则可以简单地读取.OuterXml属性。 because XSLT isn't doing the outputting to text, it should treat the closed tags correctly. 由于XSLT不会输出文本,因此应正确处理关闭的标签。

    public static XmlDocument Transform(this XmlDocument input, XslCompiledTransform xslt)
    {
        XmlDocument outDoc = new XmlDocument(input.CreateNavigator().NameTable);
        using (XmlWriter xr = outDoc.CreateNavigator().AppendChild())
        {
            xslt.Transform(input, xr);
        }

        return outDoc;
    }

Try removing all whitespace from between the tags: 尝试删除标签之间的所有空格:

<input type="checkbox" name="idsProduct" value="{@id}" id="form_checkbox_product_{@id}"><xsl:if test="$x=$y"><xsl:attribute name="checked" >checked</xsl:attribute></xsl:if></input>

Does that work? 那样有用吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM