2008-10-24 43 views
1

我創建一個XML文件,其元素有時包含HTML格式的文本文檔的WordML內更換html標籤。XML到WordML中使用XSLT 1.0 - XML內容與格式的WordML標籤

<w:p> 
    <w:r> 
    <w:t> html formatted content is in here taken from xml file! </w:t> 
    </w:r> 
</w:p> 

這就是我的模板是如何設置。我有一個遞歸調用模板函數,可以對源xml內容進行文本替換。當遇到「<b>」標記時,我在CDATA中輸出一個包含「</w:t></w:r><w:r><w:rPr><w:b/></w:rPr><w:t>」的字符串以關閉當前運行並啓用啓用粗體格式的新運行。當它到達「</b>」標籤時,它將用下面的CDATA字符串「</w:t></w:r><w:r><w:t>」替換它。

我希望做的是使用XSL關閉標籤運行,並開始新的運行不使用CDATA字符串插入。這可能嗎?

回答

0

我最有可能能幫助你,如果只有我明白你的問題......就是在CDATA節的HTML或它解析爲輸入文檔(因此良好的XML)的一部分? 既然你談論'文本替換',我會假定你把'html格式化的內容'當作一個字符串(CDATA),因此需要一個遞歸調用模板函數來執行字符串替換。您將能夠使用XSL匹配模板來執行您現在正在做的事情的唯一方法是使分析文檔(輸入文檔)的html部分成爲可能。在這種情況下,您可以匹配b標記並將其替換爲適當的輸出(再次假設它始終可以被解析爲有效的XML)。您的問題,現在已經轉移...因爲(如果我理解你的問題正確)你試圖做的是密切的w:tw:r元素,然後「重新打開」他們...這是困難的,因爲它是(因爲你可能懷疑)在XSLT中很難做到這一點(你不能只在模板A中創建一個元素,然後在模板B中關閉它)。你將不得不開始搞亂輸出等,以實現這一目標。我現在我已經做了很多假設,但這裏是一個小例子來幫助你的方式:

的input.xml

<doc xmlns:w="urn:schemas-microsoft-com:office:word"> 
<w:p> 
    <w:r> 
    <w:t>before<b>bold</b>after</w:t> 
    </w:r> 
</w:p> 
</doc> 

convert_html.xsl

<xsl:template match="@*|node()"> 
    <xsl:copy> 
    <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="/doc/w:p/w:r/w:t//b"> 
    <xsl:value-of select="'&lt;/w:t>&lt;/w:r>&lt;w:r>&lt;w:rPr>&lt;w:b/>&lt;/w:rPr>&lt;w:t>'" disable-output-escaping="yes" /> 
    <xsl:apply-templates select="@*|node()"/> 
    <xsl:value-of select="'&lt;/w:t>&lt;/w:r>&lt;w:r>&lt;w:t>'" disable-output-escaping="yes" /> 
</xsl:template> 

現在運行

xalan input.xml convert_html.xsl 

產生

<?xml version="1.0" encoding="UTF-8"?><doc xmlns:w="urn:schemas-microsoft-com:office:word"> 
<w:p> 
    <w:r> 
    <w:t>before</w:t></w:r><w:r><w:rPr><w:b/></w:rPr><w:t>bold</w:t></w:r><w:r><w:t>after</w:t> 
    </w:r> 
</w:p> 
</doc> 

我的猜測是你想要的。

希望這有助於你一些。

0

從你的描述,這聽起來像你可以解析嵌入的HTML。如果是這樣,簡單地應用模板應該做你想做的。輸出中的wordML可能不對,但希望這會有所幫助。

樣品輸入:

<text> 
    <para> 
    Test for paragraph 1 
    </para> 
    <para> 
    Test for <b>paragraph 2</b> 
    </para> 
</text> 

變換:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:w="http://foo"> 
<xsl:template match="/"> 
    <w:p> 
    <w:r> 
     <xsl:apply-templates/> 
    </w:r>  
    </w:p> 
</xsl:template> 
    <xsl:template match="para"> 
    <w:t> 
     <xsl:apply-templates/> 
    </w:t> 
    </xsl:template> 

    <xsl:template match="b"> 
    <w:rPr> 
     <w:b/> 
    </w:rPr> 
     <xsl:value-of select="."/> 
    </xsl:template> 
</xsl:stylesheet> 

結果:

<w:p xmlns:w="http://foo"> 
    <w:r> 
    <w:t> 
     Test for paragraph 1 
    </w:t> 
    <w:t> 
     Test for <w:rPr><w:b /></w:rPr>paragraph 2 
    </w:t> 
    </w:r> 
</w:p> 
3

與WordML中工作是棘手的。使用XSLT將任意XML轉換爲WordML時的一個提示是,在處理塊時不用擔心文本運行,而是直接創建與text()節點匹配的模板,並在那裏創建文本。事實證明,Word不關心你是否嵌套文本運行,這使問題更容易解決。

<xsl:template match="text()" priority="1"> 
     <w:r> 
      <w:t> 
       <xsl:value-of select="."/> 
      </w:t> 
     </w:r> 
    </xsl:template> 

    <xsl:template match="@*|node()"> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:template> 

    <xsl:template match="para"> 
     <w:p> 
     <xsl:apply-templates select="text() | *" /> 
     </w:p> 
    </xsl:template> 

    <xsl:template match="b"> 
     <w:r> 
     <w:rPr> 
      <w:b /> 
     </w:rPr> 
     <w:t><xsl:apply-templates /></w:t> 
     </w:r> 
    </xsl:template> 

這避免了直接將標籤作爲轉義文本插入的錯誤XSLT技術。你會以粗體標籤作爲嵌套文本運行,但正如我所說的,Word不在乎。如果您使用這種技術,則需要注意不要將模板應用於段落之間的空白區域,因爲它會觸發文本模板並創建不符合上下文的運行。

0

要徹底完成HTML>的WordML我建議你的代碼的這個編輯版本:

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

<xsl:template match="text()" priority="1"><w:r><w:t><xsl:value-of select="."/></w:t></w:r></xsl:template> 

<xsl:template match="@*|node()"><xsl:apply-templates select="@*|node()"/></xsl:template> 

<xsl:template match="p"><w:p><xsl:apply-templates select="text() | *" /></w:p></xsl:template> 

<xsl:template match="b"><w:r><w:rPr><w:b /></w:rPr><xsl:apply-templates /></w:r></xsl:template> 
<xsl:template match="i"><w:r><w:rPr><w:i /></w:rPr><xsl:apply-templates /></w:r></xsl:template> 
<xsl:template match="u"><w:r><w:rPr><w:u w:val="single" /></w:rPr><xsl:apply-templates /></w:r></xsl:template> 

假設你的地方有你的HTML包裹在一個標籤一個xml