Re: [xsl] How to move processing instruction?

Subject: Re: [xsl] How to move processing instruction?
From: "J. S. Rawat" <jrawat@xxxxxxxxxxxxxx>
Date: Mon, 16 Apr 2007 11:46:39 +0530
Hi
I have done all the hit and try and stuck once again!!! Please help how to move processing instruction which are sibling of <pub-div>>


XSL
<xsl:template match="processing-instruction('page')" mode="move">
<xsl:processing-instruction name="page"><xsl:value-of select="."/></xsl:processing-instruction>
</xsl:template>


<xsl:template match="div">
<div>
<xsl:attribute name="type">
<xsl:choose>
<xsl:when test="@type='part'">part</xsl:when>
<xsl:otherwise>chapter</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:if test="preceding-sibling::processing-instruction('page')">
<xsl:apply-templates select="preceding-sibling::processing-instruction('page')[following-sibling::div[1][@id = current()/@id]]" mode="move" />
</xsl:if>
</div>
</xsl:template>



INPUT <publication> <?page 1?> <?page 2?> <pub-div type="body" id="C1"> <div type="chapter" id="C2"> ... </div> <?page x?> <?page 1?> <div type="chapter" id="C3"> ... </div> </pub-div> </publication>

OUTPUT
<body>
<div type="chapter"/>
<div type="chapter">
<?page 3?>
<?page 4?>
</div>
</body>

REQUIRED OUTPUT
<body>
<div type="chapter">
<?page x?>
<?page 1?>
</div>
<div type="chapter">
<?page 3?>
<?page 4?>
</div>
</body>

thanks
JSR


Great!!! George thank a lot!!!



At 12:00 PM 4/12/2007 +0300, you wrote:
Then change the templates as below:
* match processing instructions that have the first following sibling element that contains div and do not copy them
* match div and copy the processing instructions that are preceding siblings of its parent 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="processing-instruction()[following-sibling::*[1]/div]"/>

  <xsl:template match="div">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:copy-of select="../preceding-sibling::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 George you understand right but I need to move the processing instruction (<? page x?><? page x?>) which are just before of parent of "div".
In other words we can say that: How to copy the sibling (processing instruction) of parent of "div"
thanks
...JSR


At 10:39 AM 4/12/2007 +0300, you wrote:
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