使用XSLT循环遍历XML元素并调用另一个xsl

使用XSLT循环遍历XML元素并调用另一个xsl,xml,xslt,xslt-1.0,transformation,Xml,Xslt,Xslt 1.0,Transformation,我使用XSL转换遍历不同的XML元素。以下是我的示例xml: <?xml version="1.0" encoding="UTF-8"?> <Alerts> <AlertType> <AlertTypeOne> <NewAlert></NewAlert> </AlertTypeOne>

我使用XSL转换遍历不同的XML元素。以下是我的示例xml:

<?xml version="1.0" encoding="UTF-8"?>    
<Alerts>    
      <AlertType>
         <AlertTypeOne>
            <NewAlert></NewAlert>
         </AlertTypeOne>                
      </AlertType>

      <AlertType>
         <AlertTypeTwo>
            <NewerAlert></NewerAlert>
         </AlertTypeTwo>            
      </AlertType>

      <AlertType>
         <AlertTypeThree>
            <OldAlert></OldAlert>
         </AlertTypeThree>              
      </AlertType>
</Alerts>

以下是我的xslt:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <html>
            <body>
                <h1>Alerts</h1>
                <table>
                    <tr>
                        <td>Credit Monitoring Activity</td>
                    </tr>
                    <tr>
                        <td>Lorem ipsum <br/>
                            <xsl:for-each select="//Alerts/AlertType">
                                <!--logic -->
                            </xsl:for-each></td>
                    </tr>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

警报
信贷监测活动
Lorem ipsum
我正在遍历不同的AlertType标记。AlertType标记可能包含不同的xml(彼此不相关),基于标记中的第一个元素,我想调用适当的xsl文件并将xml内容传递给它(就像java中的方法调用一样)

目前,我无法访问AlertType标记的直接子级,更不用说匹配它了。任何人都可以提供有关如何访问子节点的帮助吗?另外,基于匹配结果(使用for each),我想调用不同的XSLT。知道怎么做吗

编辑 由于xml包含不同警报的信息,我想显示一个HTML文件,其中包含多个表(每个警报一个表)。 根据标记中的不同数据,列的名称和值可能不同

这是HTML的外观:

信贷监测活动

AlertTypeOne
姓名地址城市州

数据数据

警报类型二

资格指定角色

数据数据

警报类型三

借贷


数据数据

您询问的是您的解决方案,而不是确定您试图解决的问题(请参阅meta上的“XY”帖子)

在许多地方,
xsl:for-each
是不合适的,并且它经常被具有过程背景的程序员误用。相反,您应该采用功能范式。使用
xsl:template
xsl:apply-templates
被认为是一种功能性方法

在您的情况下,没有必要为每个使用
xsl:for。相反,为输入XML中的元素编写单独的模板。例如,从以下内容开始:

编辑

作为对您的编辑的回应:

我想显示一个由多个表组成的HTML文件(每个警报一个表)

然后,将
包含在与单个警报匹配的模板中(我已经编辑了代码)

不幸的是,您没有透露您想要输出的HTML是什么样子的

基于标记中的第一个元素,我想调用适当的xsl 文件

我认为您不需要单独的XSL文件;您可以根据不同的AlertType元素的子元素,将不同的模板(在同一样式表中)应用于不同的AlertType元素。下面的样式表将为每个AlertType元素创建一个表,然后应用适当的模板来构造表的结构(和内容,如果我们有):


警报
名称
地址
城市
陈述
资格
任命
角色
信用
借记
应用于示例输入:

<?xml version="1.0" encoding="UTF-8"?>    
<Alerts>    
      <AlertType>
         <AlertTypeOne>
            <NewAlert></NewAlert>
         </AlertTypeOne>                
      </AlertType>

      <AlertType>
         <AlertTypeTwo>
            <NewerAlert></NewerAlert>
         </AlertTypeTwo>            
      </AlertType>

      <AlertType>
         <AlertTypeThree>
            <OldAlert></OldAlert>
         </AlertTypeThree>              
      </AlertType>
</Alerts>

接收到以下输出:

