将单个XML标记输出到HTML标记的最简单方法

时间:2012-03-19 22:27:56

标签: xml xslt

是否有一种简单的方法可以格式化单个heading并将这些标题标记转换为XSLT中的h3标记?

<webpage>
  <heading>Header</heading>
  <p> paragraph <a href="http://www.google.com">google</a> more paragraph</p>
  <ul>
    <li>item1</li>
    <li>item2</li>
    <li>item3</li>
    <li>item4</li>
    <li>item5</li>
    <li>item6</li>
  </ul> 
  <p>more paragraph<a href="http://www.potato.com">potato.com</a></p>
  <p>more paragraph</p>
  <p>more paragraph</p>
  <p>more paragraph</p>
  <p>more paragraph</p>
  <p>more paragraph</p>
  <p>more paragraph</p>
  <heading>heading</heading>
  <p> here comes a list:</p>
  <ul>
    <li>item1</li>
    <li>item2</li>
    <li>item3</li>
  </ul>
  <p>more paragraph</p>
  <p>more paragraph</p>
  <p>more paragraph</p>
  <p>more paragraph</p>

</webpage>

目标是把它变成这个

  <h3>Header</h3>
  <p> paragraph <a href="http://www.google.com">google</a> more paragraph</p>
  <ul>
    <li>item1</li>
    <li>item2</li>
    <li>item3</li>
    <li>item4</li>
    <li>item5</li>
    <li>item6</li>
  </ul> 
  <p>more paragraph<a href="http://www.potato.com">potato.com</a></p>
  <p>more paragraph</p>
  <p>more paragraph</p>
  <p>more paragraph</p>
  <p>more paragraph</p>
  <p>more paragraph</p>
  <p>more paragraph</p>
  <h3>heading</h3>
  <p> here comes a list:</p>
  <ul>
    <li>item1</li>
    <li>item2</li>
    <li>item3</li>
  </ul>
  <p>more paragraph</p>
  <p>more paragraph</p>
  <p>more paragraph</p>
  <p>more paragraph</p>

这似乎应该有效,但事实并非如此:

  <xsl:variable name="webpage" select="document('webpage.xml')/webpage">
    <xsl:template match="@*|node()">
      <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
    </xsl:template>
    <xsl:template match="/webpage/heading">
      <h3>
        <xsl:value-of select="."/>
      </h3>
    </xsl:template>    
  </xsl:variable>

1 个答案:

答案 0 :(得分:1)

试试这个:

        <?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:variable name="webpage" select="document('webpage.xml')/webpage"/>

    <!-- No input document - so we set the initial template to start when we call the transform --> 
    <xsl:template name="start">
        <xsl:apply-templates select="$webpage/@*|$webpage/node()"/>
    </xsl:template>

    <!-- This template is the identity transform and will copy everything from the input to the output unless there a template that is a better match -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!-- This template overrides the identity transform and changes the heading to an h3 -->
    <xsl:template match="/webpage/heading">
        <h3><xsl:value-of select="."/></h3>
    </xsl:template>

</xsl:stylesheet>

当应用于提供的输入时,xml产生:

<?xml version="1.0" encoding="UTF-8"?><webpage>
    <h3>Header</h3>
    <p> paragraph <a href="http://www.google.com">google</a> more paragraph</p>
    <ul>
        <li>item1</li>
        <li>item2</li>
        <li>item3</li>
        <li>item4</li>
        <li>item5</li>
        <li>item6</li>
    </ul> 
    <p>more paragraph<a href="http://www.potato.com">potato.com</a></p>
    <p>more paragraph</p>
    <p>more paragraph</p>
    <p>more paragraph</p>
    <p>more paragraph</p>
    <p>more paragraph</p>
    <p>more paragraph</p>
    <h3>heading</h3>
    <p> here comes a list:</p>
    <ul>
        <li>item1</li>
        <li>item2</li>
        <li>item3</li>
    </ul>
    <p>more paragraph</p>
    <p>more paragraph</p>
    <p>more paragraph</p>
    <p>more paragraph</p>

</webpage>