【问题标题】:Looping through XML elements using XSLT and calling another xsl使用 XSLT 循环遍历 XML 元素并调用另一个 xsl
【发布时间】:2014-02-19 07:40:34
【问题描述】:

我正在使用 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>

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

目前,我无法访问 AlertType 标记的直接子项,更不用说匹配它了。任何人都可以帮助如何访问子节点吗?此外,根据匹配结果(用于每个),我想调用不同的 xslts。知道怎么做吗?

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

HTML 应该是这样的:

信用监控活动

AlertTypeOne
姓名 地址 市州

数据数据数据数据

AlertTypeTwo

资格指定角色

数据数据数据

AlertTypeThree

贷记借记

数据数据

【问题讨论】:

  • 这个问题太模糊了。请展示一个代表性 XML 输入示例(一个没有&lt;!--Some info--&gt;,但包含您要检索的实际元素)和您的预期 XML 输出。
  • 已编辑。基本上,我有不同的 xsl 文件来处理 xml,我需要根据循环期间遇到的标签名称调用适当的 xsl 文件。我想将所有 xml 转换成一个 HTML。
  • 那么,为什么不显示那些不同的 XSLT 文件呢?
  • 我需要将所有数据合并到一个文件中。
  • "我想显示一个包含多个表格的 HTML 文件(每个警报一个表格)。" 请向我们展示一个包含多个(至少两个)警报的 XML 文档, 包括它们的内容。然后解释如何根据警报内容构建每个表 - 最好通过显示您希望获得的结果 HTML。

标签: xml xslt xslt-1.0 transformation


【解决方案1】:

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

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

在您的情况下,没有必要使用xsl:for-each。而是为输入 XML 中的元素编写单独的模板。例如,从以下开始:

编辑

作为对您编辑的回应:

我想显示一个包含多个表格的 HTML 文件(每个警报一个表格)。

然后,在匹配单个警报的模板中包含&lt;table&gt;(我已经编辑了代码)。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="html" indent="yes"/>

    <xsl:template match="/">
        <html>
            <body>
                <h1>Alerts</h1>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>

然后,为AlertsAlertType等编写模板:

<xsl:template match="Alerts|AlertType">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="AlertTypeOne">
    <table>
       <!--Decide what to do with this element here-->
    </table>
</xsl:template>

很遗憾,您没有透露您希望输出 HTML 的样子。

【讨论】:

  • 添加了 HTML 所需的详细信息。另外,我是 XSL 的新手。在我的 xml 中,可能存在多次出现的 标记。应用模板会处理每个 标签吗?
  • @DarshanMehta 你一直用单词来描述你需要的东西,而不是简单地显示HTML输出,这很烦人。否则,我能做的就是胡乱猜测。
  • 是的,模板负责处理元素的所有出现。
  • 我试图解释 HTML 输出。请检查已编辑的问题。
  • 正如我所说:解释它无济于事。此外,您确实 not 将 HTML 添加到您的问题中,这只是人们可能认为的浏览器输出(而您需要显示 source HTML)。
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 2018-06-05
    相关资源
    最近更新 更多