Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将样式化应用于动态XML_Xml_Xslt - Fatal编程技术网

将样式化应用于动态XML

将样式化应用于动态XML,xml,xslt,Xml,Xslt,我不熟悉XML和XSL 创建一个XML,其内容可以是动态的,即在某些情况下,某些元素可能不存在,我想将其输出为HTML表,如何编写这样的XSL 示例XML,在某些情况下配置元素可能不完全存在 <Stat> <Name></Name> <Hardware></Hardware> <Software></Version> <Vers

我不熟悉XML和XSL 创建一个XML,其内容可以是动态的,即在某些情况下,某些元素可能不存在,我想将其输出为HTML表,如何编写这样的XSL

示例XML,在某些情况下配置元素可能不完全存在

    <Stat>
         <Name></Name>
         <Hardware></Hardware>
         <Software></Version>
         <Version></Version>
         <State></State>
    </Stat>
    <Configuration>
     <Up>
        <Option1>2000</Option1>
        <Option2>2500000</Option2>
        <Option3>0</Option3>
        <Option4>0</Option4>
        <Option5>NA</Option5>
     </Uplink>
     <Down>
        <Option1>2000</Option1>
        <Option2>2500000</Option2>
        <Option3>0</Option3>
        <Option4>0</Option4>
        <Option5>NA</Option5>
      </Down>
   </Configuration>

2000
2500000
0
0
NA
2000
2500000
0
0
NA
预期产出: 具有行和列跨度的HTML表格, 是否有某种递归解决方案可以自动转到每个XML元素并将其放在表头中并选择相应的值?

假设您的XML格式良好,并且有一个根元素,那么您对标题的处理方式如下

 <th colspan="{count(Stat/*)}">
     <xsl:if test="Configuration[*]">
         <xsl:attribute name="rowspan">2</xsl:attribute>
         <xsl:text>Stat</xsl:text>
     </xsl:if>
</th>

2.
斯达
这将输出“Stat”标题的标题单元格。请注意,在执行colspan时使用了“属性值模板”。大括号表示要计算的表达式,而不是字面上的输出

xsl:if测试配置项是否存在,如果存在,则添加行跨度

类似地,“配置”的标题也是这样

<xsl:if test="Configuration[*]">
   <th colspan="{count(Configuration/*/*)}">
       <xsl:text>Configuration</xsl:text>
   </th>
</xsl:if>

配置
对于包含“UP”和“DOWN”的标题行,您可以迭代配置的子元素

<tr>
   <xsl:for-each select="Configuration/*">
       <th colspan="{count(*)}">
            <xsl:value-of select="local-name()" />
       </th>
   </xsl:for-each>
</tr>
<xsl:apply-templates select="Stat/*" mode="header" />
<xsl:apply-templates select="Configuration/*/*" mode="header" />

做下一行,只需要选择“Stat”的子元素和“Configuration”的子元素


试试这个XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output omit-xml-declaration="yes" indent="yes" />
   <xsl:template match="/*">
      <table>
         <tr>
            <th colspan="{count(Stat/*)}">
               <xsl:if test="Configuration[*]">
                  <xsl:attribute name="rowspan">2</xsl:attribute>
                  <xsl:text>Stat</xsl:text>
               </xsl:if>
            </th>
            <xsl:if test="Configuration[*]">
               <th colspan="{count(Configuration/*/*)}">
                  <xsl:text>Configuration</xsl:text>
               </th>
            </xsl:if>
         </tr>
         <tr>
             <xsl:for-each select="Configuration/*">
                <th colspan="{count(*)}">
                   <xsl:value-of select="local-name()" />
                </th>
             </xsl:for-each>
         </tr>
         <tr>
             <xsl:apply-templates select="Stat/*" mode="header" />
             <xsl:apply-templates select="Configuration/*/*" mode="header" />
         </tr>
         <tr>
             <xsl:apply-templates select="Stat/*" mode="row" />
             <xsl:apply-templates select="Configuration/*/*" mode="row" />
         </tr>
      </table>
   </xsl:template>

   <xsl:template match="*" mode="header">
      <th>
         <xsl:value-of select="local-name()" />
      </th>
   </xsl:template>

   <xsl:template match="*" mode="row">
      <td>
         <xsl:value-of select="." />
      </td>
   </xsl:template>
