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

发布于 2024-09-27 04:11:38 字数 949 浏览 7 评论 0原文

我最近将几个 .xml 文件从 docbook 更改为 dita。转换顺利,但存在一些不需要的工件。我感到困惑的是 .dita 无法识别 docbook 中的 标签,并将其替换为

。您认为这很好,但这会导致 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>

抱歉,我应该把问题说得更清楚。我需要从文档中删除深度不同的所有标签,但始终遵循(本地)树 listitem/para 。我对此有点陌生,但是我可能会因为将其附加到我的 docbook2dita 转换而做错了。可以在那个地方吗?

I recently changed a couple of my .xml files from docbook to dita. The conversion went ok, but there are some unwanted artifacts. The one I'm stumped on is that .dita does not recongnize the <para> tag from docbook, and replaces it with <p>. Which you'd think would be fine, but this causes the XML to show items in and ordered list as being on the next line, i.e:

1
 item One
2
 item Two

instead of:

1 item One
2 item Two

so how do i change this:

<section>
<title>Cool Stuff</title>
<orderedlist>
  <listitem>
    <para>ItemOne</para>
  </listitem>

  <listitem>
    <para>ItemTwo</para>
  </listitem>
</orderedlist>

to this:

<section>
<title>Cool Stuff</title>
<orderedlist>
  <listitem>
    ItemOne
  </listitem>

  <listitem>
    ItemTwo
  </listitem>
</orderedlist>

I'm sorry, I should have been more clear with the question. I need to remove all tags from the doument which are at varying levels of depth, but always follow the (local) tree listitem/para . I'm a bit new to this, but could I could just be doing it wrong by tacking it on to my docbook2dita transformation. Can it be in that place?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏の忆 2024-10-04 04:11:38

我会使用这个样式表:

<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 被绕过(这保留了混合内容)

I would use this stylesheet:

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

Note: Overwrite identity rule. listitem/para are bypassed (this preserves mixed content)

北城挽邺 2024-10-04 04:11:38

您可以使用过滤掉 节点的 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>

You can process the dita files with an XSLT that filters out the <para> nodes:

<?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>
单身情人 2024-10-04 04:11:38

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

我想用相应的类更改 div 中代码中的等效“listitem”:

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

这将删除 listitem 并将其替换为

然后,模板(在我的例子中,中的内容)可以包含标签,因此我无法使用将所有内容转换为纯文本的其他两个示例。相反,我使用了它:

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

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

I had a similar problem but am using QtDom which does not always work 100% like the XSLT 2.x specs. (I am thinking of switching to the Apache library at some point...)

I wanted to change the equivalent "listitem" in my code in a div with a corresponding class:

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

This removes the listitem and replaces it with <div class="listitem">

Then the template, what you have in <para>, in my case, can include tags, so I could not use the two other example that would transform everything into plain text. Instead I used that:

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

That removes the "para" tags, but keeps all the children as is. So paragraphs can include formatting and it is preserved across the XSLT processing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文