替换xml的一部分的正确xsl是什么

时间:2015-05-28 18:40:25

标签: html xml xslt

我正在尝试使用xsl文件创建一些html。大多数工作正常,但我对匹配更换xml的一部分的规则感到困惑。这是一个示例,我只需要用xsl规则替换 secondLine 标记。

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

<xsl:template match="/">
  <html>
    <head></head>
    <body>
      <xsl:apply-templates />
    </body>
  </html>
</xsl:template>

<xsl:template match="content/secondLine">
    <b>Second Line</b>
</xsl:template>

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

xsl文件:

<html>
<head></head>
<body>
<b>First Line</b>
<br/>
<b>Second Line</b>
<br/>
<b>Third Line</b>
</body>
</html>

它并没有真正取代 secondLine 。我正在寻找像这样的输出

void StackPanel_DragEnter(object sender, System.Windows.DragEventArgs e)
{
     e.Effects = System.Windows.DragDropEffects.Copy;
}

1 个答案:

答案 0 :(得分:1)

在这种情况下使用的工具 - 当你只想修改XML输入的部分内容并保留大部分内容时 - 是MoveWindow,它会按原样复制所有内容 - 除非另一个模板覆盖它对于(更多)特定节点。

尝试以下样式表:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="/website">
    <html>
        <head/>
        <body>
        <xsl:apply-templates select="content/*"/>
    </body>
  </html>
</xsl:template>

<xsl:template match="secondLine">
    <b>Second Line</b>
</xsl:template>

</xsl:stylesheet>

如您所见,它为默认规则创建了两个例外:第一个创建HTML包装器并跳过现有的content包装器;第二个替换secondLine节点。

相关问题