2016-09-15 48 views
1

請建議獲取通過xslt集合收集的每個文檔的絕對路徑。如何從集合中獲取文件的絕對文檔路徑

發佈腳本能夠得到所需絕對路徑,但我已經用了兩個集合(它可能冒不必要的內存來存儲所有文章的信息兩次,一次採集,收集信息等一個收集document-uri() S)。

個XML:

d:/DocumentPath/Project-01/2016/ABC/Test.xml

<article> 
    <title>First article</title> 
    <tag1>The tag 1</tag1> 
    <tag3>The tag 3</tag3> 
</article> 

d:/ DocumentPath /項目-01/2016/DEF /測試。 XML

<article> 
    <title>Second article</title> 
    <tag2>The tag 2</tag2> 
    <tag3>The tag 3</tag3> 
</article> 

等個XML ....

XSLT 2.0:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/> 

<xsl:variable name="varDocuments"> 
    <xsl:copy-of select="collection('file:///D:/DocumentPath/Project-01/2016/?select=*.xml;recurse=yes') 
     [matches(document-uri(.), '2016/([A-z]+)/.*?.xml')]"/> 
</xsl:variable> 

<xsl:variable name="varDocuments1"> 
    <xsl:copy-of select="collection('file:///D:/DocumentPath/Project-01/2016/?select=*.xml;recurse=yes') 
     [matches(document-uri(.), '2016/([A-z]+)/.*?.xml')]/document-uri(.)"/> 
</xsl:variable> 

<xsl:template match="@*|node()"> 
    <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy> 
</xsl:template> 

<xsl:template match="/"> 
    <Table border="1"> 
     <TR><TH>Position</TH><TH>Title</TH><TH>Tag1</TH><TH>Tag2</TH><TH>Tag3</TH><TH>Tag4</TH><TH>Path</TH></TR> 
     <xsl:for-each select="$varDocuments"> 
      <xsl:for-each select="article"> 
       <TR> 
        <xsl:variable name="varPos" select="position()"/> 
        <td><xsl:value-of select="position()"/></td> 
        <td><xsl:value-of select="title"/></td> 
        <td><xsl:value-of select="count(descendant::tag1)"/></td> 
        <td><xsl:value-of select="count(descendant::tag2)"/></td> 
        <td><xsl:value-of select="count(descendant::tag3)"/></td> 
        <td><xsl:value-of select="count(descendant::tag4)"/></td> 
        <td><xsl:value-of select="normalize-space(tokenize($varDocuments1, 'file:/')[position()=$varPos + 1])"/></td> 
       </TR> 
      </xsl:for-each> 
     </xsl:for-each> 
    </Table> 
</xsl:template> 

</xsl:stylesheet> 

要求的結果:

<Table border="1"> 
 
    <TR> 
 
     <TH>Position</TH> 
 
     <TH>Title</TH> 
 
     <TH>Tag1</TH> 
 
     <TH>Tag2</TH> 
 
     <TH>Tag3</TH> 
 
     <TH>Tag4</TH> 
 
     <TH>Path</TH> 
 
    </TR> 
 
    <TR> 
 
     <td>1</td> 
 
     <td>First article</td> 
 
     <td>1</td> 
 
     <td>0</td> 
 
     <td>1</td> 
 
     <td>0</td> 
 
     <td>D:/DocumentPath/Project-01/2016/ABC/Test.xml</td> 
 
    </TR> 
 
    <TR> 
 
     <td>2</td> 
 
     <td>Second article</td> 
 
     <td>0</td> 
 
     <td>1</td> 
 
     <td>1</td> 
 
     <td>0</td> 
 
     <td>D:/DocumentPath/Project-01/2016/DEF/Test.xml</td> 
 
    </TR> 
 
    <TR> 
 
     <td>3</td> 
 
     <td>Third article</td> 
 
     <td>1</td> 
 
     <td>0</td> 
 
     <td>0</td> 
 
     <td>2</td> 
 
     <td>D:/DocumentPath/Project-01/2016/GHI/Test.xml</td> 
 
    </TR> 
 
</Table>

回答

2

我首先建議修改

<xsl:variable name="varDocuments"> 
    <xsl:copy-of select="collection('file:///D:/DocumentPath/Project-01/2016/?select=*.xml;recurse=yes') 
     [matches(document-uri(.), '2016/([A-z]+)/.*?.xml')]"/> 
</xsl:variable> 

至少

<xsl:variable name="varDocuments" select="collection('file:///D:/DocumentPath/Project-01/2016/?select=*.xml;recurse=yes') 
     [matches(document-uri(.), '2016/([A-z]+)/.*?.xml')]"/> 

因爲似乎沒有成爲一個需要與collection文件拉,然後用copy-of創建一個額外的副本。

通過該更正,當您使用<xsl:for-each select="$varDocuments">處理每個文檔時,您現在可以簡單地在那裏讀出document-uri(.),因爲您正在處理拉入的文檔,並且沒有組裝任何副本。

+0

謝謝你的完美建議,先生,得到所需的絕對路徑,加上一個。 –

相關問題