.net xsl:模板匹配不';找不到匹配项
我试图使用.NET XslCompiledTransform将一些Xaml转换为HTML,但在使xslt与Xaml标记匹配时遇到了困难。例如,使用此Xaml输入:.net xsl:模板匹配不';找不到匹配项,.net,xml,xslt,.net,Xml,Xslt,我试图使用.NET XslCompiledTransform将一些Xaml转换为HTML,但在使xslt与Xaml标记匹配时遇到了困难。例如,使用此Xaml输入: <FlowDocument PagePadding="5,0,5,0" AllowDrop="True" NumberSubstitution.CultureSource="User" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<FlowDocument PagePadding="5,0,5,0" AllowDrop="True" NumberSubstitution.CultureSource="User" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Paragraph>a</Paragraph>
</FlowDocument>
A.
这个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>
A.
与预期不同的是:
<html>
<body>
<p>a</p>
</body>
</html>
a
这可能是名称空间的问题吗?这是我第一次尝试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>
当我从源文档中删除它时,它会起作用:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
我认为你的最后两个模板根本不匹配。(您可以通过在FlowDocument模板中放入包装之类的内容进行测试。)只要尝试更改即可
“xsl:template match='/'”
在xsl文件中使用
“xsl:template match='*'”
这将为您提供所需的输出。谢谢Robert-我曾尝试将名称空间添加到xsl:stylesheet标记中,但没有将名称空间添加到匹配字段中。FlowDocument直接来自WPF RichTextBox,因此我宁愿在xslt中处理它,而不是通过操纵源代码。添加名称空间并限定元素匹配字段修复了该问题。