开发者

XSL: Removing xml tags but keeping their contents

开发者 https://www.devze.com 2023-01-20 10:16 出处:网络
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&g

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>
开发者_StackOverflow社区

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?


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)


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>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号