代码之家  ›  专栏  ›  技术社区  ›  JL. Hans Passant

使用XSLT的XML摘要

  •  0
  • JL. Hans Passant  · 技术社区  · 15 年前

    我有一个XML文件目录…例如:

       result.xml
       result2.xml
       result3.xml
       resultNth.xml (because there is an unknown amount of files)
    

    每个结果文件中的模式本质上是相同的

        <Succeeded>
              <someStrongXMLObject>
    
              </someStrongXMLObject>
        </Succeeded>
    

    我的问题是-我能在那个目录中再加一个名为results.xml的XML文件吗…以及一个将格式化结果的XSL文件…

    我要从所有结果文件创建摘要…这样所有这些文件中的数据都将包含在摘要中。

    这有可能吗?不使用任何C-只是标准XML、XSLT?

    更新

    我不希望有什么解决办法,只是点头,是的,可能的,或是一个响亮的否定。我需要实现一些C。另外,我希望目录结构是YYYY/MM/DD-这样我就可以有摘要XML文件,这将重现思想的子目录…创建月度和年度总结。

    1 回复  |  直到 15 年前
        1
  •  2
  •   Mads Hansen    15 年前

    是的,这是绝对可能的。

    它的简单程度取决于文件名和文件夹名模式的可靠性。你可以使用 document() function 检查结果文件是否存在。使用递归继续查找下一个按顺序命名的文件,直到到达末尾。

    从示例文件名中,可以使用以下XSLT:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
        <xsl:template match="/">
            <summary>
                <xsl:call-template name="getResults">
                    <!--First call, use empty index -->
                    <xsl:with-param name="index" />
                </xsl:call-template>
            </summary>
        </xsl:template>
    
        <xsl:template name="getResults">
            <xsl:param name="index" />
            <xsl:variable name="filename">
                <xsl:choose>
                    <xsl:when test="$index =''">
                        <xsl:value-of select="concat('result', '.xml')" />
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="concat('result', $index, '.xml')" />
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:variable>
            <xsl:choose>
                <!--Check for the presence of the file -->
                <xsl:when test="document($filename)">
                    <!--If it exists, then output something from the document-->
                    <Result>
                        <xsl:attribute name="href">
                            <xsl:value-of select="$filename" />
                        </xsl:attribute>
                        <xsl:copy-of select="document($filename)/Succeeded" />
                    </Result>
                    <!--Then, call the function with a new index value to look for the next file-->
                    <xsl:call-template name="getResults">
                        <xsl:with-param name="index">
                            <xsl:choose>
                                <!--If empty index, start at 1 -->
                                <xsl:when test="$index=''">
                                    <xsl:value-of select="1" />
                                </xsl:when>
                                <xsl:otherwise>
                                    <!--Add 1 to the previous index value -->
                                    <xsl:value-of select="number($index+1)" />
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:with-param>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:comment>Done looking for Results. Found <xsl:value-of select="number($index)" /> files.</xsl:comment>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
    
    </xsl:stylesheet>
    

    我在与xslt相同的文件夹(即result.xml、result1.xml、result2.xml)中运行了3个结果文档,并生成了以下输出:

    <?xml version="1.0" encoding="utf-8"?>
    <summary>
    <Result href="result.xml">
    <Succeeded>
            <someStrongXMLObject>
    
            </someStrongXMLObject>
        </Succeeded>
    </Result>
    <Result href="result1.xml">
    <Succeeded>
            <someStrongXMLObject>
    
            </someStrongXMLObject>
        </Succeeded>
    </Result>
    <Result href="result2.xml">
    <Succeeded>
            <someStrongXMLObject>
    
            </someStrongXMLObject>
        </Succeeded>
    </Result>
    <!--Done looking for Results. Found 3 files.-->
    </summary>