问题描述
我正在寻找有关 XSLT 的帮助,以将一个 xml 文件转换为另一种格式.
I am looking for some help with XSLT to transform one xml file into another format.
输入的xml文件如下:
The input xml file is below:
<PATIENTLIST ELAPSEDMS="234" >
<PATIENT ID="MGH000007">
<ADDRESS1>550 BREZHNEV ST</ADDRESS1>
<ADDRESS2></ADDRESS2>
<CITY>MOSCOW</CITY>
<STATE>MA</STATE>
<ZIP>02139</ZIP>
<COUNTRY ISO3166-1="USSR"></COUNTRY>
<DATEOFBIRTH>1934/04/10</DATEOFBIRTH>
<DAYPHONE>(617) 111-1111 </DAYPHONE>
<FIRSTNAME>TEST</FIRSTNAME>
<HOMEPHONE>(617) 111-1111</HOMEPHONE>
<LASTNAME>TEST MGH</LASTNAME>
<LIMITEDACCESS>False</LIMITEDACCESS>
<MARITALSTATUS>SINGLE</MARITALSTATUS>
<MEDICALRECORDNUMBERS>
<MEDICALRECORDNUMBER>
<SITE>BWH</SITE>
<STATUS>A</STATUS>
<VALUE>0000007</VALUE>
</MEDICALRECORDNUMBER>
<MEDICALRECORDNUMBER>
<SITE>BWI</SITE>
<STATUS>A</STATUS>
<VALUE>0000007</VALUE>
</MEDICALRECORDNUMBER>
<MEDICALRECORDNUMBER>
<SITE>MEEI</SITE>
<STATUS>A</STATUS>
<VALUE>0000007</VALUE>
</MEDICALRECORDNUMBER>
<MEDICALRECORDNUMBER>
<SITE>MGH</SITE>
<STATUS>A</STATUS>
<VALUE>0000007</VALUE>
</MEDICALRECORDNUMBER>
<MEDICALRECORDNUMBER>
<SITE>SHC</SITE>
<STATUS>A</STATUS>
<VALUE>0000007</VALUE>
</MEDICALRECORDNUMBER>
<MEDICALRECORDNUMBER>
<SITE>OLD #</SITE>
<STATUS>M</STATUS>
<VALUE>0000007</VALUE>
</MEDICALRECORDNUMBER>
</MEDICALRECORDNUMBERS>
<MIDDLEINITIAL>R</MIDDLEINITIAL>
<MOTHERSMAIDENNAME></MOTHERSMAIDENNAME>
<MRNR>0000007</MRNR>
<NAME>TEST MGH, TEST R</NAME>
<NAMESUFFIX></NAMESUFFIX>
<NAMEPREFIX></NAMEPREFIX>
<PRIMARYCAREPROVIDERID>512513</PRIMARYCAREPROVIDERID>
<PRIMARYLANGUAGE>ENGLISH</PRIMARYLANGUAGE>
<RACE CODE1="BLACK" CODE2="" FREETEXT="">BLACK</RACE>
<ETHNICITY CODE1="AFRICAN AMERICAN" CODE2="" FREETEXT="">AFRICAN AMERICAN</ETHNICITY>
<RELIGION>NO PREFERENCE</RELIGION>
<SEX>M</SEX>
<SSN></SSN>
<UID>101662537</UID>
<VETERAN>NO</VETERAN>
</PATIENT>
</PATIENTLIST>
输出文件需要看起来像:
The output file needs to look like:
<?xml version="1.0" encoding="utf-8" ?>
<eCliPSEDataIntegrationServiceRequest xmlns="http://iocent.com/eCliPSEDataIntegrationServiceRequest.xsd">
<PatientIdentifierRecord MedicalRecordNumber="MGH000007" LastName="Person" FirstName="Test" MiddleInitial="A" DateOfBirth="04/10/1934" Operation="Add" OverwriteExistingData="true" />
<PatientDataRecord MedicalRecordNumber="MGH000007" ParameterName="Gender" ParameterValue="2" TimeStamp="8/30/2011" Operation="Add" OverwriteExistingData="true" />
<PatientDataRecord MedicalRecordNumber="MGH000007" ParameterName="Race" ParameterValue="1" TimeStamp="8/30/2011" Operation="Add" OverwriteExistingData="true" />
</eCliPSEDataIntegrationServiceRequest>
所以我想拔出
患者 ID 并将其用作 MedicalRecordNumber= 值
Patient ID and use it as the MedicalRecordNumber= value
DateOfBirth 节点作为 DateOfBirth 值 - 格式从 YYYY/MM/DD 更改为 MM/DD/YYYY
DATEOFBIRTH node as the DateOfBirth value - with format changed from YYYY/MM/DD to MM/DD/YYYY
FIRSTNAME 节点成为 FirstName 的值
FIRSTNAME node becomes value for FirstName
LASTNAME 节点成为 LastName 的值
LASTNAME node becomes value for LastName
MIDDLEINITIAL 节点成为 MiddleInitial 的值
MIDDLEINITIAL node becomes value for MiddleInitial
SEX 节点变为 Gender Male=1, Female=2 的值
SEX node becomes value for Gender Male=1, Female=2
RACE 节点变为 Value for Race - 基于查找表(Caucasian=1、Afican American=2 等)-
RACE node becomes Value for Race - based on a lookup table (Caucasian=1,Afican American=2, etc.) -
所以我需要提取这些值,更改格式,在某些情况下进行类似表格的翻译(针对性别和种族),然后以新格式写出文件.
So I need to pull out these values, get the format changed, in some cases do a table-like translation (for gender and race), and write the file out in the new format.
我仅限于 XSLT 1.0
I am limited to XSLT 1.0
我对 XSLT 非常陌生,因此非常感谢任何帮助!!!
I am very new to XSLT and so any help would be greatly appreciated!!!
谢谢!
推荐答案
根据反馈 - 我正在更新我的解决方案并在此处发布
Based on feedback - I am updating my solution and posting here
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" />
<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
<xsl:variable name='MRN'>
<xsl:value-of select="PATIENTLIST/PATIENT/MRNR"/>
</xsl:variable>
<xsl:variable name='BirthDate'>
<xsl:value-of select="PATIENTLIST/PATIENT/DATEOFBIRTH"/>
</xsl:variable>
<xsl:variable name="BDayYear" select="substring($BirthDate,1,4)" />
<xsl:variable name="BDayMonth" select="substring($BirthDate,6,2)" />
<xsl:variable name="BDayDay" select="substring($BirthDate,9,2)" />
<xsl:variable name='BirthDateUse'>
<xsl:value-of select="concat($BDayMonth, '/', $BDayDay, '/', $BDayYear)"/>
</xsl:variable>
<xsl:variable name='Gender'>
<xsl:if test="translate(PATIENTLIST/PATIENT/SEX, $smallcase, $uppercase)='M'">1</xsl:if>
<xsl:if test="translate(PATIENTLIST/PATIENT/SEX, $smallcase, $uppercase)='F'">2</xsl:if>
</xsl:variable>
<xsl:variable name='RaceUC'>
<xsl:value-of select="translate(PATIENTLIST/PATIENT/RACE, $smallcase, $uppercase)"/>
</xsl:variable>
<xsl:variable name='Race'>
<xsl:choose>
<xsl:when test="$RaceUC='WHITE'">1</xsl:when>
<xsl:when test="$RaceUC='BLACK'">2</xsl:when>
<xsl:when test="$RaceUC='AFRICAN AMERICAN'">2</xsl:when>
<xsl:when test="$RaceUC='HISPANIC'">3</xsl:when>
<xsl:when test="$RaceUC='ASIAN'">4</xsl:when>
<xsl:when test="$RaceUC='NATIVE AMERICAN'">5</xsl:when>
<xsl:when test="$RaceUC='INDIAN'">6</xsl:when>
<xsl:otherwise>7</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name='Operation'>
<xsl:value-of select="'Add'"/>
</xsl:variable>
<xsl:variable name='Overwrite'>
<xsl:value-of select="'true'"/>
</xsl:variable>
<xsl:template match="/">
<eCliPSEDataIntegrationServiceRequest xmlns="http://iocent.com/eCliPSEDataIntegrationServiceRequest.xsd">
<PatientIdentifierRecord
MedicalRecordNumber="{$MRN}"
LastName="{PATIENTLIST/PATIENT/LASTNAME}"
FirstName="{PATIENTLIST/PATIENT/FIRSTNAME}"
MiddleInitial="{PATIENTLIST/PATIENT/MIDDLEINITIAL}"
DateOfBirth ="{$BirthDate}"
Operation="{$Operation}"
OverwriteExistingData="{$Overwrite}"
/>
<PatientDataRecord
MedicalRecordNumber="{$MRN}"
ParameterName="Gender" ParameterValue="{$Gender}"
Operation="{$Operation}"
OverwriteExistingData="{$Overwrite}"
/>
<PatientDataRecord
MedicalRecordNumber="{$MRN}"
ParameterName="Race_7_Groups" ParameterValue="{$Race}"
Operation="{$Operation}"
OverwriteExistingData="{$Overwrite}"
/>
</eCliPSEDataIntegrationServiceRequest>
</xsl:template>
</xsl:stylesheet>
感谢您的反馈和建议.
此版本处理大小写转换/比较以及以我需要的格式格式化生日.
This version handles case conversion/comparison as well as formatting the bday in a format that I needed.
我喜欢 AVT 语法来简化代码并删除我正在做的手动格式化" - 感谢这些提示!
I like the AVT syntax to simplify the code and the to remove the "manual formatting" that I was doing - thanks for those tips!
这篇关于XSLT 将 xml 转换为 xml 提取特定值并映射到新格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!