html - XSLT 如何检查 XML 节点是否存在?

标签 html xml xslt

我有 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<Data>
  <Records>
    <Record>
     <AddInfo>
      <Info>
      </Info>
     </AddInfo>
    </Record>
  </Records>
</Data>

和XSL文件:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="Dane">
    <html>
      <link rel="stylesheet" type="text/css" href="report.css"></link>
      <body>
        <h2>Table1</h2>
        <table border="1" cellspacing="0">
          <tr>
            <th>XXX</th>
          </tr>
          <xsl:for-each select="Records/Record">
            <tr>
              <td>
                <xsl:value-of select="XXX"/>
              </td>
            </tr>
          </xsl:for-each>
        </table>
        <h2>SecondTable</h2>
        <table border="1" cellspacing="0">
          <tr>
            <th>YYY</th>
            <th>ZZZ</th>
          </tr>
          <xsl:for-each select="Records/Record/AddInfo/Info">
            <tr>
              <td>
                <xsl:value-of select="YYY"/>
              </td>
              <td>
                <xsl:value-of select="ZZZ"/>
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

我想这样做:如果节点存在,则显示带有“信息”节点的表,如果不存在,则显示一些文本。

我一直在努力

<xsl:if test="following-sibling::AddInfo">
</xsl:if>

<xsl:if test="AddInfo">
</xsl:if>

但它不起作用。

我想要这样:

Table1
---------------------
|     |      |      |

(条件:如果XML里面是节点,我想显示第二个表,在Table1下)

SecondTable
-------------
|     |     |

我该怎么做?

最佳答案

这会输出 Yep如果<AddInfo>作为 <Record> 的直接子项存在, 和 Nope否则:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="Data">
    <xsl:for-each select="Records/Record">
      <xsl:choose>
        <xsl:when test="AddInfo">Yep</xsl:when>
        <xsl:otherwise>Nope</xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

请注意,您不需要 for-each , 你应该让第二个模板匹配每个 <Record> :

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="Data">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="Data/Records/Record">
    <xsl:choose>
      <xsl:when test="AddInfo">Yep</xsl:when>
      <xsl:otherwise>Nope</xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

你也可以避免 choose并使用两个独立的 if条件:

  <xsl:template match="Data/Records/Record">
    <xsl:if test="AddInfo">Yep</xsl:if>
    <xsl:if test="not(AddInfo)">Nope</xsl:if>
  </xsl:template>

如果您不想将其限制为直系子级,请使用 .//AddInfo相反。

考虑以下样式表:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="Data">
    <xsl:apply-templates select="Records/Record"/>
  </xsl:template>

  <xsl:template match="Data/Records/Record">
    <table class="one"></table>
    <xsl:if test="AddInfo">
      <table class="two"></table>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

输出

<table class="one"></table>

如果没有 <AddInfo> <Record> 中的节点, 和

<table class="one"></table>
<table class="two"></table>

否则。

你可以不使用 if 来解决这个问题也不choose . XML:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<Data>
  <AddInfo>
    <Info>This is ignored</Info>
  </AddInfo>
  <Records>
    <Record>
        <AddInfo>
          <Info>One,</Info>
          <Info>Two,</Info>
          <Info>Three</Info>
        </AddInfo>
    </Record>
    <Record>
      <Info>Ignored as well</Info>
    </Record>
    <Record>
      <Nested>
        <AddInfo>
          <Info>So is this</Info>
        </AddInfo>
      </Nested>
    </Record>
  </Records>
</Data>

XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="Data">
    <root>
      <xsl:apply-templates select="Records/Record"/>
    </root>
  </xsl:template>

  <xsl:template match="Data/Records/Record">
    <xsl:copy>
      <table id="one"></table>
      <xsl:apply-templates select="AddInfo"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Data/Records/Record/AddInfo">
    <table id="two">
      <xsl:apply-templates select="Info"/>
    </table>
  </xsl:template>

  <xsl:template match="Data/Records/Record/AddInfo/Info">
    <xsl:value-of select="."/>
  </xsl:template>

</xsl:stylesheet>

输出:

<root>
  <Record>
    <table id="one" />
    <table id="two">One,Two,Three</table>
  </Record>
  <Record>
    <table id="one" />
  </Record>
  <Record>
    <table id="one" />
  </Record>
</root>

关于html - XSLT 如何检查 XML 节点是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27564860/

相关文章:

html - html 内容类型和内联/ block 元素之间的连接

javascript - jQuery:淡入文本输入,保持可见直到不活动

javascript - 将内容从一个网页传输到同一网站中的另一个网页?

xml - perl解析xml文件后,得到特殊字符

For-Each 内的 XSLT 条件

java - XSL-FO 无法在 Java 中添加字体

javascript - 通过 Javascript 将图像、选定的值发布到 php

xml - 将 Xquery 中序列中的所有值相乘

python - 在 View 中添加默认过滤器 - Odoo

XML - XSLT - count() 函数内的 document() 函数