2013-05-16 23 views
0

轉換XHTML到XAML我有以下XML使用XSLT

User entered text : Normal **Bold** *Italic* 
u̲n̲d̲e̲r̲l̲i̲n̲e̲ 
***BoldItalic*** ***B̲o̲l̲d̲I̲t̲a̲l̲i̲c̲U̲n̲d̲e̲r̲l̲i̲n̲e̲*** 

產生的XHTML字符串存儲在數據庫中的表。

<QuestionText xml:space="preserve"> 
    <p>Normal 
    <b>Bold </b> 
    <i>Italic </i> 
    <u>Underline </u> 

    <b>Bold <i>BoldItalic </i>Bold </b> 

    <b><i>BoldItalic </i></b> 
    <b><i><u>BoldItalicUnderline</u></i></b> 
    </p> 
    <p/> 
    <p/> 
    <p/> 
    </QuestionText> 

我需要轉換到.NET X(A)ML如下

<Section> 
    <Paragraph> 
    <Span Text="Nornal " /> 
    <Span FontWeight="Bold" Text="Bold " /> 
    <Span FontStyle="Italic" Text="Italic " /> 
    <Span Text="Underline " UnderlineDecoration="Line" /> 

    <Span FontWeight="Bold" Text="Bold " /> 
    <Span FontStyle="Italic" FontWeight="Bold" Text="BoldItalic " /> 
    <Span FontWeight="Bold" Text="Bold " /> 

    <Span FontStyle="Italic" FontWeight="Bold" Text="BoldItalic " /> 
    <Span FontStyle="Italic" FontWeight="Bold" Text="BoldItalicUnderline" UnderlineDecoration="Line" /> 
    </Paragraph> 
    <Paragraph/> 
    <Paragraph/> 
    <Paragraph/> 
</Section> 

我試着用這個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"> 
    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="QuestionText"> 
     <Section> 
     <xsl:apply-templates/> 
     </Section> 
    </xsl:template> 

    <xsl:template match="b|i|u|p/text()" > 

    <span> 
     <xsl:attribute name="Text"> 
     <xsl:value-of select="current()"/> 
     </xsl:attribute> 
     <xsl:attribute name="name"> 
     <xsl:value-of select="name()"/> 
     </xsl:attribute> 
     <xsl:attribute name="parent"> 
     <xsl:value-of select="name(..)"/> 
     </xsl:attribute> 

     <xsl:if test=".=i"> 
     <xsl:attribute name="FontStyle">italic</xsl:attribute> 
     </xsl:if> 

     <xsl:if test=".=b"> 
     <xsl:attribute name="FontWeight">bold</xsl:attribute> 
     </xsl:if> 

     <xsl:if test=".=u"> 
     <xsl:attribute name="UnderlineDecoration">line</xsl:attribute> 
     </xsl:if> 
    </span> 

    <xsl:apply-templates/> 
    </xsl:template> 

    <xsl:template match="p"> 
    <Paragraph> 
     <xsl:apply-templates/> 
    </Paragraph> 
    </xsl:template> 

</xsl:stylesheet> 

XSLT的任何幫助,高度讚賞。

+0

您在源XML中顯示的數據不是有效的XML。我已經爲您的問題應用了降價,以便從字面上顯示文字,但我完全不確定您在問什麼。 – Borodin

+0

大家注意:'̲'是Unicode組合下劃線字符。 – Borodin

+0

沒有運氣......在什麼意義上? – JLRishe

回答

1

這實質上產生您提供的樣本輸出:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="QuestionText"> 
    <Section> 
     <xsl:apply-templates /> 
    </Section> 
    </xsl:template> 

    <xsl:template match="text()[normalize-space()]" > 
    <Span Text="{.}"> 
     <xsl:apply-templates select="ancestor::*" mode="formatting" /> 
    </Span> 
    </xsl:template> 

    <xsl:template match="i" mode="formatting"> 
    <xsl:attribute name="FontStyle">italic</xsl:attribute> 
    </xsl:template> 
    <xsl:template match="b" mode="formatting"> 
    <xsl:attribute name="FontWeight">bold</xsl:attribute> 
    </xsl:template> 
    <xsl:template match="u" mode="formatting"> 
    <xsl:attribute name="UnderlineDecoration">line</xsl:attribute> 
    </xsl:template> 
    <xsl:template match="*" mode="formatting" /> 

    <xsl:template match="p"> 
    <Paragraph> 
     <xsl:apply-templates /> 
    </Paragraph> 
    </xsl:template> 

</xsl:stylesheet> 

當你的樣品輸入運行,其結果是:

<Section> 
    <Paragraph> 
    <Span Text="Normal &#xA; " /> 
    <Span Text="Bold " FontWeight="bold" /> 
    <Span Text="Italic " FontStyle="italic" /> 
    <Span Text="Underline " UnderlineDecoration="line" /> 

    <Span Text="Bold " FontWeight="bold" /> 
    <Span Text="BoldItalic " FontWeight="bold" FontStyle="italic" /> 
    <Span Text="Bold " FontWeight="bold" /> 

    <Span Text="BoldItalic " FontWeight="bold" FontStyle="italic" /> 
    <Span Text="BoldItalicUnderline" FontWeight="bold" FontStyle="italic" UnderlineDecoration="line" /> 
    </Paragraph> 
    <Paragraph /> 
    <Paragraph /> 
    <Paragraph /> 
</Section> 

的問題是在第一跨度的&#xA;,因爲源XML中的第一個文本節點在其後包含換行符和空白符。你想如何解決這個問題?將normalize-space()放在Text屬性中很容易,但也會去除其他尾隨空格。

+0

你節省了我的一天。你是英雄。 – teenboy

+0

我有一個問題。如果xml內容有:加粗 BoldItalic,那麼我沒有得到所需的結果。 。任何幫助表示讚賞。 – teenboy

+0

@teenboy修改我的答案以適應您的新要求。如果您事後需要您的要求,請考慮打開一個新問題,而不是在已經符合最初規定的要求的答案上取消接受。 – JLRishe