How to parse “invalid” nested XML tags

I'm trying to construct an XSLT file for an XML file like the following, which uses invalid nesting of tags:

<Page>
 <Content>
   <par>This content <i>contains</i> some HTML <b><i>tags</i></b>.</par>
   <par>This content <b>also</b> contains some HTML <i><b>tags</b></i>.</par>
 </Content>
</Page>

Now if I want to output the content to a new document, I have something like this:

<xsl:template match="Page/Content">
  <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
  <xsl:for-each select="par">
    <xsl:apply-templates select="."/>
  </xsl:for-each>
  <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
</xsl:template>

<xsl:template match="par">
  <p><xsl:value-of select="." /></p>
</xsl:template>

<xsl:template match="b">
  <strong><xsl:value-of select="." /></strong>
</xsl:template>

<xsl:template match="i">
  <em><xsl:value-of select="." /></em>
</xsl:template>

My question is how I need to edit the template match="par" so that the <b> and <i> tags are displayed correctly?

I've tried things like

<xsl:template match="par">
  <p>
  <xsl:apply-templates select="i"/>
  <xsl:apply-templates select="b"/>
  <xsl:value-of select="." /></p>
</xsl:template>

but that always results in an incorrect order of the output, because the <i> and <b> tags are displayed before the complete paragraph. Is there a possibility to do this without changing the format of the original XML?

Answers


I don't see any incorrectly nested tags in your sample input so I'm not sure what you mean by that. XSLT can't process incorrectly nested XML because it's not valid XML.

At any rate, the main problem with your XSLT is that you are using value-of where you should be using apply-templates:

<xsl:template match="Page/Content">
  <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
  <xsl:apply-templates select="par"/>
  <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
</xsl:template>

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

<xsl:template match="b">
  <strong><xsl:apply-templates /></strong>
</xsl:template>

<xsl:template match="i">
  <em><xsl:apply-templates /></em>
</xsl:template>

However, you haven't shown us the output you want so I'm not sure that this will completely solve your issue.


Need Your Help

How to always run migration during OpsWorks deployments to Rails stacks

ruby-on-rails ruby-on-rails-4 amazon-web-services aws-opsworks opsworks

I have a few Rails stacks set up on AWS OpsWorks, and I primarily use the OpsWorks console web app to deploy my code to the stack from GitHub.

Strange Maven out of memory error

maven-2 hudson maven

I am currently trying to build my project using hudson to call maven. I keep getting the problem of out of memoery error. I set the xmx and xms in all environmental variable, hudson configuration and

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.