XSLT与C#win8应用程序中的h2标记不匹配

时间:2013-01-14 21:16:23

标签: c# xaml xslt windows-8

我正在使用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>

我已经尝试过任何我能找到的opties,但没有一个能奏效。知道如何以这种方式匹配H2标签我可以将其转换为XAML吗?

1 个答案:

答案 0 :(得分:1)

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