开发者

Replacing br tags with p tags issue

I have this input xml

<body>

<p class="heading">
<span><tag>This text</tag>
<br/>
</span>
</p>

<p class="bodyText">
<span><tag>is</tag>
<br/>
</span>
<span><tag>meant</tag>
</span>
<span><tag>to</tag>
<br/>
</span>
</p>

<p class="bodyText">
<span>
<tag>be</tag></span>
<span>
<tag>read</tag>
<br/>
</span>
<span><tag>with</tag></span>
<span><tag>some</tag></span>
<span><tag>inline</tag><br/></span>
<span class="italic">
<tag>styles</tag>
<br/>
<tag>the</tag>
<br/>
<tag>end</tag>
</span>
</p>

</body>

I wish to get this output - replacing the br tags with p tags and inheriting all/any classes

<body>

<p class="heading">
<span>
<tag>This text</tag>
</span>
</p>

<p class="bodyText">
<span>
<tag>is</tag>
</span>
</p>

<p class="bodyText">
<span>
<tag>meant</tag>
</span>
<span>
<tag>to</tag>
</span>
</p>

<p class="bodyText">
<span>
<tag>be</tag>
</span>
<span>
<tag>read</tag>
</span>
</p>

<p class="bodyText">
<span>
<tag>with</tag>
</span>
<span>
<tag>some</tag>
</span>
<span>
<tag>inline</tag>
</span>
</p>

<p class="bodyText">
<span class="italic">
<tag>styles</tag>
</span>
</p>

<p class="bodyText">
<span class="italic">
<tag>the</tag>
</span>
</p>

<p class="bodyText">
<span class="italic">
<tag>end</tag>
</span>
</p>

</body>

XSLT: A couple of excellent suggestions so far (albeit with a simpler XML input) have been;

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

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

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

<xsl:template match="p">
<xsl:apply-templates select="span[1]" mode="group"/>
</xsl:template>

<xsl:template match="span[not(br)]" mode="group">
    <p>
<xsl:apply-templates select="."/>
</p>
<xsl:apply-templates select="following-sibling::span[br][1]/following-  sibling::span[1]" mode="group"/>
</xsl:template>

<xsl:template match="span[br]" mode="group">
<p>
<xsl:apply-templates select="."/>
</p>
<xsl:apply-templates select="following-sibling::span[1]" mode="group"/>
</xsl:template>


<xsl:template match="span[not(br)]">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
<xsl:apply-templates select="following-sibling::span[1]"/>
</xsl:template>

<xsl:template match="span/br"/>

</xsl:stylesheet>

Which works up and until the last set of br's in the last span tag of the input. I also have this excellent suggestion which does the same but using a key;

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>

<xsl:key name="para" match="span" use="generate-id(following-sibling::br[1])" />

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

<xsl:template match="p">
<xsl:apply-templates select="br" />
<xsl:if test="span[not(following-sibling::br)]">
<p>
<xsl:apply-templates select="span[not(following-sibling::br)]" />
</p>
</xsl:if>
</xsl:template>

<xsl:template match="br">
<p>
<xsl:apply-templates select="key('para', generate-id())" />
</p>
</xsl:template>

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

</xsl:stylesheet>

...and still the same issue. I would be very grateful if some one could point out what I need to do with either piece of XSLT. Sorry if this seems like a repost but I'm r开发者_如何学Goeally stuck.

many thanks


Similar to a previous suggestion, it knows needs to match on br elements that are within span elements.

<xsl:key name="para" match="span[not(br)]" 
  use="generate-id(following-sibling::span[br])"/>

So, this will group together all spans without a br

But there also needs to be a lookup for the case where there are multiple br elements within a span tag.

<xsl:key name="br" match="tag" 
  use="generate-id(preceding-sibling::br[1])"/>

Here is the full XSLT...

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

   <xsl:output method="xml" indent="yes"/>

   <xsl:key name="para" match="span[not(br)]" use="generate-id(following-sibling::span[br])"/>
   <xsl:key name="br" match="tag" use="generate-id(preceding-sibling::br[1])"/>

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

   <!-- Start by matching p tags -->
   <xsl:template match="p">
      <xsl:apply-templates select="span[br]"/>
   </xsl:template>

   <!-- Match the span tags with br tags present -->
   <xsl:template match="span[br]">
      <p>
         <!-- Get attributes for p tag -->
         <xsl:apply-templates select="../@*" />

         <!-- Copy all prior span tags without br elements -->
         <xsl:apply-templates select="key('para', generate-id())"/>

         <!-- Copy the elements for this span up to any br tag -->
         <xsl:copy>
            <xsl:apply-templates select="@*|node()[not(preceding-sibling::br)]"/>
         </xsl:copy>
      </p>

      <!-- Special case for span tags containing multiple br tags -->
      <xsl:apply-templates select="br[following-sibling::*]" mode="special" />
   </xsl:template>

   <!-- Ignore br tags normally -->
   <xsl:template match="br" />

   <!-- In the special case, need to group elements following the br tag -->
   <xsl:template match="br" mode="special">
      <p>
         <!-- Copy the attributes for current p tag -->
         <xsl:apply-templates select="../../@*" />
         <span>
            <!-- Copy the attributes for current span tag -->
            <xsl:apply-templates select="../@*" />

            <!-- List all elements following the br tag -->
            <xsl:apply-templates select="key('br', generate-id())"/>
         </span>
      </p>
   </xsl:template>

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

When applied to your input sample, the following is output...

<body>
   <p class="heading">
      <span>
         <tag>This text</tag>
      </span>
   </p>
   <p class="bodyText">
      <span>
         <tag>is</tag>
      </span>
   </p>
   <p class="bodyText">
      <span>
         <tag>meant</tag>
      </span>
      <span>
         <tag>to</tag>
      </span>
   </p>
   <p class="bodyText">
      <span>
         <tag>be</tag>
      </span>
      <span>
         <tag>read</tag>
      </span>
   </p>
   <p class="bodyText">
      <span>
         <tag>with</tag>
      </span>
      <span>
         <tag>some</tag>
      </span>
      <span>
         <tag>inline</tag>
      </span>
   </p>
   <p class="bodyText">
      <span class="italic">
         <tag>styles</tag>
      </span>
   </p>
   <p class="bodyText">
      <span class="italic">
         <tag>the</tag>
      </span>
   </p>
   <p class="bodyText">
      <span class="italic">
         <tag>end</tag>
      </span>
   </p>
</body>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