XSL:删除xml标签但保留其内容
问题描述:
我最近将一些.xml文件从docbook更改为dita。转换正常,但有一些不需要的工件。我难以忍受的是.dita没有从docbook中识别<para>
标签,并用<p>
替代它。你会觉得会好一些,但是这将导致以显示项目和有序列表作为下一行的XML,即:代替XSL:删除xml标签但保留其内容
1 item One 2 item Two
:
1 item One 2 item Two
所以怎么办我改变了:
<section>
<title>Cool Stuff</title>
<orderedlist>
<listitem>
<para>ItemOne</para>
</listitem>
<listitem>
<para>ItemTwo</para>
</listitem>
</orderedlist>
这样:
<section>
<title>Cool Stuff</title>
<orderedlist>
<listitem>
ItemOne
</listitem>
<listitem>
ItemTwo
</listitem>
</orderedlist>
对不起,我应该已经与问题更加清晰。我需要删除处于不同深度级别的所有标记,但始终要遵循(本地)树列表项/段落。我对此有点新,但是我可以通过将其应用于我的docbook2dita转换来做到这一点。它可以在那个地方吗?
答
我会用这个样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match ="listitem/para">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
注意:覆盖身份规则。 listitem/para
被绕过(这保留了混合内容)
答
您可以用过滤掉<para>
节点的XSLT处理DITA文件:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<!-- copy elements and attributes -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- replace para nodes within an orderedlist with their content -->
<xsl:template match ="orderedlist/listitem/para">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
答
我有一个类似的问题,但我使用QtDom并不总是像XSLT 2.x规范一样工作。 (我想到的是在某一时刻切换到Apache库...)
我想用一个相应的类来改变我的代码,相当于“列表项”在一个div:
<xsl:for-each select="/orderedlist/lisitem">
<div class="listitem">
<xsl:apply-templates select="node()"/>
</div>
</xsl:for-each>
这消除该列表项与< DIV CLASS =“列表项” >
然后模板,你有<对>,在我的情况下,可以包括标签替换它,所以我不能使用将改变其他两个例子一切都变成纯文本。相反,我使用的是:
<xsl:template match ="para">
<xsl:copy-of select="node()"/>
</xsl:template>
这将删除“para”标签,但保留所有的孩子。所以段落可以包含格式,并且在XSLT处理中保留。
对不起,我应该更清楚的问题。我需要删除处于不同深度级别的所有标签,但始终遵循(本地)树orderedlist/listitem/para。我对此有点新,但是我可以通过将其应用于我的docbook2dita转换来做到这一点。它可以在那个地方吗? –
Ace
2010-10-12 20:56:31