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

更改XML文件中的文件扩展名,xml,xslt,Xml,Xslt,这个有点棘手。我有一个contents.xml文件,它引用了大量其他文件。这些其他文件使用的是.xml,并已更改为.dita,我的问题是如何将所有.xml文件扩展名重命名为.dita?文件路径在树中的级别不同,前面的子文件夹数量不一致。 例如: 定义 视图组件 致: 定义 视图组件 您可以使用递归执行此操作: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

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


定义
视图组件
致:


定义
视图组件

您可以使用递归执行此操作:

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



底部的模板将所有输入直接复制到输出,但上面的模板拾取的属性除外,文本转换应用于此处。

您可以通过递归执行此操作:

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



底部的模板将所有输入直接复制到输出,上面的模板拾取的属性除外,文本转换应用于此处。

此转换

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

应用于提供的XML文档时

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

定义
视图组件
生成所需的正确结果

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

定义
视图组件

此转换:

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

应用于提供的XML文档时

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

定义
视图组件
生成所需的正确结果

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

定义
视图组件

问得好,+1。请参阅我的答案,以获得简短而完整的解决方案。:)我不明白为什么答案都是样式表。为什么不创建一个快速脚本来查找字符串“.xml”,并将其替换为“.dita”?@RyanJM:这可能是因为问题被标记为
xslt
?这可能是因为您可以有一个名为
this.xml
的元素吗?@RyanJM:只有在您不想用XSLT解决这个问题时,这个问题才可能是“棘手的”:)这解释了为什么有大量XSLT答案,以及为什么您可以在短时间内得到大量答案:)@RyanJM:您不应该将所有出现的“.xml”替换为“.dita”。也许“快速脚本”可以做到这一点,但这将是一个严重错误的“解决方案”。首先试着理解这个问题。好问题,+1。请参阅我的答案,以获得简短而完整的解决方案。:)我不明白为什么答案都是样式表。为什么不创建一个快速脚本来查找字符串“.xml”,并将其替换为“.dita”?@RyanJM:这可能是因为问题被标记为
xslt
?这可能是因为您可以有一个名为
this.xml
的元素吗?@RyanJM:只有在您不想用XSLT解决这个问题时,这个问题才可能是“棘手的”:)这解释了为什么有大量XSLT答案,以及为什么您可以在短时间内得到大量答案:)@RyanJM:您不应该将所有出现的“.xml”替换为“.dita”。也许“快速脚本”可以做到这一点,但这将是一个严重错误的“解决方案”。首先试着理解这个问题。你的答案和@Alejandro的答案几乎相同。两者都有点不正确(不够精确)。WTF?!?!?稍微不正确怎么办?我的样式表进行了要求的精确转换,我的响应首先发布。为什么我被否决了?!不是我投了你的反对票。我不知道你为什么被否决了。当你仅仅因为怀疑另一个人可能投了你的反对票就投了他的反对票时,这只会暴露出你的性格。我真的很讨厌这样的人。你的答案和@Alejandro的答案几乎一样。两者都有点不正确(不够精确)。WTF?!?!?稍微不正确怎么办?我的样式表进行了要求的精确转换,我的响应首先发布。为什么我被否决了?!不是我投了你的反对票。我不知道你为什么被否决了。当你仅仅因为怀疑另一个人可能投了你的反对票就投了他的反对票时,这只会暴露出你的性格。我真的很讨厌这样的人。