XSLT将xml转换为xml,提取特定值并映射到新格式

时间:2011-08-30 22:22:09

标签: xml xslt xpath transform

我正在寻找XSLT的一些帮助,将一个xml文件转换为另一种格式。

输入xml文件如下:

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

输出文件需要如下所示:

<?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 = value

DATEOFBIRTH节点作为DateOfBirth值 - 格式从YYYY / MM / DD更改为MM / DD / YYYY

FIRSTNAME节点成为FirstName的值

LASTNAME节点成为LastName的值

MIDDLEINITIAL节点成为MiddleInitial的值

SEX节点成为Gender Male = 1,Female = 2

的值

RACE节点成为竞赛价值 - 基于查询表(高加索人= 1,非裔美国人= 2等) -

因此,我需要提取这些值,更改格式,在某些情况下执行类似表格的翻译(针对性别和种族),并以新格式写出文件。

我仅限于XSLT 1.0

我是XSLT的新手,因此非常感谢任何帮助!!!

谢谢!

4 个答案:

答案 0 :(得分:1)

我很欣赏上面收到的开头。我发布了我想出的xslt来解决我的问题:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:variable name='newline'><xsl:text> 
</xsl:text>
</xsl:variable>

<xsl:variable name='MRN'>
    <xsl:value-of select="PATIENTLIST/PATIENT/@ID"/>
</xsl:variable>

<xsl:variable name='Gender'>
    <xsl:if test="PATIENTLIST/PATIENT/SEX='M'">1</xsl:if>
    <xsl:if test="PATIENTLIST/PATIENT/SEX='F'">2</xsl:if>
</xsl:variable>

 <xsl:variable name='RaceIn'>
   <xsl:value-of select="PATIENTLIST/PATIENT/RACE"/>
 </xsl:variable>  

<xsl:variable name='Race'>
  <xsl:choose>
    <xsl:when test="$RaceIn='WHITE'">1</xsl:when>
    <xsl:when test="$RaceIn='BLACK'">2</xsl:when>
    <xsl:when test="$RaceIn='HISPANIC'">3</xsl:when>
    <xsl:when test="$RaceIn='ASIAN'">4</xsl:when>
    <xsl:when test="$RaceIn='NATIVE AMERICAN'">5</xsl:when>
    <xsl:when test="$RaceIn='INDIAN'">5</xsl:when>
    <xsl:otherwise>7</xsl:otherwise>
  </xsl:choose>
</xsl:variable>

 <xsl:variable name='CurrDate'>
    <xsl:value-of select="'MM/DD/YYY'"/>
