使用XSLT添加新的XML元素

时间:2014-09-17 20:39:46

标签: xslt element

我有要求我需要添加新元素。任何人都可以帮助我做错了吗?我附加了示例输入,示例输出,并且我附上了我正在使用的代码:)

示例输入:

<soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
                <OrderCreate Version="5.1.1" xmlns="urn:midx:names:specification:ces:schema:all:5:1:1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                                <Header>
                                        <ThisDocumentIdentifier>
                                                <DocumentIdentifier>58585993</DocumentIdentifier>
                                        </ThisDocumentIdentifier>
                                        <ThisDocumentDateTime>
                                                <DateTime DateTimeQualifier="On">2014-08-22T00:00:00Z</DateTime>
                                        </ThisDocumentDateTime>
                                        <From>
                                                <PartnerInformation>
                                                        <PartnerName>MOSLLC</PartnerName>
                                                        <PartnerIdentifier Agency="IBM-EBID">0089646370000</PartnerIdentifier>
                                                        <ContactInformation>
                                                                <ContactName>MOSLLC </ContactName>
                                                                <ContactName>804-281-1402</ContactName>
                                                                <EmailAddress></EmailAddress>
                                                        </ContactInformation>
                                                </PartnerInformation>
                                        </From>
                                        <To>
                                                <PartnerInformation>
                                                        <PartnerName>Walmart</PartnerName>
                                                        <PartnerIdentifier Agency="IBM-EBID">0062668030000</PartnerIdentifier>
                                                </PartnerInformation>
                                        </To>
                                </Header>
                                <OrderCreateBody>
                                        <OrderCreateProperties>
                                                <PurchaseOrderNumber>
                                                        <DocumentIdentifier>OD3157538</DocumentIdentifier>
                                                </PurchaseOrderNumber>
                                                <PurchaseOrderTypeCode Domain="ANSI-ASC-X12-92">KN</PurchaseOrderTypeCode>
                                                <PurchaseOrderIssuedDate>
                                                        <DateTime DateTimeQualifier="On">2014-08-22T00:00:00Z</DateTime>
                                                </PurchaseOrderIssuedDate>
                                                <LanguageCode Domain="ISO-639-2T">eng</LanguageCode>
                                                <CurrencyCode Domain="ISO-4217">USD</CurrencyCode>
                                                <BuyerSequenceNumber>0</BuyerSequenceNumber>
                                                <DeliveryTerms>
                                                        <DeliveryTermsCode Domain="Incoterms-2000">CPT</DeliveryTermsCode>
                                                        <DeliveryTermsLocation>ORIGIN</DeliveryTermsLocation>
                                                </DeliveryTerms>
                                                <ShipmentMethodOfPaymentCode Domain="ANSI-ASC-X12-146">PC</ShipmentMethodOfPaymentCode>
                                                <PaymentTerms>
                                                        <PaymentTermsOfSale>
                                                                <TermsOfSaleDescription>DUE 25TH OF FOLLOWING MONTH</TermsOfSaleDescription>
                                                                <NetDaysDue>0</NetDaysDue>
                                                        </PaymentTermsOfSale>
                                                </PaymentTerms>
                                        </OrderCreateProperties>
                                        <OrderCreatePartners>
                                                <Buyer>
                                                        <PartnerInformation>
                                                                <PartnerName>MOSLLC</PartnerName>
                                                                <PartnerIdentifier Agency="IBM-EBID">0089646370000</PartnerIdentifier>
                                                                <ContactInformation>
                                                                        <ContactName>MOSLLC LLC</ContactName>
                                                                        <ContactNumber>804-281-1402</ContactNumber>
                                                                        <EmailAddress>Jim.Paul@sscoop.com</EmailAddress>
                                                                </ContactInformation>
                                                        </PartnerInformation>
                                                </Buyer>
                                                <Seller>
                                                        <PartnerInformation>
                                                                <PartnerName>WalmartPartnerName</PartnerName
                                                                <PartnerIdentifier Agency="IBM-EBID">0062668030000</PartnerIdentifier>
                                                        </PartnerInformation>
                                                </Seller>
                                                <ShipTo>
                                                        <PartnerInformation>
                                                                <PartnerName>ORANGE MADISON COOP ORANGE BR</PartnerName>
                                                                <PartnerIdentifier Agency="IBM-EBID">0238626420000</PartnerIdentifier>
                                                                <AddressInformation>
                                                                        <AddressLine>13323 JAMES MADISON HWY</AddressLine>
                                                                        <CityName>ORANGE</CityName>
                                                                        <StateOrProvince>VA</StateOrProvince>
                                                                        <PostalCode>22960</PostalCode>
                                                                        <PostalCountry>US</PostalCountry>
                                                                </AddressInformation>
                                                        </PartnerInformation>
                                                </ShipTo>
                                                <Payer>
                                                        <PartnerInformation>
                                                                <PartnerName>MOSLLC</PartnerName>
                                                                <PartnerIdentifier Agency="IBM-EBID">0089646370000</PartnerIdentifier>
                                                        </PartnerInformation>
                                                </Payer>
                                        </OrderCreatePartners>
                                        <OrderCreateDetails>
                                                <OrderCreateProductLineItem>
                                                        <LineNumber>1</LineNumber>
                                                        <PurchaseOrderLineItemNumber>1</PurchaseOrderLineItemNumber>
                                                        <ProductIdentification>
                                                                <ProductIdentifier Agency="AssignedByManufacturer">883580921503</ProductIdentifier>
                                                                <ProductName>HARNESS XTRA 5.6L RUP BULK</ProductName>
                                                                <ProductDescription>HARNESS XTRA 5.6L RUP BULK</ProductDescription>
                                                        </ProductIdentification>
                                                        <ProductQuantity>
                                                                <Measurement>
                                                                        <MeasurementValue>1000</MeasurementValue>
                                                                        <UnitOfMeasureCode Domain="UN-Rec-20">GLL</UnitOfMeasureCode>
                                                                </Measurement>
                                                        </ProductQuantity>
                                                        <ScheduleDateTimeInformation ScheduleType="RequestedDelivery">
                                                                <DateTimeInformation>
                                                                        <DateTime DateTimeQualifier="On">2014-08-22T00:00:00Z</DateTime>
                                                                </DateTimeInformation>
                                                        </ScheduleDateTimeInformation>
                                                </OrderCreateProductLineItem>
                                        </OrderCreateDetails>
                                </OrderCreateBody>
                        </OrderCreate>
                </soapenv:Body>

