анализ HTML-тегов в xslt

0

У меня есть следующий 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>

Я использую следующий XSLT

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

Но он показывает следующий результат

 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

Но я хочу также разделить жирный тег. Я хочу, чтобы результат был следующим образом

**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

Как это сделать?

  • 0
    Почему этот вопрос помечен тегом C #?
Теги:
xslt

1 ответ

1

Вместо использования xsl: for-each, вы можете использовать xsl: apply-templates

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

Теперь, если вы действительно не хотите выводить p- теги, вам не нужно будет писать шаблон, который соответствует p, но вместо этого позволит использовать встроенные шаблоны XSLT, которые будут перепрыгивать через него и продолжать обработку своих дочерних элементов, выводя любые найденные им текстовые узлы. Если, конечно, вы не хотели, чтобы каретка возвращалась после каждого p.

Это означает, что вам нужно только написать шаблон для вашего жирного атрибута

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

Попробуйте этот 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>

При применении к следующему хорошо сформированному XML (обратите внимание, что он имеет один корневой элемент)

<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>

Выводится следующее:

**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 Hybrid Power Systems Division. Dr. Smaling also holds a position as adjunct Professor in Mechanical and Aeronautical Engineering at Western Michigan University.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню