如何选择性地输出部分缩进格式的元素?
How to output elements with partly indent format optionally?
例如html中的“div
”通常是缩进格式,而“p
”中的“span
”则不是。如果"span"是缩进格式,不同的“span
”中的字母在浏览器中会被额外的空格隔开。我们如何以缩进格式输出“div
”。和“span
”没有?元素名称是动态的,由程序代码获取,如:
<xsl:element name="{$var1}">
...
<xsl:element name="{$var2}">
...
<xsl:element name="{$var3}">...</xsl:element>
</xsl:element>
</xsl:element>
如何有选择地使每个元素以或不以缩进格式输出?
最小但完整的示例:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="3.0">
<xsl:output encoding="UTF-8" method="html"/>
<xsl:template match="/">
<html>
<head>
<meta charset="UTF-8"/>
</head>
<body>
<xsl:for-each select="(1 to 3)">
<xsl:element name="p">
<xsl:attribute name="style"/>
<xsl:text>para_</xsl:text><xsl:value-of select="."/>
<xsl:element name="span">
<xsl:attribute name="style"/>
num_<xsl:value-of select="."/>,
</xsl:element>
<xsl:element name="span">
<xsl:attribute name="style"/>
position_<xsl:value-of select="position()"/>,
</xsl:element>
</xsl:element>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
不需要源文件。
输出:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="UTF-8">
</head>
<body>
<p style="">para_1<span style="">
num_1,
</span><span style="">
position_1,
</span></p>
<p style="">para_2<span style="">
num_2,
</span><span style="">
position_2,
</span></p>
<p style="">para_3<span style="">
num_3,
</span><span style="">
position_3,
</span></p>
</body>
</html>
我想要的是:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="UTF-8">
</head>
<body>
<p style="">para_1<span style="">num_1,</span><span style="">position_1,</span></p>
<p style="">para_2<span style="">num_2,</span><span style="">position_2,</span></p>
<p style="">para_3<span style="">num_3,</span><span style="">position_3,</span></p>
</body>
</html>
至于你的例子,你需要小心,如果你想在混合 XSLT 元素(如 xsl:value-of
)和纯文本的元素内部控制白色 space,那么你可能需要确保根据需要将所有文字文本包装在 xsl:text
元素中,请参阅 https://xsltfiddle.liberty-development.net/94AbWBq 其中
<xsl:template match="/">
<html>
<head>
<meta charset="UTF-8"/>
</head>
<body>
<xsl:for-each select="(1 to 3)">
<xsl:element name="p">
<xsl:attribute name="style"/>
<xsl:text>para_</xsl:text>
<xsl:value-of select="."/>
<xsl:element name="span">
<xsl:attribute name="style"/>
<xsl:text>num_</xsl:text>
<xsl:value-of select="."/>
<xsl:text>,</xsl:text>
</xsl:element>
<xsl:element name="span">
<xsl:attribute name="style"/>
<xsl:text>position_</xsl:text>
<xsl:value-of select="position()"/>
<xsl:text>,</xsl:text>
</xsl:element>
</xsl:element>
</xsl:for-each>
</body>
</html>
</xsl:template>
您在输出中遇到但不想要的白色 space 通过编写
插入到您的 XSLT 代码中
<xsl:element name="span">
<xsl:attribute name="style"/>
num_<xsl:value-of select="."/>,
</xsl:element>
这样逗号 ,
后跟一个换行符。
一般来说,对于 XSLT 3,如果您使用 declare <xsl:output method="html" indent="yes"/>
,那么,根据 https://www.w3.org/TR/xslt-xquery-serialization-31/#HTML_INDENT,XSLT 处理器在序列化期间缩进时遵守以下规则:
7.4.3 HTML Output Method: the indent and suppress-indentation Parameters If the indent parameter has one of the values yes, true or 1, then the HTML output method MAY add or remove whitespace as it serializes the result tree, if it observes the following constraints:
Whitespace MUST NOT be added other than before or after an element, or
adjacent to an existing whitespace character.
Whitespace MUST NOT be added or removed adjacent to an inline element.
The inline elements are those included in the %inline category of any
of the HTML 4.01 DTDs or those elements defined to be phrasing
elements in HTML5, as well as the ins and del elements if they are
used as inline elements (i.e., if they do not contain element
children).
Whitespace MUST NOT be added or removed inside a formatted element,
the formatted elements being pre, script, style, title, and textarea.
Whitespace characters MUST NOT be added in the content of an element
whose expanded QName matches a member of the list of expanded QNames
in the value of the suppress-indentation parameter. The expanded QName
of an element node is considered to match a member of the list of
expanded QNames if:
the two expanded QNames are equal;
the expanded QNames both have null namespace URIs, and the local parts
of the two QNames are equal without regard to case; or
the value of the requested HTML version is 5.0, the local parts of the
two QNames are equal without regard to case and one QName has a null
namespace URI and the namespace URI of the other is equal to the XHTML
namespace URI.
这不依赖于使用文字结果元素或 xsl:element
来创建结果元素,正确的输出方法 html
以及这些结果元素的无名称 space,使 XSLT 处理器能够根据使用的 HTML 版本 https://www.w3.org/TR/xslt-xquery-serialization-31/#HTML_VERSION.
的规则进行序列化
例如html中的“div
”通常是缩进格式,而“p
”中的“span
”则不是。如果"span"是缩进格式,不同的“span
”中的字母在浏览器中会被额外的空格隔开。我们如何以缩进格式输出“div
”。和“span
”没有?元素名称是动态的,由程序代码获取,如:
<xsl:element name="{$var1}">
...
<xsl:element name="{$var2}">
...
<xsl:element name="{$var3}">...</xsl:element>
</xsl:element>
</xsl:element>
如何有选择地使每个元素以或不以缩进格式输出?
最小但完整的示例:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="3.0">
<xsl:output encoding="UTF-8" method="html"/>
<xsl:template match="/">
<html>
<head>
<meta charset="UTF-8"/>
</head>
<body>
<xsl:for-each select="(1 to 3)">
<xsl:element name="p">
<xsl:attribute name="style"/>
<xsl:text>para_</xsl:text><xsl:value-of select="."/>
<xsl:element name="span">
<xsl:attribute name="style"/>
num_<xsl:value-of select="."/>,
</xsl:element>
<xsl:element name="span">
<xsl:attribute name="style"/>
position_<xsl:value-of select="position()"/>,
</xsl:element>
</xsl:element>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
不需要源文件。
输出:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="UTF-8">
</head>
<body>
<p style="">para_1<span style="">
num_1,
</span><span style="">
position_1,
</span></p>
<p style="">para_2<span style="">
num_2,
</span><span style="">
position_2,
</span></p>
<p style="">para_3<span style="">
num_3,
</span><span style="">
position_3,
</span></p>
</body>
</html>
我想要的是:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="UTF-8">
</head>
<body>
<p style="">para_1<span style="">num_1,</span><span style="">position_1,</span></p>
<p style="">para_2<span style="">num_2,</span><span style="">position_2,</span></p>
<p style="">para_3<span style="">num_3,</span><span style="">position_3,</span></p>
</body>
</html>
至于你的例子,你需要小心,如果你想在混合 XSLT 元素(如 xsl:value-of
)和纯文本的元素内部控制白色 space,那么你可能需要确保根据需要将所有文字文本包装在 xsl:text
元素中,请参阅 https://xsltfiddle.liberty-development.net/94AbWBq 其中
<xsl:template match="/">
<html>
<head>
<meta charset="UTF-8"/>
</head>
<body>
<xsl:for-each select="(1 to 3)">
<xsl:element name="p">
<xsl:attribute name="style"/>
<xsl:text>para_</xsl:text>
<xsl:value-of select="."/>
<xsl:element name="span">
<xsl:attribute name="style"/>
<xsl:text>num_</xsl:text>
<xsl:value-of select="."/>
<xsl:text>,</xsl:text>
</xsl:element>
<xsl:element name="span">
<xsl:attribute name="style"/>
<xsl:text>position_</xsl:text>
<xsl:value-of select="position()"/>
<xsl:text>,</xsl:text>
</xsl:element>
</xsl:element>
</xsl:for-each>
</body>
</html>
</xsl:template>
您在输出中遇到但不想要的白色 space 通过编写
插入到您的 XSLT 代码中 <xsl:element name="span">
<xsl:attribute name="style"/>
num_<xsl:value-of select="."/>,
</xsl:element>
这样逗号 ,
后跟一个换行符。
一般来说,对于 XSLT 3,如果您使用 declare <xsl:output method="html" indent="yes"/>
,那么,根据 https://www.w3.org/TR/xslt-xquery-serialization-31/#HTML_INDENT,XSLT 处理器在序列化期间缩进时遵守以下规则:
7.4.3 HTML Output Method: the indent and suppress-indentation Parameters If the indent parameter has one of the values yes, true or 1, then the HTML output method MAY add or remove whitespace as it serializes the result tree, if it observes the following constraints:
Whitespace MUST NOT be added other than before or after an element, or adjacent to an existing whitespace character.
Whitespace MUST NOT be added or removed adjacent to an inline element. The inline elements are those included in the %inline category of any of the HTML 4.01 DTDs or those elements defined to be phrasing elements in HTML5, as well as the ins and del elements if they are used as inline elements (i.e., if they do not contain element children).
Whitespace MUST NOT be added or removed inside a formatted element, the formatted elements being pre, script, style, title, and textarea.
Whitespace characters MUST NOT be added in the content of an element whose expanded QName matches a member of the list of expanded QNames in the value of the suppress-indentation parameter. The expanded QName of an element node is considered to match a member of the list of expanded QNames if:
the two expanded QNames are equal;
the expanded QNames both have null namespace URIs, and the local parts of the two QNames are equal without regard to case; or
the value of the requested HTML version is 5.0, the local parts of the two QNames are equal without regard to case and one QName has a null namespace URI and the namespace URI of the other is equal to the XHTML namespace URI.
这不依赖于使用文字结果元素或 xsl:element
来创建结果元素,正确的输出方法 html
以及这些结果元素的无名称 space,使 XSLT 处理器能够根据使用的 HTML 版本 https://www.w3.org/TR/xslt-xquery-serialization-31/#HTML_VERSION.