使用xsl转换xml以集成系统

时间:2014-10-03 10:35:08

标签: xml xslt mapping integration transformation

您好我正在尝试使用xsl将此xml转换为正确的格式。我是xsl的新手,遇到了问题。我需要通过应用程序将xml导入数据库。

我不需要通知中的所有属性。我在xsl中尝试了for-each标记。这循环遍历项目,但我只想选择其中几项,然后自己添加其他数据,如max:ponum,max:DOROLLOVER。

要转换的xml:

<Notification>
    <item name="NotificationName" type="Standard">CRHWTL01 Run Hours</item>
    <item name="NotificationDescription" type="Standard">wind turbine location 01 run hours</item>
    <item name="NotificationState" type="Standard">Trend</item>
    <item name="StartTime" type="Standard">10/1/2014 4:35:00 PM</item>
    <item name="EndTime" type="Standard">1/1/1970 12:00:00 AM</item>
    <item name="TriggerTime" type="Standard">10/1/2014 5:05:00 PM</item>
    <item name="Priority" type="Standard">Low</item>
    <item name="Target" type="Standard">\\maximo\Turbines\Turbine1</item>
    <item name="State" type="Standard">Trend</item>
    <item name="NotificationInstanceID" type="Standard">1</item>
    <item name="NotificationUniqueID" type="Standard">e4472d26-5366-4e57-bc7c-1af3beb50949</item>
    <item name="\\Turbine1|Turbine OK Hours Status" 
    type="AFAttribute">19850</item>
</Notification>

所需格式:

<?xml version="1.0" encoding="UTF-8"?>
<SyncMXMETERDATA xmlns:max="http://www.ibm.com/maximo" creationDateTime="2008-09-29T02:49:45" baseLanguage="string" transLanguage="string" messageID="string" maximoVersion="string">
    <MXMETERDATASet>
      <METERDATA action="Delete" relationship="string" ForInsert="string" transLanguage="string">
        <CHANGEDATE changed="false">2008-11-15T16:52:58</CHANGEDATE>
        <DOROLLOVER changed="true">false</max:DOROLLOVER>
        <INSPECTOR changed="true">string</INSPECTOR>
        <ISDELTA changed="false">true</ISDELTA>
        <MEASUREDATE changed="true">2018-02-04T14:35:59+00:00</MEASUREDATE>
        <MEASUREMENTID changed="true">10</MEASUREMENTID>
        <MEASUREMENTVALUE changed="false">1.051732E7</MEASUREMENTVALUE>
        <METERNAME changed="false">string</METERNAME>
        <POINTNUM changed="true">string</POINTNUM>
        <SITEID changed="false">string</SITEID>
      </METERDATA>
    </MXMETERDATASet>
</SyncMXMETERDATA>

我写的xsl

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns="http://http://www>w>org/2001/XMLSchema xmlns:maximoMappings="local" exclude-
result_prefixes="xs">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <SyncMXMETERDATA xmlns:max="http://www.ibm.com/maximo" messageID="string">
        <MXMETERDATASet>
            <METERDATA>                        
                 <MEASUREMENTVALUE><xsl:value-of 
                 select="Notification[item/@name = 'Turbine1-Turbine OK Hours
                 Status']"/></MEASUREMENTVALUE>
                 <POINTNUM>GB1001</POINTNUM>
                 <DOROLLOVER changed="true">false</DOROLLOVER>
                 <ISDELTA changed="false">true</ISDELTA>
                 <MAXINTERRORMSG>string</MAXINTERRORMSG>
              </METERDATA>
          </MXMETERDATASet>
          </SyncMXMETERDATA>
    </xsl:template>
</xsl:stylesheet>

我是导入但我没有得到数据库中我需要的数据。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

在您当前(已更新)的XSLT中,xsl:stylesheet包含已损坏但未关闭的条目xmlns="http://http://www>w>org/2001/XMLSchema此外,缺少结束标记</SyncMXMETERDATA>
以下XSLT产生(几乎)所需的输出,除了<max:MEASUREMENTVALUE>creationDateTime - 属性作为名为'Turbine1-Turbine OK Hours Status'的项目,并且creationDateTime的值未在输入XML:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"  
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns="http://www.3.org/2001/XMLSchema"
  xmlns:maximoMappings="local"
  exclude-result-prefixes="xsl maximoMappings">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
   <max:SyncMXMETERDATA xmlns:max="http://www.ibm.com/maximo" 
    baseLanguage="string" transLanguage="string" messageID="string" 
    maximoVersion="string">
      <max:MXMETERDATASet>
        <max:METERDATA action="Delete" relationship="string" 
                       ForInsert="string" transLanguage="string">
          <max:POINTNUM>GB1001</max:POINTNUM>
            <max:DOROLLOVER changed="true">false</max:DOROLLOVER>
            <max:ISDELTA changed="false">true</max:ISDELTA>
            <max:MAXINTERRORMSG>string</max:MAXINTERRORMSG>                        
          </max:METERDATA>
      </max:MXMETERDATASet>
    </max:SyncMXMETERDATA>
</xsl:template>
</xsl:stylesheet>

输出XML:

<?xml version="1.0" encoding="UTF-8"?>
<max:SyncMXMETERDATA xmlns:max="http://www.ibm.com/maximo" 
     xmlns="http://www.3.org/2001/XMLSchema" baseLanguage="string"
     transLanguage="string" messageID="string" maximoVersion="string">
  <max:MXMETERDATASet>
     <max:METERDATA action="Delete" relationship="string" 
                    ForInsert="string" transLanguage="string">
       <max:POINTNUM>GB1001</max:POINTNUM>
       <max:DOROLLOVER changed="true">false</max:DOROLLOVER>
       <max:ISDELTA changed="false">true</max:ISDELTA>
       <max:MAXINTERRORMSG>string</max:MAXINTERRORMSG>
    </max:METERDATA>
  </max:MXMETERDATASet>
</max:SyncMXMETERDATA>

输出的另一个区别是xmlns="http://www.3.org/2001/XMLSchema"的{​​{1}}属性。如果您不想在输出中使用它,则可以从<max:SyncMXMETERDATA>定义中删除xmlns="http://www.3.org/2001/XMLSchema"