2013-10-03 50 views
0

如果標題過於模糊,則提前很抱歉。僅根據文件路徑打印參數或變量一次

我必須使用XSLT處理多個相互引用的XML文件並搜索某些錯誤。

我個XML通常是這樣的:

<topic> 
    ... some elements ... 
    <topicref @href="path-to-another-file"/> 
    ... some other elements ... 
    <figure> ... </figure> 
</topic> 

而我所需的輸出是:

path-to-a-file: 
    Errors found 

path-to-another-file: 
    Other errors found 

我從HREF屬性得到的路徑,我想打印的路徑,如果有一個錯誤在相應的文件中。

我的XSLT的重要組成部分:

<!-- ingress referenced files --> 
<xsl:template match="//*[@href]"> 
    <xsl:apply-templates select="document(@href)/*"> 
     <xsl:with-param name="path" select="@href"/> 
    </xsl:apply-templates> 
    <xsl:apply-templates select="./*[@href]"> 
    </xsl:apply-templates>    
</xsl:template> 

<!-- matching topic elements to check --> 
<xsl:template match="topic"> 
    <xsl:param name="path"/> 
    <xsl:if test=".//figure"> 
     <!-- Right now this is where I print the path of the current file --> 
     <xsl:value-of select="$path"/> 
    </xsl:if> 
    <xsl:apply-templates select="figure"> 
     <xsl:with-param name="path" select="$path"/> 
    </xsl:apply-templates> 
</xsl:template> 

<!-- check if there's any error --> 
<xsl:template match="figure"> 
    <xsl:param name="path"/> 

    <xsl:if test="..."> 
     <xsl:call-template name="printError"> 
      <xsl:with-param name="errorText" select="'...'"/> 
      <xsl:with-param name="filePath" select="..."/> 
      <xsl:with-param name="elementId" select="..."/> 
     </xsl:call-template> 
    </xsl:if> 
    <xsl:if test="..."> 
     <xsl:call-template name="printError"> 
      <xsl:with-param name="errorText" select="'...'"/> 
      <xsl:with-param name="filePath" select="..."/> 
      <xsl:with-param name="elementId" select="..."/> 
     </xsl:call-template> 
    </xsl:if> 
</xsl:template> 

<!-- print error message --> 
<xsl:template name="printError"> 
    <xsl:param name="errorText"/> 
    <xsl:param name="filePath"/> 
    <xsl:param name="elementId"/> 

    ... print out some stuff ... 

</xsl:template> 

我應該在哪裏打印出文件的路徑?有了這個轉換,我總是寫出來,如果文件有一個數字元素,即使它不包含任何錯誤。 像這樣:

path-to-a-file: 
    Errors found 

path-to-file-with-no-errors: 

path-to-another-file: 
    Other errors found 

如果我把一部分有問題的其他地方(即在錯誤檢查或印刷模板)它會打印檢查每一個人物的元素或錯誤後打印。

我認爲問題可以通過變量來解決,但我是XSLT的新手,所以我不確定如何去解決這個問題。

編輯:

我需要時,我在那裏找到一個錯誤,但只有一次,以顯示該文件的路徑,被發現的第一個錯誤之後。 錯誤只能在具有圖形元素的文件中找到。

我希望這可以澄清我的問題。

<variable name='currentlocation' select="@href"/> 
<xsl:if test="not(preceding-sibling::topicref[contains(@href, $currentlocation)])"> 

(process this only if there is no preceding sibling with the same @href as the current location) 

編輯:

+0

看完你的問題後,仍然不明白你想要做什麼? – NullPointer

+0

我編輯了這個問題,我希望這個問題很清楚。 – gombost

+0

<! - 現在這是我打印當前文件的路徑 - >是打印文件路徑的地方,對吧? – NullPointer

回答

0

如果當前元素在XML以前的元素相匹配,使用像您可以測試這個測試只能解決一半的問題。您需要另一個測試來檢查其他文件中是否有錯誤。