XSLT:如何创建内联元素?类似于HTML中的

huangapple go评论53阅读模式
英文:

XSLT: how to create an inline element? like span in html

问题

通过使用XSLT,我想要创建一个名为"g"的标签,将其作为内联元素。但总是得到一个块级元素。

<xsl:element name="g">
    <xsl:text>asdf</xsl:text>
</xsl:element>

得到这个:

some text
<g>asdf</g>some text

如果将标签名称更改为"span",将会得到类似于以下内容:

some text<span>asdf</span>some text

这是一个可复制的示例:
XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:n="http://www.tei-c.org/ns/1.0"
                exclude-result-prefixes="n"
>
    <xsl:output encoding="UTF-8" method="html"/>
    <xsl:template match="/n:TEI/n:text/n:body">
        <html>
            <body>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="n:g">
        <xsl:element name="g">
            <xsl:attribute name="type">
                <xsl:text>gaiji</xsl:text>
            </xsl:attribute>
            <xsl:apply-templates/>
        </xsl:element>
        <xsl:element name="span">
            <xsl:attribute name="class">
                <xsl:text>gaiji</xsl:text>
            </xsl:attribute>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="text()">
        <xsl:value-of select="normalize-space(.)"/>
    </xsl:template>
</xsl:stylesheet>

XML:

<?xml version="1.0" encoding="UTF-8"?>
<TEI xmlns="http://www.tei-c.org/ns/1.0">
    <text>
        <body>
            some text<g>char</g>some text
        </body>
    </text>
</TEI>

输出(通过saxonb-xslt,Saxon 9.1.0.8J生成):

<html>
   <body>some text
      <g type="gaiji">char</g><span class="gaiji">char</span>some text
   </body>
</html>

区别在于前一种情况(标签"g")会生成一些无用的空格(围绕新标签),最终会显示在网页上。

是否有办法生成标签"g",使其像"span"一样成为内联元素?

我尝试在网上搜索,但没有看到类似的问题。

英文:

by use XSLT, I want to create a tag named "g" as an inline element. but always got a block element.

&lt;xsl:element name=&quot;g&quot;&gt;
    &lt;xsl:text&gt;asdf&lt;/xsl:text&gt;
&lt;/xsl:element&gt;

got this:

some text
&lt;g&gt;asdf&lt;/g&gt;some text

If changed tag name as "span", will get something like this:

some text&lt;span&gt;asdf&lt;/span&gt;some text

here is a reproducible example:
the xsl:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;xsl:stylesheet version=&quot;2.0&quot;
                xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
                xmlns:n=&quot;http://www.tei-c.org/ns/1.0&quot;
                exclude-result-prefixes=&quot;n&quot;
&gt;
    &lt;xsl:output encoding=&quot;UTF-8&quot; method=&quot;html&quot;/&gt;
    &lt;xsl:template match=&quot;/n:TEI/n:text/n:body&quot;&gt;
        &lt;html&gt;
            &lt;body&gt;
                &lt;xsl:apply-templates/&gt;
            &lt;/body&gt;
        &lt;/html&gt;
    &lt;/xsl:template&gt;
    &lt;xsl:template match=&quot;n:g&quot;&gt;
        &lt;xsl:element name=&quot;g&quot;&gt;
            &lt;xsl:attribute name=&quot;type&quot;&gt;
                &lt;xsl:text&gt;gaiji&lt;/xsl:text&gt;
            &lt;/xsl:attribute&gt;
            &lt;xsl:apply-templates/&gt;
        &lt;/xsl:element&gt;
        &lt;xsl:element name=&quot;span&quot;&gt;
            &lt;xsl:attribute name=&quot;class&quot;&gt;
                &lt;xsl:text&gt;gaiji&lt;/xsl:text&gt;
            &lt;/xsl:attribute&gt;
            &lt;xsl:apply-templates/&gt;
        &lt;/xsl:element&gt;
    &lt;/xsl:template&gt;
    &lt;xsl:template match=&quot;text()&quot;&gt;
        &lt;xsl:value-of select=&quot;normalize-space(.)&quot;/&gt;
    &lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;

the xml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;TEI xmlns=&quot;http://www.tei-c.org/ns/1.0&quot;&gt;
    &lt;text&gt;
        &lt;body&gt;
            some text&lt;g&gt;char&lt;/g&gt;some text
        &lt;/body&gt;
    &lt;/text&gt;
&lt;/TEI&gt;

the output( by saxonb-xslt,Saxon 9.1.0.8J ):

&lt;html&gt;
   &lt;body&gt;some text
      &lt;g type=&quot;gaiji&quot;&gt;char&lt;/g&gt;&lt;span class=&quot;gaiji&quot;&gt;char&lt;/span&gt;some text
   &lt;/body&gt;
