2017-06-29 22 views
0

我正在使用XSLT首次尝试将一些XML数据提取为CSV格式。这是一个有点输入:在XSLT中包含Nest作为独立实体

<diag> 
    <name>A08</name> 
    <desc>Viral and other specified intestinal infections</desc> 
    <excludes1> 
     <note>influenza with involvement of gastrointestinal tract (J09.X3, J10.2, J11.2)</note> 
    </excludes1> 
    <diag> 
     <name>A08.0</name> 
     <desc>Rotaviral enteritis</desc> 
    </diag> 
    <diag> 
     <name>A08.1</name> 
     <desc>Acute gastroenteropathy due to Norwalk agent and other small round viruses</desc> 
     <diag> 
     <name>A08.11</name> 
     <desc>Acute gastroenteropathy due to Norwalk agent</desc> 
     <inclusionTerm> 
      <note>Acute gastroenteropathy due to Norovirus</note> 
      <note>Acute gastroenteropathy due to Norwalk-like agent</note> 
     </inclusionTerm> 
     </diag> 
     <diag> 
     <name>A08.19</name> 
     <desc>Acute gastroenteropathy due to other small round viruses</desc> 
     <inclusionTerm> 
      <note>Acute gastroenteropathy due to small round virus [SRV] NOS</note> 
     </inclusionTerm> 
     </diag> 
    </diag> 
    <diag> 
     <name>A08.2</name> 
     <desc>Adenoviral enteritis</desc> 
    </diag> 
    <diag> 
     <name>A08.3</name> 
     <desc>Other viral enteritis</desc> 
     <diag> 
     <name>A08.31</name> 
     <desc>Calicivirus enteritis</desc> 
     </diag> 
     <diag> 
     <name>A08.32</name> 
     <desc>Astrovirus enteritis</desc> 
     </diag> 
     <diag> 
     <name>A08.39</name> 
     <desc>Other viral enteritis</desc> 
     <inclusionTerm> 
      <note>Coxsackie virus enteritis</note> 
      <note>Echovirus enteritis</note> 
      <note>Enterovirus enteritis NEC</note> 
      <note>Torovirus enteritis</note> 
     </inclusionTerm> 
     </diag> 
    </diag> 
    </diag> 

diag s的封装在section s,这是然后在chapter s,这都是下root。我所需的输出是:

A08;Viral and other specified intestinal infections; 
A08.0;Rotaviral enteritis; 
A08.1;Acute gastroenteropathy due to Norwalk agent and other small round viruses; 
A08.11;Acute gastroenteropathy due to Norwalk agent;Acute gastroenteropathy due to Norovirus Acute gastroenteropathy due to Norwalk-like agent 

注意,当出口下inclusionTerm任何note s就被连接成一个领域。

这是我与现在的工作的XSLT:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text" encoding="utf-8" /> 

    <xsl:template match="diag"> 
    <xsl:apply-templates select="name" mode="print"/>;<xsl:apply-templates select="desc" mode="print"/>;<xsl:apply-templates select="inclusionTerm/note" mode="print"/> 
    </xsl:template> 

</xsl:stylesheet> 

它适用于拉出根diag S(任何一个section作为其父)。我曾尝试添加另一个apply-templates来匹配嵌套的diag,但它最终让每个diag及其所有diag孩子变成一团糟。

回答

2

我建议像

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    exclude-result-prefixes="xs" 
    version="2.0"> 

    <xsl:output method="text"/> 

    <xsl:param name="sep" as="xs:string" select="';'"/> 

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

    <xsl:template match="diag"> 
     <xsl:value-of select="name, desc, string-join(inclusionTerm/note, ' ')" separator="{$sep}"/> 
     <xsl:text>&#10;</xsl:text> 
    </xsl:template> 

</xsl:stylesheet> 

的方法赋予

A08;Viral and other specified intestinal infections; 
A08.0;Rotaviral enteritis; 
A08.1;Acute gastroenteropathy due to Norwalk agent and other small round viruses; 
A08.11;Acute gastroenteropathy due to Norwalk agent;Acute gastroenteropathy due to Norovirus Acute gastroenteropathy due to Norwalk-like agent 
A08.19;Acute gastroenteropathy due to other small round viruses;Acute gastroenteropathy due to small round virus [SRV] NOS 
A08.2;Adenoviral enteritis; 
A08.3;Other viral enteritis; 
A08.31;Calicivirus enteritis; 
A08.32;Astrovirus enteritis; 
A08.39;Other viral enteritis;Coxsackie virus enteritis Echovirus enteritis Enterovirus enteritis NEC Torovirus enteritis 

很明显,你可以调整<xsl:apply-templates select="//diag"/>如果只有元素一定程度应该进行处理。

+0

现货,谢谢! –