2014-12-19 97 views
3

我有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將是節點,我想顯示第二個表,表1下)

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

我怎樣才能做到這一點?

+1

目前還不清楚是什麼你在問。一件重要的事情,你沒有公佈你的預期產出。並且,將所有場景放入您想要處理的輸入XML中。 – 2014-12-19 11:18:03

+1

你的問題不清楚。你想在哪裏插入這個測試?你只有一個普通表,所以「*如果節點存在,顯示帶有」Info「節點的表,如果不存在,則顯示」SOME TEXT *「幾乎沒有意義。 – 2014-12-19 11:19:34

+0

所以,你說如果addInfo元素不存在,應該顯示一些文本? – Rnet 2014-12-19 11:24:02

回答

11

此輸出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> 

,如果有在<Record>沒有<AddInfo>節點,

<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> 
+0

看起來像我已經測試過這樣的東西,它是不工作,因爲如果在我生成的XML中沒有「AddInfo」節點,SecondTable(YYY,ZZZ)的標題仍然顯示,而不是此標題,我想顯示SOME TEXT。 – vBB 2014-12-19 11:42:01

+0

條件節點當然需要包裝生成第二個表的所有節點。看到我更新的答案。 – CoDEmanX 2014-12-19 11:54:26

+0

謝謝!現在它正在工作:) – vBB 2014-12-19 12:33:55

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>