2011-04-27 32 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

相關問題