4

我正在尝试进行转换,但我遇到了 xsi:schemaLocation 未显示在 XML 结果中的问题。我已经搜索了为什么会发生这种情况,但我似乎无法找到它。请帮忙

输入 XML

<asx:abap version="1.0" xmlns:asx="http://www.sap.com/abapxml">
<asx:values>
    <_--5CCLASS_--3DZCL_T4_NOM_--5CTYPE_--3DTT_SAP_REQ>
        <item>
            <TECHNICAL_KEY>00006000000000326391</TECHNICAL_KEY>
            <TSCARRIER>CPL</TSCARRIER>
            <MATERIAL>V2</MATERIAL>
            <PIPELINE_CYCLE>45</PIPELINE_CYCLE>
            <PIPELINE_SEQUENCE>1</PIPELINE_SEQUENCE>
            <CYCLE_YEAR>2012</CYCLE_YEAR>
            <SCD/>
            <NOM_NUMBER>CITV2451</NOM_NUMBER>
            <DATE_TIME>2012-06-20T00:00:00</DATE_TIME>
            <CREATE_DATE_TIME>2012-06-06T18:01:18</CREATE_DATE_TIME>
            <TOTAL_ITEMS>1 </TOTAL_ITEMS>
            <T4NOMNR/>
            <ITEM_NUMBER>0000000010</ITEM_NUMBER>
            <ITEM_DETAIL_CHANGE_CODE/>
            <PIPELINE_EVENT>Delivery</PIPELINE_EVENT>
            <PIPELINE_EVENT_AFFECT>Decrease</PIPELINE_EVENT_AFFECT>
            <QUANTITY>25000.00</QUANTITY>
            <UOM>BR</UOM>
            <LOCATION>AHT</LOCATION>
            <LOCATION_PARTNER>AHT</LOCATION_PARTNER>
            <CONTRACT_PARTNER/>
            <SUPPLIER/>
            <TANKAGE_PARTNER>KM1</TANKAGE_PARTNER>
            <CONSIGNEE_PARTNER>CIT</CONSIGNEE_PARTNER>
            <CONS_SUPPLIER>Consignee</CONS_SUPPLIER>
            <TRANSPURIND>Add</TRANSPURIND>
            <VERSION>1</VERSION>
            <PARTROLEIND>Shipper</PARTROLEIND>
            <SHIPPARTID>CIT</SHIPPARTID>
            <PART_IDENTIFIER>AssignedByCarrier</PART_IDENTIFIER>
            <PROD_IDENTIFIER>AssignedByMarketplace</PROD_IDENTIFIER>
            <PART_TANKAGE>Tankage</PART_TANKAGE>
            <CARRIER_STRING>Carrier</CARRIER_STRING>
            <SHIPNOMNRSTRING>ShipperNominationNumber</SHIPNOMNRSTRING>
        </item>
    </_--5CCLASS_--3DZCL_T4_NOM_--5CTYPE_--3DTT_SAP_REQ>
</asx:values>

