繁体   English   中英

使用XSLT递归加载相关XML文件并应用转换

[英]Using XSLT to recursively load relative XML files and apply transformation

我有一个XML文件,其结构如下所示:

<root>
<includes>
    <includeFile name="../other/some_xml.xml"/>
</includes> 
<itemlist>  
        <item id="1" >
            <selections>
                <selection name="one" />
            </selections>
        </item>
</itemlist>

xslt

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xslt">
    <xsl:output method="xml" indent="yes" xalan:indent-amount="4" />

    <xsl:template match="/">
        <xsl:element name="ItemList">
            <xsl:if test="root/item">
                <xsl:call-template name="templ" />
            </xsl:if>
        </xsl:element>
    </xsl:template>

    <xsl:template name="templ">
        <xsl:element name="ItemList">
            <xsl:for-each select="root/itemlist/item">

                <xsl:element name="Item">
                    <xsl:element name="ItemIdentifier">
                        <xsl:value-of select="@id" />
                    </xsl:element>
                    <xsl:element name="Condition">
                        <xsl:value-of select="selections/selection[1]/@name" />
                    </xsl:element>
                </xsl:element>

            </xsl:for-each>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

我创建了一个XSLT,用于过滤项目。 问题在于,对于每个文件,我都必须检查它是否包含includefile标记,这是指向类似xml的相对路径,如果包含,我还需要递归地从该文件中收集项目。 现在,我使用xslt转换了xml,然后必须解析xml以查找includefile标记。 该解决方案看起来并不优雅,我想知道是否可以通过xslt完成所有解决方案。

XSLT 1.0和XSLT 2.0中的XSLT document功能以及doc函数允许您提取其他文档,然后可以使用匹配的模板轻松进行处理。 因此,考虑将您的XSLT编码样式更改为编写匹配的模板和Apply-templates,那么您可以轻松地

<xsl:template match="includes/includeFile">
  <xsl:apply-templates select="document(@name)/*"/>
<xsl:template>

然后只需确保<xsl:template match="root">...</xsl:template>创建所需的输出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM