2012-11-09 78 views
1

我們需要將xml消息轉換爲更易讀的格式,所以我嘗試添加一些XSL。 我很難列出所有我想要的方式。通過嵌套for-each select的XSL選擇XML節點

我對XSL比較陌生,所以有些幫助會被認可。

的XML文件的一部分:

<biotronik-ieee11073-export format-version="3.0" creator="BioProgrammerConverter" creator-version="3.1"> 
<dataset> 
    <section name="MDC"> 
     <section name="ATTR"> 
      <section name="PT"> 
       <value name="SEX" type="String">0</value> 
      </section> 
     </section> 
     <section name="IDC"> 
      <section name="PG"> 
       <value name="TYPE" type="MDC_IDC_ENUM_PG_TYPE">IPG</value> 
       <value name="MODEL" type="String">Evia DR-T</value> 
       <value name="SERIAL" type="String">xxxxxxxx</value> 
       <value name="MFG" type="MDC_IDC_ENUM_MFG">BIO</value> 
       <value name="IMPLANT_DT" type="DateTime">20091126</value> 
      </section> 
      <section name="LEAD"> 
       <value name="MODEL" type="String">4524</value> 
       <value name="MFG" type="MDC_IDC_ENUM_MFG">MDT</value> 
       <value name="POLARITY_TYPE" type="MDC_IDC_ENUM_LEAD_POLARITY_TYPE">BI</value> 
       <value name="LOCATION" type="MDC_IDC_ENUM_LEAD_LOCATION_CHAMBER">RA</value> 
       <value name="CONNECTION_STATUS" type="MDC_IDC_ENUM_LEAD_STATUS">Connected</value> 
      </section> 
      <section name="LEAD"> 
       <value name="MODEL" type="String">T 44 F</value> 
       <value name="MFG" type="MDC_IDC_ENUM_MFG">ELA</value> 
       <value name="POLARITY_TYPE" type="MDC_IDC_ENUM_LEAD_POLARITY_TYPE">BI</value> 
       <value name="LOCATION" type="MDC_IDC_ENUM_LEAD_LOCATION_CHAMBER">RV</value> 
       <value name="CONNECTION_STATUS" type="MDC_IDC_ENUM_LEAD_STATUS">Connected</value> 
      </section> 

在某種意義上,我想下顯示節點的所有標籤= IDC

我與後續的XSL嘗試:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/"> 
<html> 
<body> 
<h2>Biotronics Results</h2> 
<table border="1"> 
<tr bgcolor="#9acd32"> 
<th>Test</th> 
<th>Value</th> 
</tr> 
<xsl:for-each select="/biotronik-ieee11073-export/dataset/section[@name='MDC']/section[@name='IDC']"> 
<xsl:for-each select="section"> 
<xsl:for-each select="value"> 
<tr> 
<td><xsl:value-of select="value"/></td> 
</tr> 
</xsl:for-each> 
</xsl:for-each> 
</xsl:for-each> 
</table> 
</body> 
</html> 
</xsl:template> 
</xsl:stylesheet> 

幫助將不勝感激。由於

回答

0

你想

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 

<xsl:template match="/"> 
    <xsl:copy-of select="//section[@name='IDC']//value"/> 
</xsl:template> 
</xsl:stylesheet> 

當這種變換所提供的XML文檔應用:

<biotronik-ieee11073-export format-version="3.0" creator="BioProgrammerConverter" creator-version="3.1"> 
    <dataset> 
     <section name="MDC"> 
      <section name="ATTR"> 
       <section name="PT"> 
        <value name="SEX" type="String">0</value> 
       </section> 
      </section> 
      <section name="IDC"> 
       <section name="PG"> 
        <value name="TYPE" type="MDC_IDC_ENUM_PG_TYPE">IPG</value> 
        <value name="MODEL" type="String">Evia DR-T</value> 
        <value name="SERIAL" type="String">xxxxxxxx</value> 
        <value name="MFG" type="MDC_IDC_ENUM_MFG">BIO</value> 
        <value name="IMPLANT_DT" type="DateTime">20091126</value> 
       </section> 
       <section name="LEAD"> 
        <value name="MODEL" type="String">4524</value> 
        <value name="MFG" type="MDC_IDC_ENUM_MFG">MDT</value> 
        <value name="POLARITY_TYPE" type="MDC_IDC_ENUM_LEAD_POLARITY_TYPE">BI</value> 
        <value name="LOCATION" type="MDC_IDC_ENUM_LEAD_LOCATION_CHAMBER">RA</value> 
        <value name="CONNECTION_STATUS" type="MDC_IDC_ENUM_LEAD_STATUS">Connected</value> 
       </section> 
       <section name="LEAD"> 
        <value name="MODEL" type="String">T 44 F</value> 
        <value name="MFG" type="MDC_IDC_ENUM_MFG">ELA</value> 
        <value name="POLARITY_TYPE" type="MDC_IDC_ENUM_LEAD_POLARITY_TYPE">BI</value> 
        <value name="LOCATION" type="MDC_IDC_ENUM_LEAD_LOCATION_CHAMBER">RV</value> 
        <value name="CONNECTION_STATUS" type="MDC_IDC_ENUM_LEAD_STATUS">Connected</value> 
       </section> 
      </section> 
     </section> 
    </dataset> 
</biotronik-ieee11073-export> 

想要的,正確的結果產生:

<value name="TYPE" type="MDC_IDC_ENUM_PG_TYPE">IPG</value> 
<value name="MODEL" type="String">Evia DR-T</value> 
<value name="SERIAL" type="String">xxxxxxxx</value> 
<value name="MFG" type="MDC_IDC_ENUM_MFG">BIO</value> 
<value name="IMPLANT_DT" type="DateTime">20091126</value> 
<value name="MODEL" type="String">4524</value> 
<value name="MFG" type="MDC_IDC_ENUM_MFG">MDT</value> 
<value name="POLARITY_TYPE" type="MDC_IDC_ENUM_LEAD_POLARITY_TYPE">BI</value> 
<value name="LOCATION" type="MDC_IDC_ENUM_LEAD_LOCATION_CHAMBER">RA</value> 
<value name="CONNECTION_STATUS" type="MDC_IDC_ENUM_LEAD_STATUS">Connected</value> 
<value name="MODEL" type="String">T 44 F</value> 
<value name="MFG" type="MDC_IDC_ENUM_MFG">ELA</value> 
<value name="POLARITY_TYPE" type="MDC_IDC_ENUM_LEAD_POLARITY_TYPE">BI</value> 
<value name="LOCATION" type="MDC_IDC_ENUM_LEAD_LOCATION_CHAMBER">RV</value> 
<value name="CONNECTION_STATUS" type="MDC_IDC_ENUM_LEAD_STATUS">Connected</value> 
+0

是非常接近,但我想要標籤在2列表中顯示其屬性...我該怎麼做? – user1292411

+0

@ user1292411:通過詢問一個新的,單獨的SO問題。您當前的問題已完全解答 - 因此您可以通過點擊答案旁邊的複選標記來接受此答案。 –