【问题标题】:XSLT Concatenating Elements TogetherXSLT 将元素连接在一起
【发布时间】:2018-10-03 10:00:40
【问题描述】:

我希望像下面这样转换一些 XML,在此示例中只有 2 个元素可以组合在一起,但可能更多:

<file>
    <patient>
        <Lab_No_Spec_No>12345</Lab_No_Spec_No>
        <Patient_Number>ABC</Patient_Number>
        <Albumin_g_L>48 </Albumin_g_L>
        <Calcium_mmol_L>
        <Phosphate_mmol_L>100 </Phosphate_mmol_L>
    </patient>
    <patient>
        <Lab_No_Spec_No>12345</Lab_No_Spec_No>
        <Patient_Number>ABC</Patient_Number>
        <Albumin_g_L>92 </Albumin_g_L>
        <Calcium_mmol_L>50 </Calcium_mmol_L>
        <Phosphate_mmol_L/>
    </patient>
</file>

到以下结果:

<file>
    <patient>
        <Lab_No_Spec_No>12345</Lab_No_Spec_No>
        <Patient_Number>ABC</Patient_Number>
        <Albumin_g_L>48,92</Albumin_g_L>
        <Calcium_mmol_L>50</Calcium_mmol_L>
        <Phosphate_mmol_L>100</Phosphate_mmol_L>
    </patient>
</file>

这是我目前的转变:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:key name="specimen" match="patient" use="Lab_No_Spec_No" />
    <xsl:template match="file">
        <file>
            <xsl:for-each select="patient[count(. | key('specimen', Lab_No_Spec_No)[1]) = 1]">
                <patient>
                    <xsl:copy-of select="Lab_No_Spec_No" />
                    <xsl:copy-of select="Patient_Number" />
                    <Albumin_g_L>
                        <xsl:for-each select="key('specimen', Lab_No_Spec_No)">
                            <xsl:value-of select="normalize-space(Albumin_g_L)" />
                            <xsl:if test="position() != last()">
                                <xsl:text>,</xsl:text>
                            </xsl:if>
                        </xsl:for-each>
                    </Albumin_g_L>
                    <Calcium_mmol_L>
                        <xsl:for-each select="key('specimen', Lab_No_Spec_No)">
                            <xsl:value-of select="normalize-space(Calcium_mmol_L)" />
                            <xsl:if test="position() != last()">
                                <xsl:text>,</xsl:text>
                            </xsl:if>
                        </xsl:for-each>
                    </Calcium_mmol_L>
                    <Phosphate_mmol_L>
                        <xsl:for-each select="key('specimen', Lab_No_Spec_No)">
                            <xsl:value-of select="normalize-space(Phosphate_mmol_L)" />
                            <xsl:if test="position() != last()">
                                <xsl:text>,</xsl:text>
                            </xsl:if>
                        </xsl:for-each>
                    </Phosphate_mmol_L>
                </patient>
            </xsl:for-each>
        </file>
    </xsl:template>
</xsl:stylesheet>

上面有一些问题我正在寻求帮助:

  1. 在&lt;xsl:for-each select="key('specimen', Lab_No_Spec_No)"&gt; 连接过程中包含了空元素,我想省略这些元素,以免得到像&lt;Calcium_mmol_L&gt;50,&lt;/Calcium_mmol_L&gt; 这样的结果。我的 for-each 选择需要更改什么以便不选择空元素?

  2. 真正的源 XML 文件有 30 多个元素,我需要对其进行连接。我在我的示例中对 3 个元素重复了相同的转换,但是对于 Patient_Number 元素之后的元素是否有一种速记方法,或者我是否必须重复转换?大致如下:

<xsl:for-each select="following-sibling::Patient_Number">
  <local-name(.)>
      <xsl:for-each select="key('specimen', Lab_No_Spec_No)">
          <xsl:value-of select="normalize-space(.)" />
          <xsl:if test="position() != last()">
              <xsl:text>,</xsl:text>
          </xsl:if>
      </xsl:for-each>
  </local-name(.)>
</xsl:for-each>

【问题讨论】:

  • 我猜你没有使用xsl:for-each-group 意味着你被 XSLT 1.0 困住了?如果是这样,您应该相应地标记问题,这样我们就不会浪费时间为您提供 XSLT 2.0 答案。
  • 是的,已更新该标签

标签: xml xslt-1.0


【解决方案1】:

要排除空元素,只需在 xsl:for-each 中添加一个条件

