使用 XSLT 递归加载相关 XML 文件并应用转换 [英] Using XSLT to recursively load relative XML files and apply transformation

查看:27
本文介绍了使用 XSLT 递归加载相关 XML 文件并应用转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a xml file whose structure looks like this:

<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 以查找包含文件标记.这个解决方案看起来并不优雅,我想知道是否所有这些都可以通过 xslt 完成.

I created one XSLT which i am using to filter out items. The problem is that for every file, i have to check if it contains the includefile tag, which is a relative path pointing to a similar xml, and if it does, i need to collect items from that file also, recursively. For now i transformed the xml using my xslt and then i have to parse the xml to look for includefile tag. This solution doesn't look elegant and i was wondering if all of it could be done via xslt.

推荐答案

XSLT 1.0 和 XSLT 2.0 中的 XSLT document 函数另外还有 doc 函数允许你拉在进一步的文件中,可以使用匹配的模板简单地进行处理.所以考虑将你的 XSLT 编码风格移动到编写匹配模板和应用模板,然后你可以轻松做到

The XSLT document function in XSLT 1.0 and in XSLT 2.0 additionally the doc function allow you to pull in further documents, processing is then simply possible with matching templates. So consider to move your XSLT coding style to write matching templates and apply-templates, then you can easily do

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

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

and then you simply need to make sure the <xsl:template match="root">...</xsl:template> creates the output you want.

这篇关于使用 XSLT 递归加载相关 XML 文件并应用转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