2010-10-28 181 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