XSLT 代码

    <?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns="http://www.api.org/pidXML/v1.2" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:pidx="http://www.api.org/pidXML/v1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.api.org/pidXML/v1.2 https://www.transport4.com/xml/schemas/PipelineNominationCreateV1-3.xsd">
    <xsl:template match="/">
        <xsl:element name="pidx:PipelineNominationCreate">
            <xsl:attribute name="pidx:documentIdentifier"><xsl:value-of select="//TECHNICAL_KEY"/></xsl:attribute>
            <xsl:attribute name="pidx:transactionPurposeIndicator"><xsl:value-of select="//TRANSPURIND"/></xsl:attribute>
            <xsl:attribute name="pidx:version"><xsl:value-of select="//VERSION"/></xsl:attribute>
            <xsl:element name="pidx:PipelineNominationCreateProperties">
                <xsl:element name="pidx:CreatedByPartner">
                    <xsl:element name="pidx:PartnerInformation">
                        <xsl:attribute name="partnerRoleIndicator"><xsl:value-of select="//PARTROLEIND"/></xsl:attribute>
                        <xsl:element name="pidx:PartnerIdentifier">
                            <xsl:attribute name="partnerIdentifierIndicator"><xsl:value-of select="//PART_IDENTIFIER"/></xsl:attribute>
                            <xsl:value-of select="//SHIPPARTID"/>
                        </xsl:element>
                    </xsl:element>
                    <xsl:element name="pidx:EventDateTime">
                        <xsl:value-of select="//CREATE_DATE_TIME"/>
                    </xsl:element>
                </xsl:element>
                <xsl:element name="pidx:PartnerInformation">
                    <xsl:attribute name="partnerRoleIndicator"><xsl:value-of select="//PARTROLEIND"/></xsl:attribute>
                    <xsl:element name="pidx:PartnerIdentifier">
                        <xsl:attribute name="partnerIdentifierIndicator"><xsl:value-of select="//PART_IDENTIFIER"/></xsl:attribute>
                        <xsl:value-of select="//SHIPPARTID"/>
                    </xsl:element>
                </xsl:element>
                <xsl:element name="pidx:PartnerInformation">
                    <xsl:attribute name="partnerRoleIndicator"><xsl:value-of select="//CARRIER_STRING"/></xsl:attribute>
                    <xsl:element name="pidx:PartnerIdentifier">
                        <xsl:attribute name="partnerIdentifierIndicator"><xsl:value-of select="//PART_IDENTIFIER"/></xsl:attribute>
                        <xsl:value-of select="//TSCARRIER"/>
                    </xsl:element>
                </xsl:element>
                <xsl:element name="pidx:ProductInformation">
                    <xsl:element name="pidx:ProductIdentifier">
                        <xsl:attribute name="assigningOrganization"><xsl:value-of select="//PROD_IDENTIFIER"/></xsl:attribute>
                        <xsl:value-of select="//MATERIAL"/>
                    </xsl:element>
                </xsl:element>
                <xsl:element name="pidx:PipelineCycle">
                    <xsl:value-of select="//PIPELINE_CYCLE"/>
                </xsl:element>
                <xsl:element name="pidx:PipelineCycleYear">
                    <xsl:value-of select="//CYCLE_YEAR"/>
                </xsl:element>
                <xsl:element name="pidx:PipelineSequence">
                    <xsl:value-of select="//PIPLELINE_SEQUENCE"/>
                </xsl:element>
                <xsl:element name="pidx:PipelineSCD">
                    <xsl:value-of select="//SCD"/>
                </xsl:element>
                <xsl:element name="pidx:ReferenceInformation">
                    <xsl:attribute name="referenceInformationIndicator"><xsl:value-of select="//SHIPNOMNRSTRING"/></xsl:attribute>
                    <xsl:element name="pidx:ReferenceNumber">
                        <xsl:value-of select="//NOM_NUMBER"/>
                    </xsl:element>
                    <xsl:element name="pidx:Description">
                    </xsl:element>
                </xsl:element>
            </xsl:element>
            <xsl:element name="pidx:PipelineNominationCreateDetails">
                <xsl:for-each select="//item">
                    <xsl:element name="pidx:PipelineNominationCreateLineItem">
                        <xsl:element name="pidx:LineItemNumber">
                            <xsl:value-of select="ITEM_NUMBER"/>
                        </xsl:element>
                        <xsl:element name="pidx:LineItemNominationQuantity">
                            <xsl:element name="pidx:Quantity">
                                <xsl:value-of select="QUANTITY"/>
                            </xsl:element>
                            <xsl:element name="pidx:UnitOfMeasureCode">
                                <xsl:value-of select="UOM"/>
                            </xsl:element>
                        </xsl:element>
                        <xsl:element name="pidx:PartnerInformation">
                            <xsl:attribute name="partnerRoleIndicator"><xsl:value-of select="CONS_SUPPLIER"/></xsl:attribute>
                            <xsl:element name="pidx:PartnerIdentifier">
                                <xsl:attribute name="partnerIdentifierIndicator"><xsl:value-of select="//PART_IDENTIFIER"/></xsl:attribute>
                                <xsl:value-of select="CONSIGNEE_PARTNER"/>
                            </xsl:element>
                        </xsl:element>
                        <xsl:element name="pidx:PartnerInformation">
                            <xsl:attribute name="partnerRoleIndicator"><xsl:value-of select="PART_TANKAGE"/></xsl:attribute>
                            <xsl:element name="pidx:PartnerIdentifier">
                                <xsl:attribute name="partnerIdentifierIndicator"><xsl:value-of select="//PART_IDENTIFIER"/></xsl:attribute>
                                <xsl:value-of select="TANKAGE_PARTNER"/>
                            </xsl:element>
                        </xsl:element>
                        <xsl:element name="pidx:ScheduleStartDateTime">
                            <xsl:value-of select="DATE_TIME"/>
                        </xsl:element>
                        <xsl:element name="pidx:ScheduleRequestDateTime">
                    </xsl:element>
                        <xsl:element name="pidx:PipelineEventInformation">
                            <xsl:element name="pidx:PipelineEvent">
                                <xsl:value-of select="PIPELINE_EVENT"/>
                            </xsl:element>
                            <xsl:element name="pidx:PipelineEventVolumeAffect">
                                <xsl:value-of select="PIPELINE_EVENT_AFFECT"/>
                            </xsl:element>
                        </xsl:element>
                        <xsl:element name="pidx:CustodyLocationInformation">
                            <xsl:element name="pidx:CustodyLocationIdentifier">
                                <xsl:attribute name="identifierIndicator"><xsl:value-of select="//PART_IDENTIFIER"/></xsl:attribute>
                                <xsl:value-of select="LOCATION_PARTNER"/>
                            </xsl:element>
                        </xsl:element>
                        <xsl:element name="pidx:ReferenceInformation">
                            <xsl:attribute name="referenceInformationIndicator">
                        </xsl:attribute>
                            <xsl:element name="pidx:ReferenceNumber">
                        </xsl:element>
                            <xsl:element name="pidx:Description">
                        </xsl:element>
                        </xsl:element>
                        <xsl:element name="pidx:Comment">
                        </xsl:element>
                    </xsl:element>
                </xsl:for-each>
            </xsl:element>
            <xsl:element name="pidx:PipelineNominationCreateSummary">
                <xsl:element name="pidx:TotalLineItems">
                    <xsl:value-of select="//TOTAL_ITEMS"/>
                </xsl:element>
            </xsl:element>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

