xmlxsltprocessing-instruction

Add a unique id to a processing instruction (xsl)


I would like to rename a processing instruction, and at the same time add a unique id attribute to it. 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>

The result would look like this:

<?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>

Notice that I have two id's generated, id="1" for the first instruction encountered in the document, id="2" for the second. Also notice that the id is repeated in the otherPI_end processing instruction.

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:processing-instruction name="otherPI_start">
      author="<xsl:value-of select="substring-before(substring-after(.,'author=&quot;'),'&quot;')"/>"
      comment="<xsl:value-of select="substring-before(substring-after(.,'comment=&quot;'),'&quot;')"/>"
      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>


Solution

  • id="<!-- What should I put here??? -->"

    <xsl:value-of select="generate-id()"/>
    

    If you want the id of <?PI_end?> to match that of the preceding <?PI_start ?>, then use:

    <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>