処理命令の名前を変更し、同時に一意の 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>
2 つの ID が生成されていることに注意してください。ドキュメントで最初に検出された命令は id="1" で、2 番目の命令は id="2" です。また、id が otherPI_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>