预期产出:

<?xml version="1.0" encoding="UTF-8"?><soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
            <OrderCreate Version="2.0.2">
                            <Header xmlns="urn:midx:names:specification:ces:schema:all:5:1:1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                                    <ThisDocumentIdentifier>
                                            <DocumentIdentifier>58585993</DocumentIdentifier>
                                    </ThisDocumentIdentifier>
                                    <ThisDocumentDateTime>
                                            <DateTime xmlns="" DateTimeQualifier="On">20140822000000</DateTime>
                                    </ThisDocumentDateTime>
                                    <From>
                                            <PartnerInformation>
                                                    <PartnerName>MOSLLC</PartnerName>
                                                    <PartnerIdentifier Agency="IBM-EBID">0089646370000</PartnerIdentifier>
                                                    <ContactInformation>
                                                            <ContactName>MOSLLC </ContactName>
                                                            <ContactName>804-281-1402</ContactName>
                                                            <EmailAddress/>
                                                    </ContactInformation>
                                            </PartnerInformation>
                                    </From>
                                    <To>
                                            <PartnerInformation>
                                                    <PartnerName>Walmart</PartnerName>
                                                    <PartnerIdentifier Agency="IBM-EBID">0062668030000</PartnerIdentifier>
                                            </PartnerInformation>
                                    </To>
                            </Header>
                            <OrderCreateBody xmlns="urn:midx:names:specification:ces:schema:all:5:1:1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                                    <OrderCreateProperties>
                                            <PurchaseOrderNumber>
                                                    <DocumentIdentifier>OD3157538</DocumentIdentifier>
                                            </PurchaseOrderNumber>
                                            <PurchaseOrderTypeCode Domain="ANSI ASC X12 92">KN</PurchaseOrderTypeCode>
                                            <PurchaseOrderIssuedDate>
                                                    <DateTime xmlns="" DateTimeQualifier="On">20140822000000</DateTime>
                                            </PurchaseOrderIssuedDate>
                                            <LanguageCode Domain="ISO 639-2/T">eng</LanguageCode>
                                            <CurrencyCode Domain="ISO 4217">USD</CurrencyCode>
                                            <BuyerSequenceNumber>0</BuyerSequenceNumber>
                                            <DeliveryTerms>
                                                    <DeliveryTermsCode Domain="Incoterms-2000">CPT</DeliveryTermsCode>
                                                    <DeliveryTermsLocation>ORIGIN</DeliveryTermsLocation>
                                            </DeliveryTerms>
                                            <ShipmentMethodOfPaymentCode Domain="ANSI-ASC-X12-146">PC</ShipmentMethodOfPaymentCode>
                                            <PaymentTerms>
                                                    <PaymentTermsOfSale>
                                                            <TermsOfSaleDescription>DUE 25TH OF FOLLOWING MONTH</TermsOfSaleDescription>
                                                            <NetDaysDue>0</NetDaysDue>
                                                    </PaymentTermsOfSale>
                                            </PaymentTerms>
                                    </OrderCreateProperties>
                                    <OrderCreatePartners>
                                            <Buyer>
                                                    <PartnerInformation>
                                                            <PartnerName>MOSLLC</PartnerName>
                                                            <PartnerIdentifier Agency="IBM-EBID">0089646370000</PartnerIdentifier>
                                                            <ContactInformation>
                                                                    <ContactName>MOSLLC LLC</ContactName>
                                                                    <TelephoneNumber>
                                                                   <NationalPhoneNumber>813-217-9512</NationalPhoneNumber>
                                                                    </TelephoneNumber>
                                                                    <ContactNumber>804-281-1402</ContactNumber>
                                                                    <EmailAddress>Jim.Paul@sscoop.com</EmailAddress>
                                                            </ContactInformation>

                                                    </PartnerInformation>
                                            </Buyer>
                                            <Seller>
                                                    <PartnerInformation>
                                                            <PartnerName>WalmartPartnerName</PartnerName>
                                                            <PartnerIdentifier Agency="IBM-EBID">0062668030000</PartnerIdentifier>
                                                    </PartnerInformation>
                                            </Seller>
                                            <ShipTo>
                                                    <PartnerInformation>
                                                            <PartnerName>ORANGE MADISON COOP ORANGE BR</PartnerName>
                                                            <PartnerIdentifier Agency="IBM-EBID">0238626420000</PartnerIdentifier>
                                                            <AddressInformation>
                                                                    <AddressLine>13323 JAMES MADISON HWY</AddressLine>
                                                                    <CityName>ORANGE</CityName>
                                                                    <StateOrProvince>VA</StateOrProvince>
                                                                    <PostalCode>22960</PostalCode>
                                                                    <PostalCountry>US</PostalCountry>
                                                            </AddressInformation>
                                                    </PartnerInformation>
                                            </ShipTo>
                                            <Payer>
                                                    <PartnerInformation>
                                                            <PartnerName>MOSLLC</PartnerName>
                                                            <PartnerIdentifier Agency="IBM-EBID">0089646370000</PartnerIdentifier>
                                                    </PartnerInformation>
                                            </Payer>
                                    </OrderCreatePartners>
                                    <OrderCreateDetails>
                                            <OrderCreateProductLineItem>
                                                    <LineNumber>1</LineNumber>
                                                    <PurchaseOrderLineItemNumber>1</PurchaseOrderLineItemNumber>
                                                    <ProductIdentification>
                                                            <ProductIdentifier Agency="AssignedByManufacturer">883580921503</ProductIdentifier>
                                                            <ProductName>HARNESS XTRA 5.6L RUP BULK</ProductName>
                                                            <ProductDescription>HARNESS XTRA 5.6L RUP BULK</ProductDescription>
                                                    </ProductIdentification>
                                                    <ProductQuantity>
                                                            <Measurement>
                                                                    <MeasurementValue>1000</MeasurementValue>
                                                                    <UnitOfMeasureCode Domain="UN-Rec-20">GLL</UnitOfMeasureCode>
                                                            </Measurement>
                                                    </ProductQuantity>
                                                    <ScheduleDateTimeInformation ScheduleType="RequestedDelivery">
                                                            <DateTimeInformation>
                                                                    <DateTime xmlns="" DateTimeQualifier="On">20140822000000</DateTime>
                                                            </DateTimeInformation>
                                                    </ScheduleDateTimeInformation>
                                            </OrderCreateProductLineItem>
                                    </OrderCreateDetails>
                            </OrderCreateBody>
                    </OrderCreate>
            </soapenv:Body>

