代码之家  ›  专栏  ›  技术社区  ›  Ace

XSL:删除xml标记但保留其内容

  •  1
  • Ace  · 技术社区  · 15 年前

    <para> 从docbook中标记,并将其替换为 <p> . 您可能会认为这很好,但这会导致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>
    

    对不起,我本该更清楚地回答这个问题的。我需要从文档中删除深度不同的所有标记,但始终遵循(本地)树列表项/para。我对此有点陌生,但我是否可以把它附加到docbook2dita转换上,从而犯错误呢。能在那个地方吗?

    3 回复  |  直到 15 年前
        1
  •  5
  •   user357812 user357812    15 年前

    我将使用这个样式表:

    <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

        2
  •  3
  •   Dirk Vollmar    15 年前

    您可以使用XSLT处理dita文件,该XSLT过滤掉 <para>

    <?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>
    
        3
  •  0
  •   Alexis Wilke    11 年前

    我也遇到过类似的问题,但使用的QtDom并不总是像XSLT 2.x规范那样100%工作。(我正在考虑在某个时候切换到Apache库…)

    <xsl:for-each select="/orderedlist/lisitem">
      <div class="listitem">
        <xsl:apply-templates select="node()"/>
      </div>
    </xsl:for-each>
    

    这将删除列表项,并将其替换为<div class=“listitem”>

    在我的例子中,<para>中的模板可以包含标记,因此我不能使用其他两个将所有内容转换为纯文本的示例。相反,我用的是:

    <xsl:template match ="para">
      <xsl:copy-of select="node()"/>
    </xsl:template>
    

    这将删除“para”标记,但保持所有子项不变。因此段落可以包含格式,并在整个XSLT处理过程中保留格式。