【问题标题】:XLST query to remove unwanted element in returned XMLXSLT 查询以删除返回的 XML 中不需要的元素
【发布时间】:2014-10-09 10:30:00
【问题描述】:

我有一个修改后的 XSLT 查询,它返回我想要的所有内容,但还有一个我不想要的附加元素。我怎样才能修改它,使它摆脱<identifier type="nt">80df0b42de8f31ac4cb7a30d325ff0c1</identifier> 行。 xml是:

    <?xml version="1.0" encoding="UTF-8"?>
<CMDBTopology>
<Objects>
<CMDBSet>
<name>80df0b42de8f31ac4cb7a30d325ff0c1</name>
<CMDBObject>
<identifier type="nt">80df0b42de8f31ac4cb7a30d325ff0c1</identifier>
<Properties>
<root_class type="STRING">nt</root_class>
<host_servertype type="STRING"></host_servertype>
<host_osrelease type="STRING"></host_osrelease>
<display_label type="STRING">pharsm-s3004</display_label>
<host_osinstalltype type="STRING"></host_osinstalltype>
</Properties>
</CMDBObject>

我目前的 xlst 查询是 `

        <html>

          <head>

                    <title> title </title>

          </head>

          <body>

                    <xsl:apply-templates />

          </body>

        </html>

        <table width="1" border="1" >

                                            <tr>

    <td> <xsl:value-of select="display_label" /> </td>      
    <td> <xsl:value-of select="root_class" /> </td>


                                <td> <xsl:value-of select="resolver_group" /> </td>

                                <td> <xsl:value-of select="supported_by" /> </td>

                                <td> <xsl:value-of select="environment" /> </td>

                                <td> <xsl:value-of select="site_code" /> </td>

                                <td> <xsl:value-of select="sla_classification" /> </td>

                                <td> <xsl:value-of select="datacenter" /> </td>

                                </tr>

        </table>

`

【问题讨论】:

  • 您发布的 XSLT 与 XML 片段不兼容。它不会输出任何东西(它选择的元素不存在于 XML 文件或调用它们的上下文中)。编辑问题并包含您正在使用的实际文件。
  • 我已经编辑它以反映 xml 文件
  • 但是如果我将它更改为正确的“值选择”它不会运行它会给出一个空结果但是如果我运行它之前它会给出正确的数据但是我想要的额外元素摆脱...
  • 您在 XSLT 中有一个匹配 CMDBRelation 的模板,但在您的 XML 中不存在这样的元素,所以这个模板永远不会被使用。如果您还显示了当前获得的输出以及您实际想要的输出,它实际上可能会更有帮助。谢谢。
  • 我尝试将 CMDBRelation 更改为 CMDBObject 并且它给出了相同的输出......我将编辑问题以添加我想要的输出以及我得到的......它完全省略了表格格式...

标签: html xml xslt scripting


【解决方案1】:

很难给出一个准确的答案,不知道你希望你的输出是什么样子,但你的主要问题是你的 XSLT 中有一个匹配 CMDBRelation 的模板,就像这样......

<xsl:template match="CMDBRelation" >

但是您的 XML 中没有这样的 CMDBRelation 元素!这意味着这个模板永远不会被调用。这意味着,当您在第一个模板中执行 &lt;xsl:apply-templates /&gt; 时,将使用 XSLT 的“内置模板”。这些有效地迭代 XML 中的所有节点,在找到它们的位置输出文本节点,这就是为什么您可以打印出所有文本而没有表格格式的原因。

我怀疑您的模板实际上需要匹配 CMDBSet

<xsl:template match="CMDBRelation" >

您似乎还在您的 xsl:for-each 语句中引用了 ToFrom 元素,并且和以前一样,这些元素在您的 XML 中不存在。

<xsl:for-each select="To/CMDBObject/Properties">

您可能需要将这两个语句合并为一个,像这样

<xsl:for-each select="CMDBObject/Properties">

这里有一些经过修改的 XSLT 可以让您开始,它会生成一个表格并且不输出任何 identifier 元素。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="/">
      <html>
         <head>
            <title> title </title>
         </head>
         <body>
            <xsl:apply-templates/>
         </body>
      </html>
   </xsl:template>

   <xsl:template match="CMDBSet">
      <table width="1" border="1">
         <xsl:for-each select="CMDBObject/Properties">
            <tr>
               <td><xsl:value-of select="display_label"/></td>
               <td><xsl:value-of select="root_class"/></td>
               <td><xsl:value-of select="host_servertype"/></td>
               <td><xsl:value-of select="host_osrelease"/></td>
               <td><xsl:value-of select="host_osinstall"/></td>
            </tr>
         </xsl:for-each>
      </table>
   </xsl:template>
</xsl:stylesheet>

【讨论】:

  • 非常感谢它摆脱了标识符元素,现在我只需要稍微调整一下它,因为它现在只显示根类和显示标签。但这给了我一个很好的起点
  • 还有一个问题,我现在试图只为每个选定语句的值显示标题一次 - 我该如何实现这一点..
  • 请不要在问题被回答和接受后编辑问题,因为这会让其他人难以理解发生了什么。如果您将问题编辑为以前的内容,并提出一个全新的问题,那就更好了。谢谢。
  • 好的,我会把它编辑成原来的问题,没有考虑到其他人以后可能会有类似的问题
猜你喜欢
  • 2019-08-14
  • 2011-08-24
  • 2015-01-26
  • 2022-01-05
  • 2016-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多