2010-06-30 154 views
2

我有一個這樣的文件:XSLT問題(命名空間?)

<?xml-stylesheet type="text/css" href="http://ltw1001.web.cs.unibo.it/svg.css" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:svg= "http://www.w3.org/2000/svg"> 
<body> 
<svg:svg width="500" height="560" version="1.1" > 

... 
... 

</svg:svg></body></html> 

我只能提取體的我TRED與內容:

<?xml version="1.0" standalone="no"?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns="http://www.w3.org/HTML/1998/html4"> 
    <xsl:template match="/"> 
     <xsl:value-of select="//body" /> 
    </xsl:template> 
</xsl:stylesheet> 

,但它不工作

+0

好問題(+1)。查看我的答案,瞭解適用於任何文檔結構的完整且簡單的解決方案。 – 2010-06-30 19:19:27

回答

0

您有(至少)兩個問題:

  1. 默認名稱空間不同,因此XSL中的模板匹配不起作用。使它們匹配或在樣式表中提供明確的名稱空間前綴。
  2. select-value將返回body元素的文本值,這可能不是您想要的值。

如果你想完成所有待輸出SVG部分作爲SVG文檔類型,然後執行以下操作:

  1. 谷歌「XSL恆等變換」,以瞭解如何做一個「深複製「從輸入到輸出。
  2. 添加一個<xsl:output ...>標記doctype-publicdoctype-system屬性,指定要輸出的文檔類型信息。

這是未經測試的,但應該非常接近。您必須添加doctype信息:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet 
    xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:svg= "http://www.w3.org/2000/svg" 
    version="2.0"> 

    <xsl:output method="xml" doctype-public="..." doctype-system="..."/> 

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

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

</xsl:stylesheet> 
+0

我應該將其轉換爲如下的svg文檔: <?xml version =「1.0」standalone =「no」?> <!DOCTYPE svg PUBLIC「 - // W3C // DTD SVG 1.1 // EN」 「 http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd「> Erick 2010-06-30 17:11:22

0

在這種情況下,身份轉換是無用的。我會嘗試:

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:svg= "http://www.w3.org/2000/svg" 
    xmlns:xhtml= "http://www.w3.org/1999/xhtml" 
    version="1.0"> 

    <xsl:output method="xml" doctype-public="-//W3C//DTD SVG 1.1//EN" doctype-system="http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" /> 

    <xsl:template match="/"> 
    <xsl:copy-of select="/*/xhtml:body//svg:svg"/>  
    </xsl:template> 

</xsl:stylesheet> 

編輯:如果你想美化事情有點:

<xsl:stylesheet 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:svg= "http://www.w3.org/2000/svg" 
      version="1.0"> 

    <xsl:output method="xml" doctype-public="-//W3C//DTD SVG 1.1//EN" doctype-system="http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" /> 

    <xsl:template match="text()"/> 

    <xsl:template match="svg:svg"> 
     <xsl:call-template name="svg"/> 
    </xsl:template> 

    <xsl:template match="svg:*" mode="svg" name="svg"> 
     <xsl:element name="{substring-after(name(),':')}" namespace="http://www.w3.org/2000/svg"> 
      <xsl:apply-templates select="@*|node()" mode="svg"/> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="@*" mode="svg"> 
     <xsl:copy/> 
    </xsl:template> 
</xsl:stylesheet> 

編輯2:Dimitre給我們帶來了一個有趣的問題。如果輸入結構不像所提供的結構呢? 一種情況:headbody中有文本節點。我編輯了兩個答案。 其他情況:SVG在一些XHTML標記內。我編輯了兩個答案。 最壞情況:有幾個svg元素。在這種情況下,您需要將每個svg元素都合併爲一個元素。

0

該轉化

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xhtml="http://www.w3.org/1999/xhtml" 
> 
<xsl:output omit-xml-declaration="yes" indent="yes" 
    doctype-public="-//W3C//DTD SVG 1.1//EN"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="*[not(descendant::xhtml:body)]"/> 

<xsl:template match="*[descendant::xhtml:body]"> 
    <xsl:apply-templates select="*"/> 
</xsl:template> 

<xsl:template match="xhtml:body" priority="20"> 
    <xsl:copy-of select="node()"/> 
</xsl:template> 
</xsl:stylesheet> 

當所提供的XML文檔施加:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:svg= "http://www.w3.org/2000/svg"> 
<body> 
<svg:svg width="500" height="560" version="1.1" > 

... 
... 

</svg:svg></body></html> 

產生想要的結果

<!DOCTYPE svg:svg PUBLIC "-//W3C//DTD SVG 1.1//EN"> 
<svg:svg width="500" height="560" version="1.1" xmlns="http://www.w3.org/1999/xhtml" xmlns:svg="http://www.w3.org/2000/svg"> 

... 
... 

</svg:svg>