XSLT:xsi:schemaLocation未在XML上显示

时间:2012-06-07 22:06:54

标签: xslt

我正在尝试进行转换,但我遇到的问题是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>

任何帮助都将不胜感激。

谢谢!

3 个答案:

答案 0 :(得分:6)

您已在XSLT文档中定义了xsi:schemaLocation,但您在XSLT中没有尝试在XML文档中创建它。它不会自动添加到输出文档中。你需要创建它。

您可以从XSLT文档中复制xsi:schemaLocation属性,如下所示:

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

答案 1 :(得分:2)

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

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

尝试添加:

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

这应该从原始文档的根节点复制任何schemaLocation属性(无论其名称空间前缀如何)。

答案 2 :(得分: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>

更具可读性。