打印参数或变量,仅取决于文件的路径一次

时间:2013-10-03 10:50:27

标签: xslt

如果标题太模糊,请提前抱歉。

我必须使用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的新手,所以我不知道该怎么做。

编辑:

当我发现错误时,我需要显示文件的路径,但在找到第一个错误后只需显示一次。 错误只能在带有图元素的文件中找到。

我希望这能澄清我的问题。

1 个答案:

答案 0 :(得分:0)

您可以使用以下内容测试当前元素是否与XML中的上一个元素匹配:

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

编辑:此测试只能解决问题的一半。您需要另一个测试来检查另一个文件中是否有错误。

相关问题