这是需要添加的部分

在元素买家/ ContactDescription之后,我需要添加此

 <TelephoneNumber>
<NationalPhoneNumber>605225-1372</NationalPhoneNumber>
</TelephoneNumber>

这是我写的示例代码。有人可以指导我吗?

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dp="http://www.datapower.com/extensions" extension-element-prefixes="dp">
    <xsl:output method="xml"/>
    <xsl:template match="attribute::Domain[starts-with(name(..),'PurchaseOrderType')]">
        <xsl:attribute name="Domain"><xsl:value-of select="translate(., '-', ' ')"/></xsl:attribute>
    </xsl:template>
    <xsl:template match="attribute::*">
        <xsl:copy/>
    </xsl:template>
    <xsl:template match="attribute::Domain[starts-with(name(..),'CurrencyCode')]">
        <xsl:attribute name="Domain"><xsl:value-of select="translate(., '-', ' ')"/></xsl:attribute>
    </xsl:template>
    <xsl:template match="attribute::*">
        <xsl:copy/>
    </xsl:template>
    <!--    <xsl:template match="attribute::Domain[starts-with(name(..),'LanguageCode')]">
<xsl:attribute name="Domain"><xsl:value-of select="translate('.', '-', ' ')"/>
</xsl:attribute>
    </xsl:template>
