我正在尝试使用 .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">
<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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)