2017-03-09 52 views
1

「我要重命名的XML文件。像old.xml與XSLT new.xml但我的代碼不能正常工作,請檢查。如何使用xslt重命名XML文件。我的代碼是

<xsl:for-each select="document('file:///d:/123/xslt_path.html')//@path"> 
    <xsl:variable name="aa" select="."/> 
    <xsl:variable name="aaa" select="concat('file:///', $aa)"/> 
    <xsl:variable name="ajeet_1" select="iri-to-uri(replace($aaa, '\\', '/'))"/> 
    <xsl:variable name="ajeet_coll" select="collection(concat($ajeet_1, '/?select=*.xml;recurse=yes'))"/> 
    <xsl:for-each select="$ajeet_coll"> 
    <xsl:if test="//citation=''"> 
     <xsl:variable name="hhhh" select="'file:///d:/123/list_of_files.html'"/> 
     <xsl:result-document href="{$hhhh}"> 
     <xsl:value-of select="replace(tokenize(document-uri(.), '/')[last()], '%20', ' ')"/> 
     <xsl:text>Citation Tag Empty</xsl:text> 
     </xsl:result-document> 
    </xsl:if> 
    <xsl:if test="//citation"> 
     <xsl:variable name="aa1" select="node()"/> 
     <xsl:variable name="jaanam" select="replace(replace($aa1, '([^&lt;&gt;]+) : ([^&lt;&gt;]+)', '$1'), ' ', '_')"/> 
     <xsl:variable name="jaanam1" select="concat(replace(tokenize(document-uri(.), '/')[last()], '%20', ' '), '')"/> 
     <xsl:copy-of select="document($jaanam)"/> 
    </xsl:if> 
    </xsl:for-each> 
</xsl:for-each> 
+1

提供關於在什麼包含更多細節 「文件:/// d:/123/xslt_path.html」 和你包含了什麼 「文件:/// d:/123/list_of_files.html」 – Rupesh

+0

xslt_path .html: - d:\ 2017 \ working \ 003_March \ 2017_03_08 file:/// d:/123/list_of_files.html 寫錯誤 –

+0

XSLT雖然可能是一種專用語言,一件事:轉換XML內容。考慮一種通用語言(Java,PHP,Python),它可以更輕鬆地迭代和運行操作系統文件系統更改,如重命名文件。 – Parfait

回答

1
<?xml version="1.0"?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml"/> 
<xsl:template match="/"> 
<xsl:for-each select="document('file:///d:/xslt_path.html')//@path"> 
<xsl:variable name="ajeet_1" select="iri-to-uri(concat('file:///', replace(., '\\', '/')))"/> 
<xsl:variable name="ajeet_coll" select="collection(concat($ajeet_1, '/?select=*.xml;recurse=yes'))"/> 
<xsl:variable name="hhhh" select="'file:///d:/list_of_files.html'"/> 
<xsl:result-document href="{$hhhh}"> 
<xsl:for-each select="$ajeet_coll"> 
<xsl:choose> 
<xsl:when test="//citation=''"> 
<xsl:text>Citation Tag Empty : </xsl:text> 
<xsl:value-of select="replace(tokenize(document-uri(.), '/')[last()], '%20', ' ')"/> 
<xsl:text>&#xa; </xsl:text> 
</xsl:when> 
<xsl:otherwise> 
<xsl:if test="//citation!=''"> 
<xsl:variable name="aa1" select="node()"/> 
<xsl:variable name="citation" select="//citation/text()"/> 
<xsl:variable name="jaanam"> 
<xsl:value-of select="tokenize(document-uri(.), '/')[position() ne last()]" separator="/"/> 
<xsl:value-of select="concat('/', replace(normalize-space(replace($citation, '([^:]+)', '$1')), ' ', '_'), '.xml')"/> 
</xsl:variable> 
<xsl:result-document href="{$jaanam}"> 
<xsl:copy-of select="$aa1"/> 
</xsl:result-document> 
</xsl:if> 
</xsl:otherwise> 
</xsl:choose> 
</xsl:for-each> 
</xsl:result-document> 
</xsl:for-each> 
</xsl:template> 
</xsl:stylesheet> 
0

你不能在XSLT重命名文件,但您可以創建新文件按您的要求

改變這種

<xsl:if test="//citation=''"> 
    <xsl:variable name="hhhh" select="'file:///d:/123/list_of_files.html'"/> 
    <xsl:result-document href="{$hhhh}"> 
    <xsl:value-of select="replace(tokenize(document-uri(.), '/')[last()], '%20', ' ')"/> 
    <xsl:text>Citation Tag Empty</xsl:text> 
    </xsl:result-document> 
</xsl:if> 

<xsl:variable name="hhhh" select="'file:///d:/123/list_of_files.html'"/> 
      <xsl:result-document href="{$hhhh}"> 
       <xsl:for-each select="$ajeet_coll"> 
        <xsl:if test="//citation=''"> 
         <xsl:value-of select="replace(tokenize(document-uri(.), '/')[last()], '%20', ' ')"/> 
         <xsl:text>Citation Tag Empty</xsl:text> 
        </xsl:if> 
       </xsl:for-each> 
      </xsl:result-document> 

,因爲它可能導致錯誤引用的重複。

和改變這種

<xsl:if test="//citation"> 
     <xsl:variable name="aa1" select="node()"/> 
     <xsl:variable name="jaanam" select="replace(replace($aa1, '([^&lt;&gt;]+) : ([^&lt;&gt;]+)', '$1'), ' ', '_')"/> 
     <xsl:variable name="jaanam1" select="concat(replace(tokenize(document-uri(.), '/')[last()], '%20', ' '), '')"/> 
     <xsl:copy-of select="document($jaanam)"/> 
    </xsl:if> 

<xsl:for-each select="$ajeet_coll"> 
     <xsl:if test="//citation!=''"> 
      <xsl:variable name="aa1" select="node()"/> 
      <xsl:variable name="jaanam" select="replace(replace($aa1, '([^&lt;&gt;]+) : ([^&lt;&gt;]+)', '$1'), ' ', '_')"/> 
      <xsl:variable name="jaanam1" select="concat(replace(tokenize(document-uri(.), '/')[last()], '%20', ' '), '')"/> 
      <xsl:result-document href="{$jaanam1}"> 
      <xsl:copy-of select="$aa1"/> 
      </xsl:result-document> 
     </xsl:if> 
    </xsl:for-each> 

它可能具有一些改變解決您的問題。