<?xml version="1.0" encoding="utf-8"?>
<html>
   <body>
      <h1>Alerts</h1>
      <table>
         <tr>
            <th>Name</th>
            <th>Address</th>
            <th>City</th>
            <th>State</th>
         </tr>
         <tr>
            <td/>
            <td/>
            <td/>
            <td/>
         </tr>                
      </table>
      <table>
         <tr>
            <th>Qualification</th>
            <th>Designation</th>
            <th>Role</th>
         </tr>
         <tr>
            <td/>
            <td/>
            <td/>
         </tr>            
      </table>
      <table>
         <tr>
            <th>Credit</th>
            <th>Debit</th>
         </tr>
         <tr>
            <td/>
            <td/>
         </tr>              
      </table>
   </body>
</html>

警报
名称
地址
城市
陈述
资格
任命
角色
信用
借记

这个问题太模糊了。请显示一个具有代表性的XML输入示例(一个没有
,但包含您要检索的实际元素)和您预期的XML输出。已编辑。基本上,我有不同的xsl文件要处理,xmls,我需要根据我在循环过程中遇到的标记的名称调用相应的xsl文件。我想将所有XML转换为单个HTML。那么,为什么不显示这些不同的XSLT文件呢?我需要将所有数据合并为单个文件。“我想显示一个包含多个表(每个警报一个表)的HTML文件。”请向我们显示一个包含多个(至少两个)警报的XML文档,包括其内容。然后解释如何根据警报内容构造每个表-最好显示希望得到的结果HTML。添加了有关HTML所需的详细信息。另外,我是XSL的新手。在我的xml中,可能会出现多次标记。apply templates会处理每个标记吗?@DarshanMehta你总是用文字描述你需要什么,而不是简单地显示HTML输出,这真是令人讨厌。否则,我所能做的就是胡乱猜测。是的,模板会处理元素的所有出现。我已经尝试解释HTML输出。请检查编辑的问题。正如我所说:解释它不会有帮助。此外,您没有将HTML添加到问题中,而只是将其视为浏览器输出(而您需要这样做)
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="/">
<html>
<body>
    <h1>Alerts</h1>
    <xsl:for-each select="Alerts/AlertType">
        <table>
            <xsl:apply-templates/>
        </table>
    </xsl:for-each>
</body>
</html>
</xsl:template>

<xsl:template match="AlertTypeOne">
    <tr>
        <th>Name</th>
        <th>Address</th>
        <th>City</th>
        <th>State</th>
    </tr>
    <tr>
        <td><!--logic --></td>
        <td><!--logic --></td>
        <td><!--logic --></td>
        <td><!--logic --></td>
    </tr>
</xsl:template>

<xsl:template match="AlertTypeTwo">
    <tr>
        <th>Qualification</th>
        <th>Designation</th>
        <th>Role</th>
    </tr>
    <tr>
        <td><!--logic --></td>
        <td><!--logic --></td>
        <td><!--logic --></td>
    </tr>
</xsl:template>

<xsl:template match="AlertTypeThree">
    <tr>
        <th>Credit</th>
        <th>Debit</th>
    </tr>
    <tr>
        <td><!--logic --></td>
        <td><!--logic --></td>
    </tr>
</xsl:template>

</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>    
<Alerts>    
      <AlertType>
         <AlertTypeOne>
            <NewAlert></NewAlert>
         </AlertTypeOne>                
      </AlertType>

      <AlertType>
         <AlertTypeTwo>
            <NewerAlert></NewerAlert>
         </AlertTypeTwo>            
      </AlertType>

      <AlertType>
         <AlertTypeThree>
            <OldAlert></OldAlert>
         </AlertTypeThree>              
      </AlertType>
</Alerts>
<?xml version="1.0" encoding="utf-8"?>
<html>
   <body>
      <h1>Alerts</h1>
      <table>
         <tr>
            <th>Name</th>
            <th>Address</th>
            <th>City</th>
            <th>State</th>
         </tr>
         <tr>
            <td/>
            <td/>
            <td/>
            <td/>
         </tr>                
      </table>
      <table>
         <tr>
            <th>Qualification</th>
            <th>Designation</th>
            <th>Role</th>
         </tr>
         <tr>
            <td/>
            <td/>
            <td/>
         </tr>            
      </table>
      <table>
         <tr>
            <th>Credit</th>
            <th>Debit</th>
         </tr>
         <tr>
            <td/>
            <td/>
         </tr>              
      </table>
   </body>
</html>