XSL:模板匹配没有找到匹配 [英] xsl:template match doesn't find matches

查看:222
本文介绍了XSL:模板匹配没有找到匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一些XAML中使用.NET XslCompiledTransform HTML和正在运行陷入困境得到XSLT匹配的XAML标记。比如这个XAML中输入:

I'm trying to convert some Xaml to HTML using the .NET XslCompiledTransform and am running into difficulties getting the xslt to match Xaml tags. For instance with this Xaml input:

<FlowDocument PagePadding="5,0,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>

我得到这样的输出:

I get this output:

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

,而不是预期的:

Rather than the expected:

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

难道这是与命名​​空间的问题吗?这是我在一个XSL第一次尝试转型,所以我不知所措。

Could this be a problem with the namespace? This is my first attempt at an xsl transform, so I'm at a loss.

推荐答案

是的,它与命名空间中的一个问题。所有在输入文档中的元素都在命名空间 http://schemas.microsoft.com/winfx/2006/xaml/$p$psentation 。您的模板是试图以匹配在默认名称空间的元素,并且它没有找到任何。

Yes, it's a problem with the namespace. All of the elements in your input document are in the namespace http://schemas.microsoft.com/winfx/2006/xaml/presentation. Your template is trying to match elements that are in the default namespace, and it's not finding any.

您需要声明这个命名空间中的变换,分配给它一个preFIX,然后使用preFIX在旨在满足该命名空间中元素的任何模式。所以,你的XSLT应该是这个样子:

You need to declare this namespace in your transform, assign it a prefix, and then use that prefix in any patterns that are intended to match elements in that namespace. So your XSLT should look something like this:

<?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>

这篇关于XSL:模板匹配没有找到匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