缺少 schemaLocation 的结果 XML

    <?xml version="1.0" encoding="iso-8859-1"?>
<pidx:PipelineNominationCreate xmlns:pidx="http://www.api.org/pidXML/v1.2" xmlns="http://www.api.org/pidXML/v1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" pidx:documentIdentifier="00006000000000326391" pidx:transactionPurposeIndicator="Add" pidx:version="1">
    <pidx:PipelineNominationCreateProperties>
        <pidx:CreatedByPartner>
            <pidx:PartnerInformation partnerRoleIndicator="Shipper">
                <pidx:PartnerIdentifier partnerIdentifierIndicator="AssignedByCarrier">CIT</pidx:PartnerIdentifier>
            </pidx:PartnerInformation>
            <pidx:EventDateTime>2012-06-07T15:55:35</pidx:EventDateTime>
        </pidx:CreatedByPartner>
        <pidx:PartnerInformation partnerRoleIndicator="Shipper">
            <pidx:PartnerIdentifier partnerIdentifierIndicator="AssignedByCarrier">CIT</pidx:PartnerIdentifier>
        </pidx:PartnerInformation>
        <pidx:PartnerInformation partnerRoleIndicator="Carrier">
            <pidx:PartnerIdentifier partnerIdentifierIndicator="AssignedByCarrier">CPL</pidx:PartnerIdentifier>
        </pidx:PartnerInformation>
        <pidx:ProductInformation>
            <pidx:ProductIdentifier assigningOrganization="AssignedByMarketplace">V2</pidx:ProductIdentifier>
        </pidx:ProductInformation>
        <pidx:PipelineCycle>45</pidx:PipelineCycle>
        <pidx:PipelineCycleYear>2012</pidx:PipelineCycleYear>
        <pidx:PipelineSequence/>
        <pidx:PipelineSCD/>
        <pidx:ReferenceInformation referenceInformationIndicator="ShipperNominationNumber">
            <pidx:ReferenceNumber>CITV2451</pidx:ReferenceNumber>
            <pidx:Description/>
        </pidx:ReferenceInformation>
    </pidx:PipelineNominationCreateProperties>
    <pidx:PipelineNominationCreateDetails>
        <pidx:PipelineNominationCreateLineItem>
            <pidx:LineItemNumber>0000000010</pidx:LineItemNumber>
            <pidx:LineItemNominationQuantity>
                <pidx:Quantity>25000.0</pidx:Quantity>
                <pidx:UnitOfMeasureCode>BR</pidx:UnitOfMeasureCode>
            </pidx:LineItemNominationQuantity>
            <pidx:PartnerInformation partnerRoleIndicator="Consignee">
                <pidx:PartnerIdentifier partnerIdentifierIndicator="AssignedByCarrier">CIT</pidx:PartnerIdentifier>
            </pidx:PartnerInformation>
            <pidx:PartnerInformation partnerRoleIndicator="Tankage">
                <pidx:PartnerIdentifier partnerIdentifierIndicator="AssignedByCarrier">KM1</pidx:PartnerIdentifier>
            </pidx:PartnerInformation>
            <pidx:ScheduleStartDateTime>2012-06-20T00:00:00</pidx:ScheduleStartDateTime>
            <pidx:ScheduleRequestDateTime/>
            <pidx:PipelineEventInformation>
                <pidx:PipelineEvent>Delivery</pidx:PipelineEvent>
                <pidx:PipelineEventVolumeAffect>Decrease</pidx:PipelineEventVolumeAffect>
            </pidx:PipelineEventInformation>
            <pidx:CustodyLocationInformation>
                <pidx:CustodyLocationIdentifier identifierIndicator="AssignedByCarrier">AHT</pidx:CustodyLocationIdentifier>
            </pidx:CustodyLocationInformation>
            <pidx:ReferenceInformation referenceInformationIndicator="">
                <pidx:ReferenceNumber/>
                <pidx:Description/>
            </pidx:ReferenceInformation>
            <pidx:Comment/>
        </pidx:PipelineNominationCreateLineItem>
    </pidx:PipelineNominationCreateDetails>
    <pidx:PipelineNominationCreateSummary>
        <pidx:TotalLineItems>1 </pidx:TotalLineItems>
    </pidx:PipelineNominationCreateSummary>
