2011-04-27 62 views
2

我想合并,如节点:如何使用xslt将节点(使用相同名称)合并为单节点输出?

<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> 

要这样的单个节点(不必担心节点值,我把它处理):

<sourcePatientInfo> 
     <patientIdentifier> 
     </patientIdentifier> 
     <patientName> 
     </patientName> 
     <patientSex></patientSex> 
    </sourcePatientInfo> 

如果发现几个岗位: post 1 Post 2

但他们合并与源XML名称不同的节点。现在我有这个:

<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,这是不好的。

任何帮助?谢谢!!!!

+0

我不太明白 - “PID-1”,“PID-5”是什么类型的标识符,即修复,你可以真正使用它们来匹配(就像你在你的例子中做的那样)或者你会有使用'follow-sibling'或'next()' – 2011-04-27 20:35:18

+0

我实际上假装使用这些PID-x标识符在之间进行选择,即我的3个可能的子元素之一。 – 2011-04-27 20:46:50

回答

6

这个样式表:

<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> 

有了这个输入:

<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> 

输出:

<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> 

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

+2

他是XSL魔术师! – 2011-04-27 21:45:29

+0

如果我想为'patientName'做一些特殊的事情,可以在最后一个模板中使用< ...吗?它的工作原理,但我虽然我可以使用模板匹配。谢谢!!! – 2011-04-27 21:48:16

+0

@ code-gijoe:够公平的。检查我的更新。 – 2011-04-27 21:59:03

相关问题