2014-02-13 6 views
0

я следующий XMLшелуха HTML теги в XSLT

<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#? –

ответ

1

Вместо использования XSL: для-каждого, вы могли бы использовать XSL: применять-шаблоны

<xsl:apply-templates select="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> 

0 llowing 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. 
Смежные вопросы