I would like to rename a processing instruction in xsl. My input looks like this:
<?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>
And I would like to rename the processing instruction from 'PI' to 'otherPI', as well as rename the attribute "author" to "user".
The result would look like this:
<?xml version="1.0" encoding="utf-8"?>
<material xml:lang="en-us">
<title>
<?otherPI_start user="joepublic" comment="Comment #1"?>Discovering
<?otherPI_end?>XML
</title>
<related-stuff>
<?otherPI_start user="johndoe" comment="Comment #3"?>
<a href="otherdoc.xml" />
<?otherPI_end?>
</related-stuff>
</material>
Can you help me identify the matching statement in the xsl that would do this?
<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:copy>
<!-- What should I put here??? -->
</xsl:copy>
</xsl:template>
<xsl:template match="processing-instruction('PI_end')">
<xsl:copy>
<!-- What should I put there??? -->
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
To rename a processing instruction, you would do:
<xsl:template match="processing-instruction('PI_start')">
<xsl:processing-instruction name="otherPI_start">
<xsl:value-of select="." />
</xsl:processing-instruction>
</xsl:template>
If you also want to modify the content, e.g. change author="joepublic"
to user="joepublic"
, you will have to do this using string manipulation - for example:
<xsl:template match="processing-instruction('PI_start')">
<xsl:processing-instruction name="otherPI_start">
<xsl:value-of select="substring-before(., 'author=')" />
<xsl:text>user=</xsl:text>
<xsl:value-of select="substring-after(., 'author=')" />
</xsl:processing-instruction>
</xsl:template>