我有以下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'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'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's Hybrid Power Systems Division. Dr. Smaling also holds a position as adjunct Professor in Mechanical and Aeronautical Engineering at Western Michigan University
怎么做?
答案 0 :(得分:1)
您可以使用 xsl:apply-templates
,而不是使用 xsl:for-each<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> </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'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's Hybrid Power Systems Division. Dr. Smaling also holds a position as adjunct Professor in Mechanical and Aeronautical Engineering at Western Michigan University.