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

时间:2014-12-19 11:12:49

标签: 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>

我希望这样做:如果节点存在,则显示带有“Info”节点的表,如果不存在,则显示SOME TEXT。

我一直在尝试

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

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

但它不起作用。

我想这样:

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

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

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

我怎么能这样做?

2 个答案:

答案 0 :(得分:14)

如果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 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> 来解决此问题。 XML:

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

XSLT:

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

输出:

{{1}}

答案 1 :(得分:0)

要检查xml中是否存在此XSLT代码的节点

<xsl:choose>
                <xsl:when test="XMLNodeName">
                  <Cell ss:Index="2" ss:StyleID="s110">
                    <Data ss:Type="String">
                      <xsl:value-of select="NodeOne"/>
                    </Data>
                  </Cell>
                </xsl:when>
                <xsl:otherwise>
                  <Cell`enter code here` ss:Index="2" ss:StyleID="s110">
                    <Data ss:Type="String">
                      <xsl:value-of select="NodeTwo"/>
                    </Data>
                  </Cell>
                </xsl:otherwise>
 </xsl:choose>
相关问题