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

时间:2010-10-28 16:09:39

标签: xml xslt

这个有点棘手。我有一个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 个答案:

答案 0 :(得分: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 :(得分:-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>

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