</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="/">
    <xsl:text>
    </xsl:text>
      <eCliPSEDataIntegrationServiceRequest >
        <xsl:text>
        </xsl:text>
        <PatientIdentifierRecord>
          <xsl:attribute name="MedicalRecordNumber">
            <xsl:value-of select="$MRN"/>
          </xsl:attribute>
          <xsl:attribute name="LastName">
            <xsl:value-of select="PATIENTLIST/PATIENT/LASTNAME"/>
          </xsl:attribute>
          <xsl:attribute name="FirstName">
            <xsl:value-of select="PATIENTLIST/PATIENT/FIRSTNAME"/>
          </xsl:attribute>
          <xsl:attribute name="MiddleInitial">
            <xsl:value-of select="PATIENTLIST/PATIENT/MIDDLEINITIAL"/>
          </xsl:attribute>
          <xsl:attribute name="DateOfBirth">
            <xsl:value-of select="PATIENTLIST/PATIENT/DATEOFBIRTH"/> <!-- do we need to change the date format?-->
          </xsl:attribute>
          <xsl:attribute name="Operation">
            <xsl:value-of select="$Operation"/>
          </xsl:attribute>
          <xsl:attribute name="OverwriteExistingData">
            <xsl:value-of select="$Overwrite"/>
          </xsl:attribute>
        </PatientIdentifierRecord>
        <xsl:text>
            </xsl:text>
          <PatientDataRecord>
            <xsl:attribute name="MedicalRecordNumber">
              <xsl:value-of select="$MRN"/>
            </xsl:attribute>
            <xsl:attribute name="ParameterName">
              <xsl:value-of select="'Gender'"/>
            </xsl:attribute>
            <xsl:attribute name="ParameterValue">
              <xsl:value-of select="$Gender"/>
            </xsl:attribute>
            <xsl:attribute name="Timestamp">
              <xsl:value-of select="$CurrDate"/>
            </xsl:attribute>
            <xsl:attribute name="Operation">
              <xsl:value-of select="$Operation"/>
            </xsl:attribute>
            <xsl:attribute name="OverwriteExistingData">
              <xsl:value-of select="$Overwrite"/>
            </xsl:attribute>
          </PatientDataRecord>
        <xsl:text>
            </xsl:text>
        <PatientDataRecord>
          <xsl:attribute name="MedicalRecordNumber">
            <xsl:value-of select="$MRN"/>
          </xsl:attribute>
          <xsl:attribute name="ParameterName">
            <xsl:value-of select="'Race'"/>
          </xsl:attribute>
          <xsl:attribute name="ParameterValue">
            <xsl:value-of select="$Race"/>           <!-- this needs to be transformed based on race table and race_7_groups parameter -->
          </xsl:attribute>
          <xsl:attribute name="Timestamp">
            <xsl:value-of select="$CurrDate"/>                  <!-- how do i get the current date?-->
          </xsl:attribute>
          <xsl:attribute name="Operation">
            <xsl:value-of select="$Operation"/>
          </xsl:attribute>
          <xsl:attribute name="OverwriteExistingData">
            <xsl:value-of select="$Overwrite"/>
          </xsl:attribute>
        </PatientDataRecord>
        <xsl:text>
    </xsl:text>
      </eCliPSEDataIntegrationServiceRequest>
  </xsl:template>
</xsl:stylesheet>

我仍然致力于获得当前日期的解决方案。我看过的选项 - 作为参数传入 - 编写脚本函数以获取当前日期

此外,我不确定是否需要将日期格式从YYYY / MM / DD更改为MM / DD / YYYY。

如果有人对上述两个主题有一些建议,他们会受到欢迎。

感谢

答案 1 :(得分:1)

根据反馈 - 我正在更新我的解决方案并在此处发布

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

感谢您的反馈和建议。

此版本处理案例转换/比较以及以我需要的格式格式化bday。

我喜欢AVT语法来简化代码并删除我正在做的“手动格式化” - 感谢这些提示!

答案 2 :(得分:0)

这应该让你开始走正确的道路:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="PATIENTLIST">
        <xsl:for-each select="PATIENT">                                             
            <eCliPSEDataIntegrationServiceRequest>
                <xsl:attribute name="xmlns">
                    http://iocent.com/eCliPSEDataIntegrationServiceRequest.xsd
                </xsl:attribute>
                <PatientIdentifierRecord>
                    <xsl:attribute name="MedicalRecordNumber">
                        <xsl:value-of select="@ID"/>
                    </xsl:attribute>
                    ...
                    <xsl:for-each select="MEDICALRECORDNUMBER">
                        <xsl:attribute name="MedicalRecordNumber">
                            <xsl:value-of select="@ID"/>
                        </xsl:attribute>
                        ...
                    </xsl:for-each>                                             
                </PatientIdentifierRecord>
            </eCliPSEDataIntegrationServiceRequest>
         </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

您可能需要一些硬编码<xsl:if test="SEX='MALE'">1</xsl:if>语句来执行“枚举”切换

答案 3 :(得分:0)

只是一些提示。

使用AVT语法简化代码。例如:

    <PatientIdentifierRecord
      MedicalRecordNumber="{$MRN}"
      LastName="{PATIENTLIST/PATIENT/LASTNAME}"
     ...
    />

对于换行,请使用ISO-8859-1字符集中的代码:

<xsl:variable name="lf" select="'&#10;'"/>

有关日期时间,请参阅此topic并查看解析器文档。

如果您需要更改日期格式,请使用substring()concat()功能。检查this similar topic

您似乎正在使用换行来缩进代码。对于这项工作,您有一个正确的指令(根据您的解析器可能支持):

<xsl:output indent="yes"/>