</pidx:PipelineNominationCreate>

任何帮助,将不胜感激。

谢谢!

4

3 回答 3

7

您已经xsi:schemaLocation在 XSLT 文档中进行了定义,但是您在 XSLT 中的任何地方都没有尝试在 XML 文档中创建它。它不会自动添加到您的输出文档中。您将需要创建它。

xsi:schemaLocation您可以像这样从 XSLT 文档中复制属性:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
  xmlns="http://www.api.org/pidXML/v1.2" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:pidx="http://www.api.org/pidXML/v1.2" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.api.org/pidXML/v1.2 https://www.transport4.com/xml/schemas/PipelineNominationCreateV1-3.xsd">
    <xsl:template match="/">
        <xsl:element name="pidx:PipelineNominationCreate">
            <xsl:copy-of select="document('')/*/@xsi:schemaLocation"/>
于 2012-06-08T01:48:03.283 回答
2

您是否尝试过xsi:schemaLocation在 XSLT 中显式地添加/复制属性,就像添加/复制任何其他属性一样?例如,在该行之后

<xsl:element name="pidx:PipelineNominationCreate">

尝试添加这个:

<xsl:copy-of select="/node()/@*[local-name()='schemaLocation']"/>

schemaLocation这应该从原始文档的根节点复制任何属性(不管它们的命名空间前缀)。

于 2012-06-07T22:29:19.543 回答
2

就像其他人所说的那样,该属性不存在于输出中,因为您没有尝试将其放在那里。

您是否知道,如果您使用文字结果元素,您的代码可能不会那么冗长?例如,这个:

<xsl:element name="pidx:PipelineNominationCreate">
            <xsl:attribute name="pidx:documentIdentifier"><xsl:value-of select="//TECHNICAL_KEY"/></xsl:attribute>
            <xsl:attribute name="pidx:transactionPurposeIndicator"><xsl:value-of select="//TRANSPURIND"/></xsl:attribute>
            <xsl:attribute name="pidx:version"><xsl:value-of select="//VERSION"/></xsl:attribute>
            <xsl:element name="pidx:PipelineNominationCreateProperties">
                <xsl:element name="pidx:CreatedByPartner">
                    <xsl:element name="pidx:PartnerInformation">
                        <xsl:attribute name="partnerRoleIndicator"><xsl:value-of select="//PARTROLEIND"/></xsl:attribute>
                        <xsl:element name="pidx:PartnerIdentifier">
                            <xsl:attribute name="partnerIdentifierIndicator"><xsl:value-of select="//PART_IDENTIFIER"/></xsl:attribute>
                            <xsl:value-of select="//SHIPPARTID"/>
                        </xsl:element>
                    </xsl:element>
                    <xsl:element name="pidx:EventDateTime">
                        <xsl:value-of select="//CREATE_DATE_TIME"/>
                    </xsl:element>
                </xsl:element>

可以这样重写:

<pidx:PipelineNominationCreate 
  pidx:documentIdentifier="{//TECHNICAL_KEY}"
  pidx:transactionPurposeIndicator="{//TRANSPURIND}"
  pidx:version="{//VERSION}">
  <pidx:PipelineNominationCreateProperties>
     <pidx:CreatedByPartner>
        <pidx:PartnerInformation partnerRoleIndicator="{//PARTROLEIND}">
            <pidx:PartnerIdentifier 
                partnerIdentifierIndicator="{//PART_IDENTIFIER}">
                <xsl:value-of select="//SHIPPARTID"/>
            </pidx:PartnerIdentifier>
        </pidx:PartnerInformation>
        <pidx:EventDateTime>
            <xsl:value-of select="//CREATE_DATE_TIME"/>
        </pidx:EventDateTime>

这更具可读性。

于 2012-06-08T08:27:00.920 回答