开发者

XSLT: Transfer text of element to parent element, and remove element

This is my XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="test_2.xsl" type="text/xsl"?>

<doc xmlns="http://www.foo.org">
  <div>
    <title>Mr. Title</title>
    <paragraph>This is one paragraph.
    </paragraph>
    <paragraph>Another paragraph.
    </paragraph>
    <list>
      <orderedlist>
        <item>
          <paragraph>An item paragraph.</paragraph>
        </item>
        <item>
          <paragraph>Another item paragraph</paragraph>
        </item>
      </orderedlist>
  开发者_如何学Go  </list>
  </div>    
</doc>

My XML gets rid of list, changes orderedlist to ol, changes item to li. Now I would like to get rid of the paragraph nodes that are children of item while transferring the text to the new li. Note that I do not want to get rid of the paragraph nodes that are NOT children of item.

This is my XSLT so far:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:foo="http://www.foo.org">

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="foo:doc">
  <xsl:element name="newdoc" namespace="http://www/w3.org/1999/xhtml">
   <xsl:apply-templates/>
  </xsl:element>
 </xsl:template>

 <xsl:template match="foo:div">
  <segment title="{foo:title}">
   <xsl:apply-templates/>
  </segment>
 </xsl:template>

 <xsl:template match="foo:title">
  <xsl:element name="h2">
   <xsl:apply-templates/>
  </xsl:element>
 </xsl:template>

 <xsl:template match="foo:paragraph">
  <xsl:element name="p">
   <xsl:apply-templates/>
  </xsl:element>
 </xsl:template>

 <xsl:template match="foo:list">
  <xsl:apply-templates/>
 </xsl:template>

 <xsl:template match="foo:orderedlist">
  <xsl:element name="ol">
   <xsl:apply-templates/>
  </xsl:element>
 </xsl:template>

 <xsl:template match="foo:item">
  <xsl:element name="li">
   <xsl:apply-templates/>
  </xsl:element>
 </xsl:template>

</xsl:stylesheet>

The output is this:

<newdoc xmlns="http://www/w3.org/1999/xhtml">
  <segment xmlns="" title="Mr. Title">
    <h2>Mr. Title</h2>
    <p>This is one paragraph.
    </p>
    <p>Another paragraph.
    </p>

      <ol>
        <li>
          <p>An item paragraph.</p>

        </li>
        <li>
          <p>Another item paragraph</p>
        </li>
      </ol>

  </segment>    
</newdoc>

Bonus: I would also like to get rid of the blank lines and correct the weird formatting that was caused by eliminating the list node with

 <xsl:template match="foo:list">
  <xsl:apply-templates/>
 </xsl:template>

so if anybody knows a better way to remove a node, I'd love to hear it.

Thanks much!


Simply add a more specific rule for paragraphs that are children of lists:

<xsl:template match="foo:item/foo:paragraph">
  <xsl:apply-templates/>
</xsl:template>

The same applies for your bonus question, if I understand the question correctly: Do not copy pure-whitespace text nodes which are children of list elements.

<xsl:template match="foo:item/text()[normalize-space(.)='']" />


This stylesheet:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:foo="http://www.foo.org"
 xmlns="http://www/w3.org/1999/xhtml"
 exclude-result-prefixes="foo">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:preserve-space elements="foo:paragraph"/>
    <xsl:template match="foo:doc">
        <newdoc>
            <xsl:apply-templates/>
        </newdoc>
    </xsl:template>
    <xsl:template match="foo:div">
        <segment title="{foo:title}">
            <xsl:apply-templates/>
        </segment>
    </xsl:template>
    <xsl:template match="foo:title">
        <h2>
            <xsl:apply-templates/>
        </h2>
    </xsl:template>
    <xsl:template match="foo:paragraph">
        <p>
            <xsl:apply-templates/>
        </p>
    </xsl:template>
    <xsl:template match="foo:orderedlist">
        <ol>
            <xsl:apply-templates/>
        </ol>
    </xsl:template>
    <xsl:template match="foo:item">
        <li>
            <xsl:apply-templates/>
        </li>
    </xsl:template>
    <xsl:template match="foo:item/foo:paragraph">
        <xsl:apply-templates/>
    </xsl:template>
</xsl:stylesheet>

Output:

<newdoc xmlns="http://www/w3.org/1999/xhtml">
   <segment title="Mr. Title">
      <h2>Mr. Title</h2>
      <p>This is one paragraph.     </p>
      <p>Another paragraph.     </p>
      <ol>
         <li>An item paragraph.</li>
         <li>Another item paragraph</li>
      </ol>
   </segment>
</newdoc>

Note: Currently, your stylesheet is a mess of namespaces. Use @exclude-result-prefixes. Correct whitespace only text nodes through xsl:strip-space and xsl:preserve-space declarations.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