在XSLT中将嵌套包含为单独的实体

时间:2017-06-29 14:08:08

标签: xml xslt

我第一次尝试将一些XML数据提取为CSV格式时,正在使用XSLT。这里有一点输入:

  <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封装在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 之下。我想要的输出是:

note

请注意,inclusionTerm下的任何<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> 在导出时都会连接到一个字段中。

这是我现在正在使用的XSLT:

diag

它适用于提取根section s(任何apply-templates作为其父项)。我确实尝试添加另一个diag来匹配嵌套的diag,但它最终使每个diag及其所有MailMessage mail = new MailMessage("from", "to"); SmtpClient client = new SmtpClient(); client.EnableSsl = true; client.Host = "smtp-mail.outlook.com"; client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential("email", "password"); client.Port = 587; client.DeliveryMethod = SmtpDeliveryMethod.Network; mail.Subject = "test"; mail.Body = "test"; client.Send(mail); 个孩子陷入混乱。< / p>

1 个答案:

答案 0 :(得分: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"/>