xslt flattening out child elements in a DocBook para element

后端 未结 3 446
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-26 01:16

I am transforming some generated DocBook xml (from Doxygen) to my companies xml which is really a subset of DocBook. There is a para element like the following:

         


        
相关标签:
3条回答
  • 2021-01-26 01:55

    I had to correct a bit of your XML example so that it was well-formed. But the following:

        <xsl:template match="para">
            <xsl:for-each select="node()">
                <xsl:choose>
                    <xsl:when test="self::text() and normalize-space(.)!=''">
                        <xsl:element name="para">
                            <xsl:apply-templates select="."/>
                        </xsl:element>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:apply-templates select="."/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>
        </xsl:template>
        <xsl:template match="text()">
            <xsl:copy-of select="."/>
        </xsl:template>
        <xsl:template match="literallayout">
            <xsl:copy-of select="."/>
        </xsl:template>
        <xsl:template match="table">
            <xsl:copy-of select="."/>
        </xsl:template>
    

    Outputs:

    <para>some text..... </para>
    <literallayout>
    </literallayout>
    <para> more text.... </para>
    <table> ... </table>
    <para> even more text </para>
    <table>...</table>
    <literallayout>text also look here <link xlink:href="http://someurl.com"/></literallayout>
    <para> more text. </para>
    

    I hope that helps.

    0 讨论(0)
  • 2021-01-26 02:03

    I should use these templates:

    <xsl:template match="para">
        <xsl:apply-templates select="node()" mode="flat" />
    </xsl:template>
    
    <xsl:template match="*" mode="flat">
        <xsl:copy-of select="." />
    </xsl:template>
    
    <xsl:template match="text()[normalize-space()!='']" mode="flat">
        <para>
            <xsl:value-of select="."/>
        </para>
    </xsl:template> 
    
    <xsl:template match="text()[normalize-space()='']" mode="flat" />
    
    0 讨论(0)
  • 2021-01-26 02:21

    You can turn every text node within a para element into its own para using something like

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:template match="@*|node()">
        <xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
      </xsl:template>
    
      <xsl:template match="para">
        <xsl:apply-templates />
      </xsl:template>
    
      <xsl:template match="para/text()">
        <para><xsl:value-of select="." /></para>
      </xsl:template>
    </xsl:stylesheet>
    

    but this may not be sufficient if you only want to break up the para at certain child elements and not others.

    0 讨论(0)
提交回复
热议问题