</xsl:stylesheet>

2.
斯达
配置
当应用于以下XML时

<root>
    <Stat>
         <Name>A</Name>
         <Hardware></Hardware>
         <Software></Software>
         <Version></Version>
         <State></State>
    </Stat>
    <Configuration>
     <Up>
        <Option1>2000</Option1>
        <Option2>2500000</Option2>
        <Option3>0</Option3>
        <Option4>0</Option4>
        <Option5>NA</Option5>
     </Up>
     <Down>
        <Option1>2000</Option1>
        <Option2>2500000</Option2>
        <Option3>0</Option3>
        <Option4>0</Option4>
        <Option5>NA</Option5>
      </Down>
   </Configuration>
</root>

A.
2000
2500000
0
0
NA
2000
2500000
0
0
NA
以下是输出

<table>
  <tr>
    <th colspan="5" rowspan="2">Stat</th>
    <th colspan="10">Configuration</th>
  </tr>
  <tr>
    <th colspan="5">Up</th>
    <th colspan="5">Down</th>
  </tr>
  <tr>
    <th>Name</th>
    <th>Hardware</th>
    <th>Software</th>
    <th>Version</th>
    <th>State</th>
    <th>Option1</th>
    <th>Option2</th>
    <th>Option3</th>
    <th>Option4</th>
    <th>Option5</th>
    <th>Option1</th>
    <th>Option2</th>
    <th>Option3</th>
    <th>Option4</th>
    <th>Option5</th>
  </tr>
  <tr>
    <td>A</td>
    <td/>
    <td/>
    <td/>
    <td/>
    <td>2000</td>
    <td>2500000</td>
    <td>0</td>
    <td>0</td>
    <td>NA</td>
    <td>2000</td>
    <td>2500000</td>
    <td>0</td>
    <td>0</td>
    <td>NA</td>
  </tr>
</table>

斯达
配置
向上的
向下
名称
硬件
软件
版本
陈述
选择1
选择2
选择3
选择4
选择5
选择1
选择2
选择3
选择4
选择5
A.
2000
2500000
0
0
NA
2000
2500000
0
0
NA

“预期输出:带有行和列span的HTML表”恐怕不够具体;请发布您希望看到的实际代码作为结果。添加了预期的输出图片…不准确,缺少一些列…但这是我想要的谢谢让我试试!想问一下第二个xsl:template块的用途是什么?在第二个模板块中,您正在尝试访问被前一个模板块关闭的th…代码正在工作只是想了解感谢第二个模板没有尝试访问前一个块中的
th
。模板仅与输入XML文档匹配,而与输出文档不匹配。需要注意的是在两个模板(以及相关的
xsl:apply模板
)上使用了“mode”。模板在输入XML中匹配相同的元素,但根据节点的不同执行两种不同的操作。一个输出带有元素名称的标题单元格,另一个输出带有实际值的单元格。感谢我的观点,我的另一个问题是:假设我有多个Stat和Configuration元素,如何创建一个只有一个表头和多个数据行的表?我试过这样的方法:xsl/,它将创建html,然后它应该调用另一个模板来构造表头和表数据。。在构造表头时会出现问题,因为它应该只考虑Stat和Configuration的实例,计算colspan和rowspan等,当前如果我说Stat/*,它会选择xml中的所有Stat块并给出计数,如果您就此提出一个全新的问题,这可能是最好的。如果您需要一个包含多个Stat元素的XML示例,以及您已经尝试过的内容,那么很可能不需要花太多时间就可以让它正常工作。谢谢