我想重命名一个处理指令,同时为它添加一个唯一的id属性。我的输入是这样的:给处理指令添加一个唯一的id(xsl)
?xml version="1.0" encoding="utf-8" ?>
<material xml:lang="en-us">
<title>
<?PI_start author="joepublic" comment="Comment #1" ?>Discovering
<?PI_end?>XML
</title>
<related-stuff>
<?PI_start author="johndoe" comment="Comment #3" ?>
<a href="otherdoc.xml" />
<?PI_end?>
</related-stuff>
</material>
结果是这样的:
<?xml version="1.0" encoding="utf-8"?>
<material xml:lang="en-us">
<title>
<?otherPI_start author="joepublic" comment="Comment #1" id ="1" ?>Discovering
<?otherPI_end id ="1" ?>XML
</title>
<related-stuff>
<?otherPI_start author="johndoe" comment="Comment #3" id ="2" ?>
<a href="otherdoc.xml" />
<?otherPI_end id ="2"?>
</related-stuff>
</material>
请注意,我有两个ID的产生,ID =” 1“表示文档中遇到的第一条指令,第二条表示id =”2“。 另请注意,ID在其他PI_end处理指令中重复。
你能帮我确定xsl中的匹配语句吗?
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="processing-instruction('PI_start')">
<xsl:processing-instruction name="otherPI_start">
author="<xsl:value-of select="substring-before(substring-after(.,'author="'),'"')"/>"
comment="<xsl:value-of select="substring-before(substring-after(.,'comment="'),'"')"/>"
id="<!-- What should I put here??? -->"
</xsl:processing-instruction>
</xsl:template>
<xsl:template match="processing-instruction('PI_end')">
<xsl:processing-instruction name="otherPI_end">
id="<!-- What should I put there??? -->"
</xsl:processing-instruction>
</xsl:template>
</xsl:stylesheet>
你为什么说“这不会计算当前的处理指令”?我不知道我以前是否编号过pis,但我不明白为什么'xsl:number'的行为与编号元素有所不同。当我用Saxon 6.5.5的' '测试你的代码片段时,数字以'1'开头。 –
随着http://xsltransform.net/再次下降,我是http://www.xslfiddle.net/,它开始在0编号。我已经重新措辞我的答案,使这一点更清晰。 –
嗯,从零开始似乎是Chrome和libxslt中的一个错误。 –