英文:
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.
<xsl:element name="g">
<xsl:text>asdf</xsl:text>
</xsl:element>
got this:
some text
<g>asdf</g>some text
If changed tag name as "span", will get something like this:
some text<span>asdf</span>some text
here is a reproducible example:
the 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>
the 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>
the output( by 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>
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="no"
,或者将输出方法更改为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> 	</xsl:text>
<body>
<xsl:apply-templates/>
</body>
<xsl:text> </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="no"
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
<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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论