简体   繁体   English

XML消息的转换不会更改架构

[英]Transform of XML Message doesn't change schema

I am working on a project that requires me to transform a message and replace the old namespaces with new namespaces. 我正在一个项目上,该项目需要我转换一条消息并将旧的名称空间替换为新的名称空间。 I have that component working correctly. 我的组件正常工作。 I do not have the schema attribute changing. 我没有更改架构属性。 I can do it separately but I cannot change the schema namespace and the element namespaces. 我可以单独进行操作,但是不能更改架构名称空间和元素名称空间。 What am I missing here? 我在这里想念什么?

Here is the message: 这是消息:

<ns1:SupportNotificationMsg xsi:schemaLocation="http://namespace1.com:8081/Organization/SupportServices SupportServices.xsd" 
          xmlns:ns3="http://namespace3.com/Common" 
          xmlns:ns1="http://namespace1.com:8081/Organization/SupportServices" 
          xmlns:ns2="http://namespace2.com:8081/Organization/" 
          xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" 
          xmlns:j="http://www.it.ojp.gov/jxdm/3.0.3" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
         <ns2:SupportNotification>
          <ns3:MessageHeader>
           <ns3:MessageSender>
            <ns3:OrganizationName>ORG</ns3:OrganizationName>
            <ns3:ApplicationName>SEARCH</ns3:ApplicationName>
            <ns3:ContactName>Support</ns3:ContactName>
            <ns3:ContactPhoneNumber>111-222-3333</ns3:ContactPhoneNumber>
           </ns3:MessageSender>
           <ns3:MessageSentDateTime>2017-03-17T12:14:33</ns3:MessageSentDateTime>
          </ns3:MessageHeader>
          <ns2:SupportID>11243</ns2:SupportID>
          <ns2:IdSetting idKey="123456">
           <ns2:IdName ns2:code="APP1">ApplicationName</ns2:IdName>
           <ns2:IdTypeText ns2:code="HLP">HelpDesk</ns2:IdTypeText>
           <ns2:Setting settingKey="3091062">
            <ns2:SupportDateTimes>2017-03-17T08:30:00-05:00</ns2:SupportDateTimes>
            <ns2:SupportSettingStatus>Open</ns2:SupportSettingStatus>
           </ns2:Setting>
          </ns2:IdSetting>
     </ns2:SupportNotification>
</ns1:SupportNotificationMsg >

Here is the transform: 这是转换:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:NS1="http://namespace1.com:8081/Organization/SupportServices" 
    xmlns:NS2="http://namespace2.com:8081/Organization/" 
    xmlns:NS3="http://namespace3.com/Common" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    version="1.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="NS1:*">
        <xsl:element name="{local-name()}"
            namespace="http://newnamespace1.com/Organization/SupportServices">
            <xsl:attribute name="xsi:schemaLocation">http://newnamespace1.com/Organization/SupportServices SupportServices.xsd</xsl:attribute>
            <xsl:copy-of select="@*" />
            <xsl:apply-templates />
        </xsl:element>
    </xsl:template>

    <xsl:template match="NS2:*">
        <xsl:element name="{local-name()}"
            namespace="http://newnamespace2.com/Organization/">
            <xsl:copy-of select="@*" />
            <xsl:apply-templates />
        </xsl:element>
    </xsl:template>

    <xsl:template match="NS3:*">
        <xsl:element name="{local-name()}"
            namespace="http://newnamespace3.com/Common">
            <xsl:copy-of select="@*" />
            <xsl:apply-templates />
        </xsl:element>
    </xsl:template>


</xsl:stylesheet>

I have added 我已经添加了

<xsl:template match="/attribute::xsi:schemaLocation"/>

or 要么

<xsl:template match="/@xsi:schemaLocation[. =  'http://namespace1.com:8081/Organization/SupportServices SupportServices.xsd']">
    <xsl:attribute name="xsi:schemaLocation">http://newnamespace1.com/Organization/SupportServices SupportServices.xsd</xsl:attribute>
</xsl:template>

It will validate even with the old schema but I'd really like to change it for my own best practice. 即使使用旧架构,它也可以验证,但我真的很想根据自己的最佳做法对其进行更改。

Any insight is greatly appreciated. 非常感谢任何见解。

Thank you, 谢谢,

Dave 戴夫

You can use the following XSLT to transform your structure as requested. 您可以使用以下XSLT根据要求转换结构。 I have added some comments to the stylesheet to hopefully help explain the transformations. 我已在样式表中添加了一些注释,以希望有助于解释转换。 Let me know if this isn't clear or still misses the mark in some way. 让我知道这是不清楚的还是仍然以某种方式错过了标记。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:NS1="http://namespace1.com:8081/Organization/SupportServices" 
  xmlns:NS2="http://namespace2.com:8081/Organization/" 
  xmlns:NS3="http://namespace3.com/Common" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  version="1.0"
>

<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>  

<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
</xsl:template>


<xsl:template match="NS1:*">
    <!-- Add element with namespace -->
    <xsl:element name="ns1:{local-name()}"
        namespace="http://newnamespace1.com/Organization/SupportServices">
            <!-- Copy all of the namespaces from the source xml but exclude
                 the ns1, ns2, ns3 original namespaces from the source xml
                 document -->
            <xsl:copy-of select="namespace::*[not(name()='ns2')
              and
               not(name()='ns3')
              and
               not(name()='ns1')]"/>  
        <xsl:apply-templates select="@* | node()"/>

    </xsl:element>
</xsl:template>    

<xsl:template match="NS2:*">
    <xsl:element name="ns2:{local-name()}"
        namespace="http://newnamespace2.com/Organization/">
        <xsl:apply-templates />
    </xsl:element>
</xsl:template>

<xsl:template match="NS3:*">
    <xsl:element name="ns3:{local-name()}"
        namespace="http://newnamespace3.com/Common">
        <xsl:apply-templates />
    </xsl:element>
</xsl:template>

<!-- replace xsi:schemaLocation attribute -->
<xsl:template match="@xsi:schemaLocation">
  <xsl:attribute name="xsi:schemaLocation">http://newnamespace1.com/Organization/SupportServices SupportServices.xsd</xsl:attribute>
</xsl:template>        
</xsl:stylesheet>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM