Re: [xsl] How to move processing instruction?

Subject: Re: [xsl] How to move processing instruction?
From: George Cristian Bina <george@xxxxxxxxxxxxx>
Date: Thu, 12 Apr 2007 10:39:20 +0300
Hi,

It is not clear what the logic of the transformation is so I will just assume that what you want is to move the processing instructions that are in the source before the root element inside the div element.
In that case you can start with the recursive copy template and add a couple of rules to avoid copying PIs that are children of the document node and to copy then inside the div element:


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>


  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/">
    <xsl:apply-templates select="*"/>
  </xsl:template>

  <xsl:template match="div">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates select="/processing-instruction()"/>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com


J. S. Rawat wrote:
Dear Expert(s):
Could anybody help me to get the following results:

INPUT:
<?page x?>
<?page 1?>
<pub-div type="body" id="C7967-10">
<div type="chapter" id="C7967-11">
<div-meta>
<lrh>MOVIES AND THE REAGAN PRESIDENCY: SUCCESS AND ETHICS</lrh>
<rrh>DEFINING REAGAN-ERA HOLLYWOOD</rrh>
</div-meta>
<label>Chapter 1</label>
<head>Defining Reagan-Era Hollywood</head>
<p>xxxx</p>
</div>
</pub-div>

OUTPUT:
<pub-div type="body" id="C7967-10">
<div type="chapter" id="C7967-11">
<?page x?>
<?page 1?>
<div-meta>
<lrh>MOVIES AND THE REAGAN PRESIDENCY: SUCCESS AND ETHICS</lrh>
<rrh>DEFINING REAGAN-ERA HOLLYWOOD</rrh>
</div-meta>
<label>Chapter 1</label>
<head>Defining Reagan-Era Hollywood</head>
<p>xxxx</p>
</div>
</pub-div>

XSL:
<xsl:template match="div">
<div>
<xsl:if test="preceding-sibling::processing-instruction('page')">
<xsl:apply-templates select="preceding-sibling::processing-instruction('page')[following-sibling::div[1][@id = current()/@id]]"/>
</xsl:if>
<xsl:apply-templates select="*[not(self::div-meta or self::label or self::head or self::subhead or self::author)]"/>
</xsl:template>


thanks in advance
...Joga S. Rawat

Current Thread