2017-08-30 98 views
0

我遇到了XSLT問題,並且完全卡住了。從段落中刪除嵌入圖像

我的情況如下,我收到一個word文檔。我必須將其轉換爲我們的內部XML格式。在這種格式下,圖像已將與段落分開。

我已經嘗試了許多像每個循環,模板,使用helpercode的事情,但我認爲我在XSLT方面的知識僅限於解決該問題。

在易方面,我收到Wordxml如下

<w:document> 
    <w:p> 
     <w:r> 
      <w:t>sometext</w:t> 
     </w:r> 
     <w:r> 
      <w:drawing></w:drawing> 
     </w:r> 
     <w:r> 
      <w:t>anothertext</w:t> 
     </w:r> 
    </w:p> 
</w:document> 

我試圖創建以下兩種結果。

選項1:

<w:document> 
    <w:p> 
     <w:r> 
      <w:t>sometext</w:t> 
     </w:r> 
    </w:p> 
    <w:drawing></w:drawing> 
    <w:p> 
     <w:r> 
      <w:t>anothertext</w:t> 
     </w:r> 
    </w:p> 
</w:document> 

選項2:

<w:document> 
    <w:p> 
     <w:r> 
      <w:t>sometext</w:t> 
     </w:r> 
     <w:r> 
      <w:t>anothertext</w:t> 
     </w:r> 
    </w:p> 
    <w:drawing></w:drawing> 
</w:document> 
+0

輸入XML和XML不能很好形成的個XML。您能否確認是否錯過或不能有效使用適當的結束標籤。 –

+0

糾正了我的格式不正確的XML –

+0

只是爲了確認所需的輸出,因爲它們有多個文檔元素。這又使得它們不是格式良好的XML。你確定你需要與選項中提到的相同的輸出嗎? –

回答

3

試試這個:

<xsl:template match="w:p[w:r/w:drawing]"> 
    <xsl:copy> 
    <xsl:apply-templates select="*[not(w:drawing)]"/> 
    </xsl:copy> 
    <xsl:apply-templates select="w:r/w:drawing"/> 
</xsl:template> 

我不知道這是否會包括所有的可能性,但它應該輸出爲每選擇2與給出的樣本。

+0

我只能通過添加身份模板來重現此解決方案。 – zx485

+0

這不是一個完整的樣式表,它是一個能夠實現所需更改的模板。我不知道需要什麼其他處理,所以我沒有假設身份模板是合適的。我不認爲我的工作是爲他們寫OP的xslt,只是爲了幫助他們這樣做。 – Flynn1179

+0

那麼,只是考慮這個筆記。 – zx485

0

這裏是選項1的溶液:在選項2

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

<xsl:template match="w:p">      <!-- remove outter w:p --> 
    <xsl:apply-templates /> 
</xsl:template> 

<xsl:template match="w:r[w:drawing]">   <!-- move up w:drawing one level --> 
    <xsl:copy-of select="*" /> 
</xsl:template> 

<xsl:template match="w:r">      <!-- encapsulate w:r in w:p and copy it --> 
    <w:p> 
    <xsl:copy> 
     <xsl:apply-templates /> 
    </xsl:copy> 
    </w:p> 
</xsl:template> 
+0

雖然這個答案是非常清晰的,並輸出我期望的解決方案(選項1),我沒有使用它,因爲給出的其他答案更具體,因此可以應用比這個解決方案更少的副作用。 –

+0

感謝您花時間解釋您的決定。 – zx485