0

我正在用 C# 构建一个 Windows 8 应用程序。我在 XSLT 文件的帮助下将 HTML 转换为 XAML。我正在使用https://github.com/MacawNL/WinRT-RichTextBlock.Html2Xaml将 HTML 转换为 XAML。这适用于我一直使用的所有 HTML 标签,除了 H2(或 H3)。

我的 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="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes"/>

    <!-- The html root element must be div, it translates to a xaml richtextblock.-->
    <xsl:template match="/div" priority="9">
      <RichTextBlock>
        <RichTextBlock.Resources>
          <Style x:Key="Bullet" TargetType="Ellipse">
            <Setter Property="Fill" Value="Black" />
            <Setter Property="Width" Value="6" />
            <Setter Property="Height" Value="6" />
            <Setter Property="Margin" Value="-30,0,0,1" />
          </Style>
          <Style x:Key="Link" TargetType="HyperlinkButton">
            <Setter Property="Foreground" Value="#ff6600" />
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="FontSize" Value="14" />
            <Setter Property="Margin" Value="-15,-11" />
          </Style>
        </RichTextBlock.Resources>
        <xsl:if test="normalize-space(text()) != ''">
          <Paragraph><xsl:value-of select="normalize-space(text())" /></Paragraph>
        </xsl:if>
        <xsl:apply-templates select="/div/*" />
      </RichTextBlock>
    </xsl:template>
    <xsl:template match="div" priority="0">
      <Span><xsl:apply-templates /></Span>
    </xsl:template>

    <!-- XAML Paragraphs cannot contain paragraphs, so we convert top-level html paragraphs to xaml paragraphs and convert nested html paragraphs to xaml spans with linebreaks -->
    <xsl:template match="/div/P | /div/p" priority="9">
      <Paragraph LineStackingStrategy="MaxHeight" Foreground="Black"><xsl:apply-templates />
        <LineBreak />
      </Paragraph>
    </xsl:template>
    <xsl:template match="P | p" priority="0">
      <Paragraph LineStackingStrategy="MaxHeight" Foreground="Black"><LineBreak /><xsl:apply-templates /><LineBreak /></Paragraph>
    </xsl:template>
    <xsl:template match="h2 | H2">
      <Paragraph>
        <Bold FontSize="56" Foreground="Black">
          <xsl:apply-templates />
        </Bold>        
        <LineBreak/>
      </Paragraph>
    </xsl:template>
    <!-- The RichTextBlock XAML element can contain only paragraph child elements, so any unknown html child elements of the root element will become XAML paragraphs -->
    <xsl:template match="/div/*">
      <Paragraph LineStackingStrategy="MaxHeight" Foreground="Black"><xsl:apply-templates /></Paragraph>
    </xsl:template>

    <!-- Lists can only occur outside paragraphs, at the top level -->
    <xsl:template match="/div/UL | /div/ul">
      <Paragraph Foreground="Black" Margin="20,0,0,0"><LineBreak /><xsl:apply-templates /></Paragraph>
    </xsl:template>

  <xsl:template match="LI | li">
      <Span><InlineUIContainer><Ellipse Style="{{StaticResource Bullet}}"/></InlineUIContainer><xsl:apply-templates /><LineBreak /></Span>
    </xsl:template>

    <xsl:template match="B | b">
      <Bold FontSize="56"><xsl:apply-templates /></Bold>
    </xsl:template>
    <xsl:template match="STRONG | strong">
      <Bold FontSize="20" Foreground="Black">
        <xsl:apply-templates />
      </Bold>
    </xsl:template>
    <xsl:template match="I | i">
      <Italic><xsl:apply-templates /></Italic>
    </xsl:template>

    <xsl:template match="U | u">
      <Underline><xsl:apply-templates /></Underline>
    </xsl:template>

    <xsl:template match="BR | br">
      <LineBreak />
    </xsl:template>

    <xsl:template match="A | a">
      <Span><InlineUIContainer><HyperlinkButton Style="{{StaticResource Link}}"><xsl:attribute name="NavigateUri"><xsl:value-of select="@href"/></xsl:attribute><xsl:apply-templates /></HyperlinkButton></InlineUIContainer></Span>
    </xsl:template>

    <xsl:template match="IMG | img">
      <InlineUIContainer><Image MaxHeight="480" Margin="0,20,0,10" Stretch="Uniform" ><xsl:attribute name="Source"><xsl:value-of select="@src"/></xsl:attribute><xsl:apply-templates /></Image></InlineUIContainer>
    </xsl:template>

    <!-- Note that by default, the text content of any unmatched HTML elements will be copied in the XAML. -->
</xsl:stylesheet>

我已经尝试了我能找到的任何选项,但它们都不起作用。知道如何匹配 H2 标记以将其转换为 XAML 吗?

4

1 回答 1

1

默认优先级<xsl:template match="/div/*">高于 , <xsl:template match="h2 | H2"> 因此如果 h2 的父级是 div,则 h2 模板将永远不会触发。将 `priority="10" 添加到 h2 模板(或您选择的其他数字)

于 2013-01-15T01:15:22.733 回答