.net – xsl:template match找不到匹配项

我正在尝试使用.NET xslcompiledtransform将一些Xaml转换为 HTML,并且遇到了使xslt匹配Xaml标签的困难.例如,使用此Xaml输入:
<FlowDocument PagePadding="5,5,0" AllowDrop="True" NumberSubstitution.CultureSource="User" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
  <Paragraph>a</Paragraph>
</FlowDocument>

而这个xslt:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>

  <xsl:output method="html" indent="yes"/>

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

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

  <xsl:template match="Paragraph" >
    <p>
      <xsl:apply-templates />
    </p>
  </xsl:template>

我得到这个输出

<html>
    <body>
  a
</body>
</html>

而不是预期:

<html>
   <body>
      <p>a</p>
   </body>
</html>

这可能是命名空间的问题吗?这是我第一次尝试一个xsl转换,所以我很失落.

是的,这是命名空间的问题.您的输入文档中的所有元素都位于命名空间http://schemas.microsoft.com/winfx/2006/xaml/presentation中.您的模板正在尝试匹配认命名空间中的元素,并且没有找到任何内容.

您需要在转换中声明此名称空间,为其分配一个前缀,然后在该命名空间中匹配元素的任何模式中使用该前缀.所以你的XSLT应该是这样的:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
    xmlns:p="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    exclude-result-prefixes="msxsl"/>

<xsl:output method="html" indent="yes"/>

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

<xsl:template match="p:FlowDocument">
  <xsl:apply-templates />
</xsl:template>

<xsl:template match="p:Paragraph" >
  <p>
    <xsl:apply-templates />
  </p>
</xsl:template>

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念