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

XSLT将xml转换为xml,提取特定值并映射为新格式,xml,xslt,xpath,transform,Xml,Xslt,Xpath,Transform,我正在寻找XSLT的帮助,以便将一个xml文件转换为另一种格式 输入xml文件如下所示: <PATIENTLIST ELAPSEDMS="234" > <PATIENT ID="MGH000007"> <ADDRESS1>550 BREZHNEV ST</ADDRESS1> <ADDRESS2></ADDRESS2> <CITY>MOSCOW</CIT

我正在寻找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>

勃列日涅夫街550号
莫斯科
文科硕士
02139
1934/04/10
(617) 111-1111 
试验
(617) 111-1111
测试MGH
假的
单身
BWH
A.
0000007
BWI
A.
0000007
米伊
A.
0000007
MGH
A.
0000007
SHC
A.
0000007
旧的#
M
0000007
R
0000007
测试MGH,测试R
512513
英语
黑色
非裔美国人
无偏好
M
101662537
不
输出文件需要如下所示:

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

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

FIRSTNAME节点成为FIRSTNAME的值

LASTNAME节点成为LASTNAME的值

MIDDLEINITIAL节点成为MIDDLEINITIAL的值

性别节点成为性别男性=1,女性=2的值

RACE节点成为RACE的值-基于查找表(高加索人=1,美国黑人=2,等等)

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

我仅限于XSLT1.0

我对XSLT非常陌生,因此非常感谢您的帮助


谢谢

这会让你走上正确的道路:

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

http://iocent.com/eCliPSEDataIntegrationServiceRequest.xsd
...
...

您可能需要一些硬编码的
1
语句来执行“enum”切换

我感谢您从上面收到的开始。我在下面发布我为解决我的问题而提出的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>

1.
2.
1.
2.
3.
4.
5.
5.
7.
我仍在研究一个解决方案,以获取当前日期。我看过的选项 -作为参数传入 -编写脚本函数以获取当前日期

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

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

谢谢

只是一些提示

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

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

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

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

1.
2.
1.
2.
2.
3.
4.
5.
6.
7.
感谢您的反馈和建议

这个版本处理大小写转换/比较,以及以我需要的格式格式化bday


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

你能澄清性别和种族节点吗?它们不存在于示例输出中。是-性别值来自原始xml文件中的性别节点Race from Race您的xml示例输出没有正确的值。非常感谢让我开始!我将继续发表一篇文章,介绍我为解决这个问题而创作的东西。在这里,对有用的答案表示感谢的正确方式是向上投票;-)。无论如何,欢迎你+谢谢你的努力。
<xsl:output indent="yes"/>
<?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>