首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用xslt将节点(同名)合并到单个节点输出?

如何使用xslt将节点(同名)合并到单个节点输出?
EN

Stack Overflow用户
提问于 2011-04-28 04:24:18
回答 1查看 537关注 0票数 2

我想合并以下节点:

代码语言:javascript
复制
<sourcePatientInfo>PID-3|1428eab4645a4ce^^^&amp;1.3.6.1.4.1.21367.2008.2.1&amp;ISO</sourcePatientInfo>
<sourcePatientInfo>PID-5|WILKINS^CHARLES^^^</sourcePatientInfo>
<sourcePatientInfo>PID-8|M</sourcePatientInfo>

这样的单个节点(不用担心节点值,我已经处理好了):

代码语言:javascript
复制
   <sourcePatientInfo>
      <patientIdentifier>
      </patientIdentifier>
      <patientName>
      </patientName>
      <patientSex></patientSex>
   </sourcePatientInfo>

如果找到一些帖子:post 1 Post 2

但是它们正在合并源xml中具有不同名称的节点。现在我有这样的想法:

代码语言:javascript
复制
<xsl:template match="sourcePatientInfo">
    <sourcePatientInfo>
        <xsl:choose>
            <xsl:when test="matches(., 'PID-3')">
            <patientIdentifier />
            </xsl:when>
            <xsl:when test="matches(., 'PID-5')">
            <patientName />
            </xsl:when>
            <xsl:when test="matches(., 'PID-8')">
            <patientSex />
            </xsl:when>                 
        </xsl:choose>
    </sourcePatientInfo>
</xsl:template>

我排除了一些细节,以避免过多的代码。我得到的是3个独立的sourcePatientInfo,这是不好的。

有什么帮助吗?谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-28 05:30:48

此样式表:

代码语言:javascript
复制
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:l="http://localhost"
 exclude-result-prefixes="l">
    <l:n id="PID-3">patientIdentifier</l:n>
    <l:n id="PID-5">patientName</l:n>
    <l:n id="PID-8">patientSex</l:n>
    <xsl:variable name="vNames" select="document('')/*/l:n"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="sourcePatientInfo"/>
    <xsl:template match="sourcePatientInfo[1]">
        <xsl:copy>
            <xsl:apply-templates
             select=".|following-sibling::sourcePatientInfo"
             mode="merge"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="sourcePatientInfo" mode="merge">
        <xsl:apply-templates 
             select="$vNames[@id=substring-before(current(),'|')]">
            <xsl:with-param name="pCurrent" select="."/>
        </xsl:apply-templates>
    </xsl:template>
    <xsl:template match="l:n">
        <xsl:param name="pCurrent" select="/.."/>
        <xsl:element name="{.}">
            <xsl:value-of select="substring-after($pCurrent,'|')"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

使用此输入:

代码语言:javascript
复制
<item>
    <sourcePatientInfo>PID-3|1428eab4645a4ce^^^&amp;1.3.6.1.4.1.21367.2008.2.1&amp;ISO</sourcePatientInfo>
    <sourcePatientInfo>PID-5|WILKINS^CHARLES^^^</sourcePatientInfo>
    <sourcePatientInfo>PID-8|M</sourcePatientInfo>
</item>

输出:

代码语言:javascript
复制
<item>
    <sourcePatientInfo>
        <patientIdentifier>1428eab4645a4ce^^^&amp;1.3.6.1.4.1.21367.2008.2.1&amp;ISO</patientIdentifier>
        <patientName>WILKINS^CHARLES^^^</patientName>
        <patientSex>M</patientSex>
    </sourcePatientInfo>
</item>

编辑:将模板应用于内联地图的节点,以进行“复杂”的进一步处理。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5810100

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档