2010-10-28 186 views
2

這是一個棘手的問題。我有一個contents.xml文件引用其他文件的主機。這些其他文件使用.xml,並已被修改爲.dita,我的問題是我如何重命名所有的.xml文件擴展名爲.dita?文件路徑在樹中是不同的級別,並且前面的子文件夾數量不一致。
例子:更改XML文件內的文件擴展名

<article 
    xmlns:xi="http://www.w3.org/2001/XInclude"> 

    <title>Definition</title> 
    <xi:include href="./introduction.xml"/> 
    <section xml:id="viewComponents"><title>View Components</title> 
     <xi:include href="./components/page.xml"/> 
     <xi:include href="./views/sheet.xml"/> 
     <xi:include href="./folder/xsubfolders/plaque.xml"/> 
    </section> 
</article> 

要:

<article 
    xmlns:xi="http://www.w3.org/2001/XInclude"> 

    <title>Definition</title> 
    <xi:include href="./introduction.dita"/> 
    <section xml:id="viewComponents"><title>View Components</title> 
     <xi:include href="./components/page.dita"/> 
     <xi:include href="./views/sheet.dita"/> 
     <xi:include href="./folder/xsubfolders/plaque.dita"/> 
    </section> 
</article> 
+0

好問題,+1。看到我的答案是一個簡短而完整的解決方案。 :) – 2010-10-28 16:37:00

+0

我很困惑,爲什麼答案都是樣式表。爲什麼不創建一個快速腳本來查找字符串「.xml」並將其替換爲「.dita」? – RyanJM 2010-10-28 16:41:14

+0

@RyanJM:難道是因爲問題用'xslt'標記?難道是因爲你可以有一個名爲'this.xml'的元素? – 2010-10-28 16:45:40

回答

1

這種轉變

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

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

<xsl:template match= 
    "xi:include/@href[substring(., string-length()-3)='.xml']"> 
    <xsl:attribute name="href"> 
    <xsl:value-of select="concat(substring(.,1, string-length()-3),'dita')"/> 
    </xsl:attribute> 
</xsl:template> 
</xsl:stylesheet> 

時所提供的XML文檔應用:

<article 
    xmlns:xi="http://www.w3.org/2001/XInclude"> 

    <title>Definition</title> 
    <xi:include href="./introduction.xml"/> 
    <section xml:id="viewComponents"><title>View Components</title> 
     <xi:include href="./components/page.xml"/> 
     <xi:include href="./views/sheet.xml"/> 
     <xi:include href="./folder/xsubfolders/plaque.xml"/> 
    </section> 
</article> 

產生想要的,正確的結果

<article xmlns:xi="http://www.w3.org/2001/XInclude"> 
    <title>Definition</title> 
    <xi:include href="./introduction.dita"></xi:include> 
    <section xml:id="viewComponents"> 
     <title>View Components</title> 
     <xi:include href="./components/page.dita"></xi:include> 
     <xi:include href="./views/sheet.dita"></xi:include> 
     <xi:include href="./folder/xsubfolders/plaque.dita"></xi:include> 
    </section> 
</article> 
-1

你可以用遞歸做到這一點:

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


    <xsl:template match="@*"> 
     <xsl:choose> 
      <xsl:when test="substring(string(.), string-length(string(.)) - 3) = '.xml'"> 
       <xsl:attribute name="{name()}"> 
        <xsl:value-of select="concat(substring(string(.), 1, string-length(string(.)) - 4), '.dita')"/> 
       </xsl:attribute> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:attribute name="{name()}"><xsl:value-of select="string(.)"/></xsl:attribute> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 

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

底部將模板複製所有直接輸入到輸出除了被拾取的屬性外上面的模板和文本轉換應用在這裏。

+0

您的答案几乎與@Alejandro相同。兩者都有些不正確(不夠精確)。 – 2010-10-28 16:41:54

+0

WTF?!?!?如何不正確?我的樣式表完成了被要求的確切轉換,並且我的回覆被首先發布。爲什麼我爲此而低估?! – Robin 2010-10-28 18:43:50

+0

不是我,誰低估了你。我不知道你爲什麼被低估。當你基於懷疑他可能低估了你而低估了另一個人時,這隻會暴露你的性格。我真的很愛這樣的人。 – 2010-10-28 19:24:14