加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 07:43:18 所属栏目:百科 来源:网络整理
导读:我正在尝试使用.NET XslCompiledTransform将一些Xaml转换为 HTML,并且遇到了使xslt匹配Xaml标签的困难.例如,使用此Xaml输入: FlowDocument PagePadding="5,5,0" AllowDrop="True" NumberSubstitution.CultureSource="User" xmlns="http://schemas.microsoft
我正在尝试使用.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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读