<xsl:for-each select="key('specimen', Lab_No_Spec_No)[normalize-space(Albumin_g_L)]">

至于避免重复,你可以聪明一点,对每个 Lab_No_Spec_No 的列名进行第二级分组。

试试这个 XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:key name="specimen" match="patient" use="Lab_No_Spec_No" />
    <xsl:key name="cols" match="patient/*" use="concat(../Lab_No_Spec_No, '|', local-name())" />

    <xsl:template match="file">
        <file>
            <xsl:for-each select="patient[count(. | key('specimen', Lab_No_Spec_No)[1]) = 1]">
                <patient>
                    <xsl:copy-of select="Lab_No_Spec_No" />
                    <xsl:copy-of select="Patient_Number" />
                    <xsl:apply-templates select="*[not(self::Lab_No_Spec_No) and not(self::Patient_Number)]
                                                  [count(. | key('cols', concat(../Lab_No_Spec_No, '|', local-name()))[1]) = 1]" />
                </patient>
            </xsl:for-each>
        </file>
    </xsl:template>

    <xsl:template match="patient/*">
      <xsl:copy>
        <xsl:for-each select="key('cols', concat(../Lab_No_Spec_No, '|', local-name()))[normalize-space()]">
            <xsl:value-of select="normalize-space(.)" />
            <xsl:if test="position() != last()">
                <xsl:text>,</xsl:text>
            </xsl:if>
        </xsl:for-each>
      </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

不过,在 XSLT 2.0 中,这一切都变得容易得多......

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="file">
        <file>
            <xsl:for-each-group select="patient" group-by="Lab_No_Spec_No">
                <patient>
                    <xsl:copy-of select="Lab_No_Spec_No,Patient_Number" />
                    <xsl:for-each-group select="current-group()/(* except (Lab_No_Spec_No, Patient_Number))" group-by="local-name()">
                        <xsl:copy>
                            <xsl:value-of select="current-group()[normalize-space()]/normalize-space()" separator="," />
                        </xsl:copy>
                    </xsl:for-each-group>
                </patient>
            </xsl:for-each-group>
        </file>
    </xsl:template>
</xsl:stylesheet>

【讨论】:

  • 卡在 .NET 中的 XSLT 1.0,我想由于这个原因,我必须列出所有我不想像你的答案一样连接在一起的元素?在我的示例数据中,只有 2 个,但实际上有 10 个。
  • 你可以写[contains('Albumin_g_L Calcium_mmol_L Phosphate_mmol_L', local-name())]而不是[not(self::Lab_No_Spec_No) and not(self::Patient_Number)]
【解决方案2】:

Tim 已经提供了一个很好的答案,但我认为这样的问题受益于从身份转换模板开始,然后只为需要转换的那些元素添加模板。此外,只要在输入中每个patient 具有相同的child 元素,我认为您可以简单地分组为完成,然后只处理每个组中第一个patient 元素的子元素,然后在模板中为那些需要连接数据的元素,您可以使用建议的键将它们推送到另一种模式以输出列表:

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

  <xsl:output method="xml" indent="yes" version="5"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:key name="specimen" match="patient" use="Lab_No_Spec_No" />

  <xsl:key name="child" match="patient/*" use="concat(../Lab_No_Spec_No, '|', local-name())"/>

  <xsl:template match="patient[count(. | key('specimen', Lab_No_Spec_No)[1]) = 1]">
      <xsl:copy>
          <xsl:apply-templates/>
      </xsl:copy>
  </xsl:template>

  <xsl:template match="patient[not(count(. | key('specimen', Lab_No_Spec_No)[1]) = 1)]"/>

  <xsl:template match="patient/*[not(self::Lab_No_Spec_No|self::Patient_Number)]">
      <xsl:copy>
          <xsl:apply-templates select="key('child', concat(../Lab_No_Spec_No, '|', local-name()))[normalize-space()]" mode="value"/>
      </xsl:copy>
  </xsl:template>

  <xsl:template match="patient/*" mode="value">
      <xsl:if test="position() > 1">,</xsl:if>
      <xsl:value-of select="normalize-space()"/>      
  </xsl:template>

</xsl:stylesheet>

这并没有让您摆脱您在评论中提出的关于必须在某处列出您不想连接的所有元素的问题,但您只有一个 match 属性谓词需要这样做。

https://xsltfiddle.liberty-development.net/gWmuiJX/2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-09
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    • 2021-06-13
    相关资源
    最近更新 更多