简体   繁体   English

转换xslt不会转换所有元素

[英]Transform xslt not transforming all elements

I am importing an xml file into Access, and using an xslt file to transform the file to include the orderNumber for each section. 我正在将XML文件导入Access,并使用xslt文件将文件转换为包括每个部分的orderNumber。 Not sure of the proper term. 不确定适当的术语。 In the code below I am getting the field and orderNumber added for the items, and only the field for the other sections, shippingAddress, payment, etc.. 在下面的代码中,我将为项目添加字段和orderNumber,而仅为其他部分,shippingAddress,付款等添加字段。

I do not quite understand were I need to make changes to get the data for orderNumber added to the fields. 我不太了解我是否需要进行更改才能将orderNumber的数据添加到字段中。

Any help would be greatly appreciated. 任何帮助将不胜感激。

Greg 格雷格

This is what I am given to work with. 这就是我的工作目标。

 <?xml version="1.0" encoding="UTF-8"?>
-<orders xmlns:php="http://php.net/xsl">
-<order>
        <orderNumber>100000379</orderNumber>
-<billingAddress>
            <company>Acme</company>
            <name>John Doe</name>
            <address1>59 Any St</address1>
            <address2/>
            <city>New York</city>
            <state>NY</state>
            <zip>10013-4020</zip>
            <country>US</country>
            <phone>212-555-1212</phone>
        </billingAddress>
-<shippingAddress>
            <company>Acme</company>
            <name>John Doe</name>
            <address1>59 Any St</address1>
            <address2/>
            <city>New York</city>
            <state>NY</state>
            <zip>10013-4020</zip>
            <country>US</country>
            <phone>212-555-1212</phone>
        </shippingAddress>
        <createdOn>2016-06-22 14:38:06</createdOn>
        <shipMethod>ups_GND</shipMethod>
        <shipDescription>United Parcel Service - Ground</shipDescription>
-<payment>
            <tax>0</tax>
            <shipmentCost>26.76</shipmentCost>
            <subtotalExclTax>1616</subtotalExclTax>
            <subtotalInclTax>1616</subtotalInclTax>
            <discount>0</discount>
            <grandTotal>1642.76</grandTotal>
            <paymentMethod>purchaseorder</paymentMethod>
        </payment>
-<items>
-<item>
                <itemId>645</itemId>
                <productId>171</productId>
                <sku>12749</sku>
                <qty>1</qty>
                <price>858</price>
                <tax>0</tax>
                <itemTotal>858</itemTotal>
            </item>
-<item>
                <itemId>646</itemId>
                <productId>178</productId>
                <sku>127478</sku>
                <qty>1</qty>
                <price>758</price>
                <tax>0</tax>
                <itemTotal>758</itemTotal>
            </item>
        </items>
    </order>
</orders>

This is the xslt code. 这是xslt代码。 I found this as an example, but I can't seem to make it work. 我以这个为例,但似乎无法实现。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>

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

    </dataroot>
</xsl:template>

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

    </xsl:copy>
</xsl:template>

<xsl:template match="billingAddress">
    <billingAddress>
        <orderNumber><xsl:value-of select="../../orderNumber"/></orderNumber>
        <xsl:apply-templates select="node()"/>
    </billingAddress>
</xsl:template>

<xsl:template match="shippingAddress">
    <shippingAddress>
        <orderNumber><xsl:value-of select="../../orderNumber"/></orderNumber>
        <xsl:apply-templates select="node()"/>
    </shippingAddress>
</xsl:template>

<xsl:template match="payment">
    <payment>
        <orderNumber><xsl:value-of select="../../orderNumber"/></orderNumber>
        <xsl:apply-templates select="node()"/>
    </payment>
</xsl:template>

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

 <xsl:template match="item">
    <item>
        <orderNumber><xsl:value-of select="../../orderNumber"/></orderNumber>
        <xsl:apply-templates select="@*|node()"/>
    </item>
</xsl:template>

The <orderNumber> element is a sibling of <billingAddress> , <shippingAddress> , <payment> but a level above <items> . <orderNumber>元素是<billingAddress><shippingAddress><payment>的同级元素,但在<items>之上。 So your context level directive ../../ only works for <item> node. 因此,您的上下文级别指令../../仅适用于<item>节点。

Simply replace: 只需更换:

<xsl:value-of select="../../orderNumber"/>

with a common expression as <order> is the ancestor to all the required nodes: 通用表达式为<order>的祖先是所有必需节点的祖先:

<xsl:value-of select="ancestor::order/orderNumber"/>

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

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