2013-04-04 61 views
1

我們有一堆html頁面的文件,其中包含其他xml元素(全部以我們的公司名稱'TLA'作爲前綴),以便爲舊程序提供數據和結構,我現在正在重寫。複製名稱空間的所有元素,但別無其他

實施例形式:

<html > 
<head> 
    <title>Highly Simplified Example Form</title> 
</head> 
<body> 
    <TLA:document xmlns:TLA="http://www.tla.com"> 
     <TLA:contexts> 
      <TLA:context id="id_1" value=""></TLA:context> 
     </TLA:contexts> 
     <TLA:page> 
      <TLA:question id="q_id_1"> 
       <table> 
        <tr> 
         <td> 
          <input id="input_id_1" type="text" /> 
         </td> 
        </tr> 
       </table> 
      </TLA:question> 
     </TLA:page> 
     <!-- Repeat many times --> 
    </TLA:document> 
</body> 
</html> 

我的任務是寫一個預處理器,將提取 'TLA' 的所有元素,並忽略html元素

希望的XML輸出:

<?xml version="1.0" encoding="utf-8" ?> 
<TLA:document xmlns:TLA="http://www.tla.com"> 
    <TLA:contexts> 
     <TLA:context id="id_1" value=""></TLA:context> 
    </TLA:contexts> 
    <TLA:page> 
     <TLA:question id="q_id_1"> 
     </TLA:question> 
    </TLA:page> 
    <!-- Repeat many times --> 
</TLA:document> 

這應該是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" exclude-result-prefixes="msxsl" 
    xmlns:tla="http://www.tla.com" 
> 
    <xsl:output method="xml" indent="yes"/> 

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

這是我提取想要的元素(但不包括它們的屬性!),而且提取的HTML元素的文本屬性和內容。我如何排除HTML元素及其內容?

+0

您不會收到關於未綁定TLA前綴的錯誤? – 2013-04-04 14:23:16

+0

@DanielHaley否 - 但我沒有正確剪切和粘貼xslt - 現在已更新 – 2013-04-04 14:36:08

+0

您仍然沒有在輸入文檔中爲該TLA前綴綁定'xmlns:TLA =「...」'。如果沒有這個名稱空間,那麼使用XSLT就很難從這樣的文檔中獲取任何明智的東西。 – 2013-04-04 14:38:42

回答

3

這應做到:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:tla="http://www.tla.com"> 
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" /> 
    <xsl:strip-space elements="*" /> 

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

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

當你的樣品輸入運行(一旦丟失命名空間聲明被添加),結果是:

<TLA:document xmlns:TLA="http://www.tla.com"> 
    <TLA:contexts> 
    <TLA:context id="id_1" value="" /> 
    </TLA:contexts> 
    <TLA:page> 
    <TLA:question id="q_id_1" /> 
    </TLA:page> 
</TLA:document> 
2

你可以嘗試這樣的事情......

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:tla="http://www.tla.com" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 
    <xsl:strip-space elements="*"/> 

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

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

</xsl:stylesheet> 
相關問題