代码之家  ›  专栏  ›  技术社区  ›  Ace

更改XML文件中的文件扩展名

  •  2
  • Ace  · 技术社区  · 15 年前

    这个有点棘手。我有一个contents.xml文件,它引用了许多其他文件。这些其他文件以前是.xml,现在改为.dita,我的问题是如何将所有.xml文件扩展名重命名为.dita?文件路径是树中的不同级别,并且前面的子文件夹数目不一致。
    例子:

    <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>
    
    2 回复  |  直到 15 年前
        1
  •  1
  •   Dimitre Novatchev    15 年前

    这种转变 :

    <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>
    

    :

    <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>
    
        2
  •  -1
  •   Dimitre Novatchev    15 年前

    可以通过递归来完成此操作:

    <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>