&lt;/html&gt;

The difference is that the previous case (tag "g") will generate some useless spaces. (around the new tag) and will eventually display on the web page.

Is there any way to generate tag "g" as inline element like "span"?

I've tried searching the web and haven't seen a similar problem.

答案1

得分: 1

区别在于结果序列化的方式不同。序列化程序知道span是内联元素,因此会抑制添加空格。

这表明您正在使用HTML序列化方法来序列化实际上不是HTML的内容。也许您需要切换到XML序列化方法。请检查您的xsl:output声明。

另外,在XSLT问题中,您应该始终为问题添加特定的XSLT版本标签,因为它们之间有很大的区别。

英文:

The difference is in the way the result is being serialized. The serializer knows that span is an inline element, so it suppresses addition of whitespace.

This suggests that you are using the HTML serialization method to serialize something that isn't actually HTML. Perhaps you need to switch to the XML serialization method. Check your xsl:output declarations.

Also, with XSLT questions you should always tag the question with a specific XSLT version, since they are very different.

答案2

得分: 1

您在g元素之前看到的空白是序列化器执行的缩进结果。

您可以通过将xsl:output声明中添加indent=&quot;no&quot;,或者将输出方法更改为xml(这会将indent属性的默认值设置为no)来全局消除所有这样的空白。我不知道有一种方法可以仅在某些元素上局部关闭它。

但是,您可以通过向输出添加一些自己的空白来改善结果的可读性。以下是一个示例(还简化了您现有的代码):

XSLT 2.0

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xpath-default-namespace="http://www.tei-c.org/ns/1.0">
<xsl:output encoding="UTF-8" method="html" indent="no"/>

<xsl:template match="/TEI">
    <html>
        <xsl:text>&#10;&#9;</xsl:text>
        <body>
            <xsl:apply-templates/>
        </body>
        <xsl:text>&#10;</xsl:text>
    </html>
</xsl:template>

<xsl:template match="g">
    <g type="gaiji">
        <xsl:apply-templates/>
    </g>
    <span class="gaiji">
        <xsl:apply-templates/>
    </span>
</xsl:template>

<xsl:template match="text()">
    <xsl:value-of select="normalize-space(.)"/>
</xsl:template>

</xsl:stylesheet>

Result

<!DOCTYPE HTML><html>
    <body>some text<g type="gaiji">char</g><span class="gaiji">char</span>some text</body>
</html>
英文:

The whitespace you see before the g element is a result of indentation performed by the serializer.

You can eliminate all such whitespace globally by adding indent=&quot;no&quot; to the xsl:output declaration, or by changing the output method to xml (which makes no the default value for the indent attribute). I don't know of a method to turn it off locally for some elements only.

However, you can improve the result's readability by adding some of your own whitespace to the output. Here is an example (which also simplifies your existing code):

XSLT 2.0

&lt;xsl:stylesheet version=&quot;2.0&quot;
xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
xpath-default-namespace=&quot;http://www.tei-c.org/ns/1.0&quot;&gt;
&lt;xsl:output encoding=&quot;UTF-8&quot; method=&quot;html&quot; indent=&quot;no&quot;/&gt;

&lt;xsl:template match=&quot;/TEI&quot;&gt;
	&lt;html&gt;
		&lt;xsl:text&gt;&amp;#10;&amp;#9;&lt;/xsl:text&gt;
    	&lt;body&gt;
    		&lt;xsl:apply-templates/&gt;
    	&lt;/body&gt;
		&lt;xsl:text&gt;&amp;#10;&lt;/xsl:text&gt;
	&lt;/html&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match=&quot;g&quot;&gt;
    &lt;g type=&quot;gaiji&quot;&gt;
        &lt;xsl:apply-templates/&gt;
    &lt;/g&gt;
    &lt;span class=&quot;gaiji&quot;&gt;
        &lt;xsl:apply-templates/&gt;
    &lt;/span&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match=&quot;text()&quot;&gt;
    &lt;xsl:value-of select=&quot;normalize-space(.)&quot;/&gt;
&lt;/xsl:template&gt;

&lt;/xsl:stylesheet&gt;

Result

&lt;!DOCTYPE HTML&gt;&lt;html&gt;
    &lt;body&gt;some text&lt;g type=&quot;gaiji&quot;&gt;char&lt;/g&gt;&lt;span class=&quot;gaiji&quot;&gt;char&lt;/span&gt;some text&lt;/body&gt;
&lt;/html&gt;

huangapple
  • 本文由 发表于 2023年6月27日 18:28:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76563937.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定