我想重命名一个处理指令,同时为它添加一个唯一的id属性。我的输入如下:
?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>
答案 0 :(得分:1)
您可以在此使用xsl:number
<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="<xsl:number count="processing-instruction('PI_start')" level="any" />"
</xsl:processing-instruction>
</xsl:template>
如果你发现这不计算当前的处理指令(这意味着编号从零开始),那就试试吧....
<xsl:number count="/|processing-instruction('PI_start')" level="any" />
答案 1 :(得分:1)
id="<!-- What should I put here??? -->"
<xsl:value-of select="generate-id()"/>
如果您希望<?PI_end?>
的ID与前一个<?PI_start ?>
的ID匹配,请使用:
<xsl:value-of select="generate-id(preceding-sibling::processing-instruction('PI_start')[1])"/>
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<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">
<xsl:value-of select="."/>
<xsl:text>id="</xsl:text>
<xsl:value-of select="generate-id()"/>
<xsl:text>"</xsl:text>
</xsl:processing-instruction>
</xsl:template>
<xsl:template match="processing-instruction('PI_end')">
<xsl:processing-instruction name="otherPI_end">
<xsl:text>id="</xsl:text>
<xsl:value-of select="generate-id(preceding-sibling::processing-instruction('PI_start')[1])"/>
<xsl:text>"</xsl:text>
</xsl:processing-instruction>
</xsl:template>
</xsl:stylesheet>