0

I have the following XML

<p><bold>Dr. Rudy Smaling</bold></p>
<p>Dr. Rudy Smaling currently serves as executive director of systems engineering at Cummins, responsible for implementation of systems engineering principles and processes across the corporation. Dr. Smaling previously held the position of Chief Engineer with global responsibility for hybrid system architecture and new product development in Eaton Corporation&#x0027;s Hybrid Power Systems Division. Dr. Smaling also holds a position as adjunct Professor in Mechanical and Aeronautical Engineering at Western Michigan University.</p>

I am using following XSLT

<xsl:for-each select="p">
  <xsl:value-of select="text()"/>
</xsl:for-each>

But it is showing following output

 Dr. Rudy Smaling currently serves as executive director of systems engineering at Cummins, responsible for implementation of systems engineering principles and processes across the corporation. Dr. Smaling previously held the position of Chief Engineer with global responsibility for hybrid system architecture and new product development in Eaton Corporation&#x0027;s Hybrid Power Systems Division. Dr. Smaling also holds a position as adjunct Professor in Mechanical and Aeronautical Engineering at Western Michigan University

But I want to parse bold tag also. I want output in the following way

**Dr. Rudy Smaling**    
Dr. Rudy Smaling currently serves as executive director of systems engineering at Cummins, responsible for implementation of systems engineering principles and processes across the corporation. Dr. Smaling previously held the position of Chief Engineer with global responsibility for hybrid system architecture and new product development in Eaton Corporation&#x0027;s Hybrid Power Systems Division. Dr. Smaling also holds a position as adjunct Professor in Mechanical and Aeronautical Engineering at Western Michigan University

How to do this?

1
  • Why this question tagged with C# tag? Commented Feb 13, 2014 at 13:16

1 Answer 1

1

Instead of using xsl:for-each, you could use xsl:apply-templates

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

Now, if you did not actually want to output p tags, you would not need to write a template that matches p, but instead allow XSLT's built-in templates to match it, which will jump over it and carry on processing its children, outputting any text nodes it finds. Unless, of course, you wanted a carriage return after each p.

This means you only need to write a template for your bold attribute

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

Try this XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output omit-xml-declaration="yes" indent="yes" />
   <xsl:template match="/*">
     <xsl:apply-templates select="p" />     
   </xsl:template>

   <xsl:template match="p">
     <xsl:apply-templates />
     <xsl:text>&#10;</xsl:text>
   </xsl:template>

   <xsl:template match="bold">
     <xsl:text>**</xsl:text>
        <xsl:apply-templates />
     <xsl:text>**</xsl:text>
   </xsl:template>
</xsl:stylesheet>

When applied to the following well-formed XML (note it has a single root element)

<body>
  <p><bold>Dr. Rudy Smaling</bold></p>
  <p>Dr. Rudy Smaling currently serves as executive director of systems engineering at Cummins, responsible for implementation of systems engineering principles and processes across the corporation. Dr. Smaling previously held the position of Chief Engineer with global responsibility for hybrid system architecture and new product development in Eaton Corporation&#x0027;s Hybrid Power Systems Division. Dr. Smaling also holds a position as adjunct Professor in Mechanical and Aeronautical Engineering at Western Michigan University.</p>
</body>

The following is output

**Dr. Rudy Smaling**
Dr. Rudy Smaling currently serves as executive director of systems engineering at Cummins, responsible for implementation of systems engineering principles and processes across the corporation. Dr. Smaling previously held the position of Chief Engineer with global responsibility for hybrid system architecture and new product development in Eaton Corporation's Hybrid Power Systems Division. Dr. Smaling also holds a position as adjunct Professor in Mechanical and Aeronautical Engineering at Western Michigan University.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.