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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27564860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:14:03  来源:igfitidea点击:

XSLT How to check if XML Node exists?

htmlxmlxslt

提问by vBB

I have XML file:

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

and XSL file:

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

And I want to make it like this: if node exists, display table with "Info" nodes, and if not, display SOME TEXT.

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

I've been trying

我一直在努力

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

and

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

But it is not working.

但它不起作用。

I want it like this:

我想要这样:

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

(condition: if inside XML will be node, I want to display second table, under Table1)

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

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

How I can do this?

我怎么能做到这一点?

回答by CodeManX

This outputs Yepif <AddInfo>exists as immediate child of <Record>, and Nopeotherwise:

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>

Note that you don't need for-each, you should let a second template match each <Record>:

请注意,您不需要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>

You could also avoid chooseand use two independent ifconditions:

您还可以避免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>

If you don't want to limit it to immediate children, use .//AddInfoinstead.

如果您不想将其限制为直接的孩子,请.//AddInfo改用。

Consider the following stylesheet:

考虑以下样式表:

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

It outputs

它输出

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

if there's no <AddInfo>node in <Record>, and

如果 中没有<AddInfo>节点<Record>,并且

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

otherwise.

除此以外。

You can solve this with neither using ifnor choose. XML:

您可以不使用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:

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>

Output:

输出:

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

回答by Girish kumrawat

To check if node exist in xml this XSLT code works

要检查节点是否存在于 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>