Pergunta

I have the following XML:

<content>
  <p>Para one</p>
  <p>Para two</p>
  <img src='pic.jpg' alt='pic'/>
</content>

In my XSLT I have

<xsl:processing-instruction name="php">
    $content = "<xsl:copy-of select="content/node()"/>";
</xsl:processing-instruction>

But it's outputting:

$content = "Para one
Para two";

I want it to output:

$content = "<p>Para one</p><p>Para two</p><img src='pic.jpg' alt=='pic'/>";

How do I do that?

Foi útil?

Solução 2

This script as well as this script gave me the result I desired:

<xsl:include href="nodetostring.xsl"/>
<xsl:template match="content">
    <xsl:param name="content">
        <xsl:apply-templates mode="nodetostring" select="node()"/>
    </xsl:param>
    <xsl:processing-instruction name="php">
        $content = '<xsl:copy-of select="$content"/>';
    </xsl:processing-instruction>
</xsl:template>

Outras dicas

Usually, copy-of="node()" retrieves the child nodes of an element. But in the case of processing instructions it seems that only text content is output.

This does not make sense to me, but the solution below is a workaround for this.

Stylesheet

<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>

 <xsl:template match="/content">
   <xsl:processing-instruction name="php">
       <xsl:text>$content = "</xsl:text>
       <xsl:apply-templates/>
       <xsl:text>";</xsl:text>
   </xsl:processing-instruction>
 </xsl:template>

 <xsl:template match="content/*">
    <xsl:text>&lt;</xsl:text><xsl:value-of select="name()"/><xsl:text>&gt;</xsl:text>
    <xsl:value-of select="."/>
    <xsl:text>&lt;/</xsl:text><xsl:value-of select="name()"/><xsl:text>&gt;</xsl:text>
 </xsl:template>

</xsl:stylesheet>

Output

<?php $content = "<p>Para one</p><p>Para two</p>";?>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top