(XSLT) 使用属性值对标签进行编号
(XSLT) Numbering tags with attribute values
所以我得到了这个 xhtml 文件 (https://xsltfiddle.liberty-development.net/bdxtqF):
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<p>first line</p>
<p>second line</p>
<p>third line</p>
<p>forth line</p>
<p>fifth line</p>
</body>
我想给 p 标签编号,但它们的值应被视为 id 属性。我知道你可以使用 xsl:number 但我只知道如何在节点内编号:
<?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"
xpath-default-namespace="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="#all"
version="3.0">
<xsl:template match="body">
<test>
<xsl:apply-templates />
</test>
</xsl:template>
<xsl:template match="p">
<p><xsl:number/>. <xsl:apply-templates /></p>
</xsl:template>
</xsl:stylesheet>
但我想要的结果应该是这样的
<?xml version="1.0" encoding="UTF-8"?>
<test>
<p id="1">first line</p>
<p id="2">second line</p>
<p id="3">third line</p>
<p id="4">forth line</p>
<p id="5">fith line</p>
</test>
如何在标签内创建属性名称并开始对其中的值进行编号?提前致谢!
你可以在这里使用xsl:attribute
创建属性
<xsl:template match="p">
<p>
<xsl:attribute name="id">
<xsl:number />
</xsl:attribute>
<xsl:apply-templates />
</p>
</xsl:template>
或者,如果您将 strip-space
添加到您的样式表,您可以使用 position()
<xsl:strip-space elements="*" />
<xsl:template match="body">
<test>
<xsl:apply-templates />
</test>
</xsl:template>
<xsl:template match="p">
<p id="{position()}">
<xsl:apply-templates />
</p>
</xsl:template>
如果没有 strip-space
,xsl:apply-templates
会 select 白色-space 文本节点,这会影响位置。请注意,如果除了 p
之外,body
下还有其他元素,这不会给您预期的结果。在这种情况下,您可以执行 <xsl:apply-templates select="p" />
,但假设您想忽略其他元素。
正如 Tim 已经向您展示的那样,您可以使用 xsl:attribute
构建属性节点并使用 xsl:number
.
填充其值
但是,在 XSLT 2 或 3 中,您还可以使用 Tim 展示的使用 position()
的属性值模板方法,而不是调用您自己的函数,然后使用 xsl:number
和 select
属性:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xpath-default-namespace="http://www.w3.org/1999/xhtml"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="#all"
version="2.0">
<xsl:function name="mf:number">
<xsl:param name="node" as="node()"/>
<xsl:number select="$node"/>
</xsl:function>
<xsl:template match="body">
<test>
<xsl:apply-templates />
</test>
</xsl:template>
<xsl:template match="p">
<p id="{mf:number(.)}">
<xsl:apply-templates/>
</p>
</xsl:template>
</xsl:stylesheet>
http://xsltransform.hikmatu.com/eiZQaEL is a working XSLT 2 example, https://xsltfiddle.liberty-development.net/bdxtqF/1 the same for XSLT 3 (https://www.w3.org/TR/xslt-30/#element-number).
所以我得到了这个 xhtml 文件 (https://xsltfiddle.liberty-development.net/bdxtqF):
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<p>first line</p>
<p>second line</p>
<p>third line</p>
<p>forth line</p>
<p>fifth line</p>
</body>
我想给 p 标签编号,但它们的值应被视为 id 属性。我知道你可以使用 xsl:number 但我只知道如何在节点内编号:
<?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"
xpath-default-namespace="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="#all"
version="3.0">
<xsl:template match="body">
<test>
<xsl:apply-templates />
</test>
</xsl:template>
<xsl:template match="p">
<p><xsl:number/>. <xsl:apply-templates /></p>
</xsl:template>
</xsl:stylesheet>
但我想要的结果应该是这样的
<?xml version="1.0" encoding="UTF-8"?>
<test>
<p id="1">first line</p>
<p id="2">second line</p>
<p id="3">third line</p>
<p id="4">forth line</p>
<p id="5">fith line</p>
</test>
如何在标签内创建属性名称并开始对其中的值进行编号?提前致谢!
你可以在这里使用xsl:attribute
创建属性
<xsl:template match="p">
<p>
<xsl:attribute name="id">
<xsl:number />
</xsl:attribute>
<xsl:apply-templates />
</p>
</xsl:template>
或者,如果您将 strip-space
添加到您的样式表,您可以使用 position()
<xsl:strip-space elements="*" />
<xsl:template match="body">
<test>
<xsl:apply-templates />
</test>
</xsl:template>
<xsl:template match="p">
<p id="{position()}">
<xsl:apply-templates />
</p>
</xsl:template>
如果没有 strip-space
,xsl:apply-templates
会 select 白色-space 文本节点,这会影响位置。请注意,如果除了 p
之外,body
下还有其他元素,这不会给您预期的结果。在这种情况下,您可以执行 <xsl:apply-templates select="p" />
,但假设您想忽略其他元素。
正如 Tim 已经向您展示的那样,您可以使用 xsl:attribute
构建属性节点并使用 xsl:number
.
但是,在 XSLT 2 或 3 中,您还可以使用 Tim 展示的使用 position()
的属性值模板方法,而不是调用您自己的函数,然后使用 xsl:number
和 select
属性:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xpath-default-namespace="http://www.w3.org/1999/xhtml"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="#all"
version="2.0">
<xsl:function name="mf:number">
<xsl:param name="node" as="node()"/>
<xsl:number select="$node"/>
</xsl:function>
<xsl:template match="body">
<test>
<xsl:apply-templates />
</test>
</xsl:template>
<xsl:template match="p">
<p id="{mf:number(.)}">
<xsl:apply-templates/>
</p>
</xsl:template>
</xsl:stylesheet>
http://xsltransform.hikmatu.com/eiZQaEL is a working XSLT 2 example, https://xsltfiddle.liberty-development.net/bdxtqF/1 the same for XSLT 3 (https://www.w3.org/TR/xslt-30/#element-number).