Как правильно использоватьсвывести несколько файлов XML? - PullRequest
0 голосов
/ 11 июля 2019

Я хочу преобразовать один XML-файл в несколько, основываясь на определенном узле и количестве их в XML-файле.XML, которым я хочу манипулировать, выглядит следующим образом.

<?xml version="1.0" encoding="utf-8"?>
<PurchaseOrder xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <StoreFrontName>Store front name</StoreFrontName>
    <PurchaseOrderNumber>7291</PurchaseOrderNumber>
    <PurchaseOrderDate>2019-07-09</PurchaseOrderDate>
    <OrdByAddress>
        <Name>First Name Last Nam</Name>
    </OrdByAddress>
    <PurchaseOrderItemDetail id="1">
        <LineNumber>2224</LineNumber>
        <VendorProductID/>
        <VendorProductDescription>Vend Product Desc</VendorProductDescription>
        <CustomerProductID/>
        <CustomerProductDescription>Cust Prod Desc</CustomerProductDescription>
        <OrderQuantity>1</OrderQuantity>
    </PurchaseOrderItemDetail>
    <PurchaseOrderItemDetail id="2">
        <LineNumber>2219</LineNumber>
        <VendorProductID/>
        <VendorProductDescription>Vend Product Desc</VendorProductDescription>
        <CustomerProductID/>
        <CustomerProductDescription>Cust Prod Desc</CustomerProductDescription>
        <OrderQuantity>1</OrderQuantity>
    </PurchaseOrderItemDetail>
    <PurchaseOrderItemDetail id="3">
        <LineNumber>2220</LineNumber>
        <VendorProductID/>
        <VendorProductDescription>Vend Product Desc</VendorProductDescription>
        <CustomerProductID/>
        <CustomerProductDescription>Cust Prod Desc</CustomerProductDescription>
        <OrderQuantity>1</OrderQuantity>
    </PurchaseOrderItemDetail>
    <PurchaseOrderItemDetail id="4">
        <LineNumber>2221</LineNumber>
        <VendorProductID/>
        <VendorProductDescription>Vend Product Desc</VendorProductDescription>
        <CustomerProductID/>
        <CustomerProductDescription>Cust Prod Desc</CustomerProductDescription>
        <OrderQuantity>1</OrderQuantity>
    </PurchaseOrderItemDetail>
    <PurchaseOrderItemDetail id="5">
        <LineNumber>2222</LineNumber>
        <VendorProductID/>
        <VendorProductDescription>Vend Product Desc</VendorProductDescription>
        <CustomerProductID/>
        <CustomerProductDescription>Cust Prod Desc</CustomerProductDescription>
        <OrderQuantity>1</OrderQuantity>
    </PurchaseOrderItemDetail>
    <PurchaseOrderItemDetail id="6">
        <LineNumber>2223</LineNumber>
        <VendorProductID/>
        <VendorProductDescription>Vend Product Desc</VendorProductDescription>
        <CustomerProductID/>
        <CustomerProductDescription>Cust Prod Desc</CustomerProductDescription>
        <OrderQuantity>1</OrderQuantity>
    </PurchaseOrderItemDetail>
</PurchaseOrder>

Мой XSLT выглядит следующим образом:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"
        name="xml"/>
    <xsl:template match="PurchaseOrder">
        <xsl:for-each select="PurchaseOrder/PurchaseOrderItemDetail">
            <xsl:result-document format="xml" href="PurchaseOrderItemDetail{position()}.xml" encoding="UTF-8" indent="yes">
                <PurchaseOrderItemDetail>
                    <StorefrontName>
                        <xsl:value-of select="PurchaseOrder/StorefrontName"/>
                    </StorefrontName>
                    <OrderdBy>
                        <xsl:value-of select="OrdByAddress/Name"/>
                    </OrderdBy>
                    <PurchaseOrderNumber>
                        <xsl:value-of select="PurchaseOrder/OrderId"/>
                    </PurchaseOrderNumber>
                    <LineNumber>
                        <xsl:value-of select="PurchaseOrder/PurchaseOrderItemDetail/LineNumber"/>
                    </LineNumber>
                    <PurchaseOrderDate>
                        <xsl:value-of select="PurchaseOrder/OrderDateUtc"/>
                    </PurchaseOrderDate>
                    <OrderQuantity>
                        <xsl:value-of select="PurchaseOrder/PurchaseOrderItemDetail/OrderQuantity"/>
                    </OrderQuantity>
                </PurchaseOrderItemDetail>
            </xsl:result-document>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

Все, что я получаю, когда пытаюсь преобразовать XML (используя XMLspear), этотэг для кодирования, за которым последовала ошибка, указывающая, что есть «Открыть неформатированную исходную панель с заданным содержимым» и LSException: преждевременный конец файла.

Я также пробовал «копировать» вместо «значение-из»"для фактического преобразования узлов, которые я хочу, но это не повлияло на мою проблему.

Я не уверен, что мне не хватает.

Ответы [ 2 ]

0 голосов
/ 12 июля 2019

Для LineNumber и OrderQuantity вы можете просто сделать <xsl:copy-of select="LineNumber"/> и т. Д.

Для StoreFrontName и PurchaseOrderNumber вы можете сделать <xsl:copy-of select="/*/PurchaseOrderNumber"/> и т. Д.

В большинстве случаев ваша ошибка заключается в том, что контекст неверен: вам нужно подумать о том, какой элемент является узлом контекста, и убедиться, что ваши пути начинаются с него.

В других случаях вы просто небрежно относились к именованиюнапример, заглавная буква StoreFrontName неверна, и нет элемента с именем OrderId.

0 голосов
/ 11 июля 2019

Большинство выражений XPath внутри for-each кажутся неправильными, каждое PurchaseOrderItemDetail является узлом контекста внутри for-each, поэтому для создания мелкой копии, охватывающей выбранные дочерние элементы, используйте, например, <xsl:copy><xsl:copy-of select="LineNumber, OrderQuantity"/></xsl:copy>, для копирования ссылок на элементыдети от родителя или предка используют путь, достигающий, например, <xsl:copy><xsl:copy-of select="ancestor::PurchaseOrder/StorefrontName, LineNumber, OrderQuantity"/></xsl:copy>.Конечно, вы также можете создавать новые элементы, но убедитесь, что вы используете правильный путь для выбора значений.

...