-->
    <xsl:template match="attribute::Domain[starts-with(name(..),'LanguageCode')]">
        <xsl:attribute name="Domain"><xsl:value-of select="concat('ISO ',substring-after(substring-before(.,'T'),'-'), '/T')"/></xsl:attribute>
    </xsl:template>
    <xsl:template match="attribute::*">
        <xsl:copy/>
    </xsl:template>
    <xsl:template match="*[local-name()='DateTime']">
        <DateTime>
            <xsl:copy-of select="attribute::*"/>
            <xsl:value-of select="translate(., '-T:Z', '')"/>
        </DateTime>
    </xsl:template>
    <xsl:template match="*[local-name()='OrderCreate']">
        <OrderCreate Version="2.0.2">
            <xsl:apply-templates select="node()"/>
        </OrderCreate>
    </xsl:template>
    <xsl:template match="*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="attribute::*|node()"/>
        </xsl:element>
        <!--Adding the elements
        <TelephoneNumber>
        <NationalPhoneNumber>605225-1372</NationalPhoneNumber>
        </TelephoneNumber>-->
    </xsl:template>
    <xsl:template match="/*">
        <xsl:copy>
            <xsl:copy-of select="attribute::*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="Buyer/ContactName">
        <xsl:copy-of select="."/>
        <xsl:element name="TelephoneNumber">
             <xsl:attribute name="NationalPhoneNumber">605225-1372</xsl:attribute>
        </xsl:element>
        <!--<TelephoneNumber>
            <NationalPhoneNumber>605225-1372</NationalPhoneNumber>
        </TelephoneNumber>-->
    </xsl:template>
</xsl:stylesheet>

2 个答案:

答案 0 :(得分:1)

要解决您的问题,您需要进行两项更改:

  1. 使用一些前缀在根标记中指定文档的默认命名空间,例如“我的”:
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    xmlns:my="urn:midx:names:specification:ces:schema:all:5:1:1"
                    xmlns:dp="http://www.datapower.com/extensions" extension-element-prefixes="dp">
    
    1. 修复您的买方模板:
    2. <xsl:template match="my:Buyer/my:PartnerInformation/my:ContactInformation/my:ContactName">
        <xsl:copy-of select="."/>
        <TelephoneNumber xmlns="urn:midx:names:specification:ces:schema:all:5:1:1">
          <NationalPhoneNumber>605225-1372</NationalPhoneNumber>
        </TelephoneNumber>
      </xsl:template>
      

答案 1 :(得分:0)

Thank you Lagivan and Mathias. Can you please explain the use of adding the namespace?

答案太长了,无法在评论中添加,这就是为什么我写一个单独的答案,即使@lagivan已经给出了代码方面的答案。

添加命名空间解决问题的原因是具有命名空间的元素与没有命名空间的元素不同,即使它们的元素名称相同。或换句话说,

<ns:number xmlns:ns="www.ns.com"/>

完全不同
<number>

ns:number被称为前缀,这是表示它属于某个名称空间的简写,在本例中为www.ns.com。此外,还有一种方法可以定义默认命名空间

<number xmlns="www.ns.com">
  <digit/>
</number>

在上面的示例中,number元素上定义了默认命名空间。这对number元素本身没有任何影响,但number的所有后代(在这种情况下只有digit)将采用此命名空间。


现在,针对您的具体问题,您要匹配的元素(ContactName)本身并不具有前缀,但它具有默认命名空间。此默认命名空间未在ContactName元素上声明,但在OrderCreate元素的树中更高:

<OrderCreate Version="5.1.1" xmlns="urn:midx:names:specification:ces:schema:all:5:1:1">

OrderCreate的任何后代都将位于名称空间urn:midx:names:specification:ces:schema:all:5:1:1中,例如...... ContactName

这对您的XSLT代码意味着,如果要匹配输入中的ContactName元素,则必须在XSLT样式表中声明此命名空间,并将前缀元素名称声明为能够找到它。

<xsl:stylesheet xmlns:my="urn:midx:names:specification:ces:schema:all:5:1:1">

现在,您可以编写模板以匹配OrderCreate的后代元素,并始终在其前面添加my:

<xsl:template match="my:ContactName">

另一方面,

<xsl:template match="ContactName">

将永远不会匹配任何内容,因为您的输入XML中不存在这样的元素(一个名为&#34; ContactName&#34;以及一个不在任何名称空间中的元素)。