บันทึกข้อมูลจาก XSLT ที่สร้างจากไฟล์ XML

ฉันมีปัญหาขณะพยายามบันทึกข้อมูลบางอย่างลงใน XML โดยใช้ XSLT ปัญหาคือว่าทุกอย่างดูเรียบร้อยดี ไม่มีข้อยกเว้น ไฟล์บันทึกก็สะอาดเช่นกัน แต่ฉันไม่เห็นการเปลี่ยนแปลงในไฟล์ XML และฉันทำไม่ได้ นี่คือรหัสของฉันสำหรับบันทึกเอาต์พุตเป็นไฟล์

    Transformer transformer = XslTemplatesPool.getTransformer(SAVE_ITEM, realPath);
    setCategoryAndSubcateory(transformer, request);

    String name = request.getParameter(NAME);
    /*retrieving some more parameters*/
    String price = request.getParameter(PRICE);

    transformer.setParameter(NAME, name);
    /*...*/
    transformer.setParameter(XML_PATH, "E:/xslt/WebContent/xml/shop.xml");

    if (price == null) {
        price = "";
    }
    transformer.setParameter(PRICE, price);
    if (notInStock == null) {
        notInStock = "";
    }
    /*
     * out is an instance of PrintWriter
     * PrintWriter out = httpServletResponse.getWriter()
     */
    transformer.setParameter(NOT_IN_STOCK, notInStock);
    executeWrite(out, readWriteLock, transformer);


    protected void executeWrite(PrintWriter out, ReadWriteLock readWriteLock, Transformer transformer) throws HandledException {

    Source xmlSource = new StreamSource("E:/xslt/WebContent/xml/shop.xml");
    StreamResult result = new StreamResult(out);
    Lock writeLock = readWriteLock.writeLock();
    writeLock.lock();

    try {
        transformer.transform(xmlSource, result);
    } catch (TransformerException e) {
        ExceptionHandler.logAndThrow(e, logger);
    } finally {
        writeLock.unlock();
    }
}

จาก addItem ของฟอร์มที่สร้างโดย XSLT (ความสงบสุขของข้อมูลที่ฉันต้องการนั้นดีและราบรื่นในขั้นตอนนี้) ฉันดึงข้อมูลบางส่วนแล้วลองเพิ่มลงในไฟล์ xml โดยใช้เทมเพลต saveItem

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.example.org/products"
xmlns:redirect="http://xml.apache.org/xalan/redirect"
extension-element-prefixes="redirect"
xmlns:validation="xalan://com.xslt.util.Validator"
exclude-result-prefixes="validation redirect">

<xsl:import href="/thaddItem.xsl" />
<xsl:import href="/thproductsList.xsl" />

<xsl:param name="categoryName" />
<xsl:param name="subcategoryName" />
<xsl:param name="name" />
<xsl:param name="producer" />
<xsl:param name="model" />
<xsl:param name="date-of-issue" />
<xsl:param name="color" />
<xsl:param name="not-in-stock" />
<xsl:param name="price" />
<xsl:param name="xmlPath"/>
<xsl:param name="isValid" select="validation:validate($name, $producer, $model, $date-of-issue, $color, $price, $not-in-stock)" />
<xsl:param name="nameError" select="validation:getNameError()" />
<xsl:param name="producerError" select="validation:getProducerError()" />
<xsl:param name="modelError" select="validation:getModelError()" />
<xsl:param name="dateError" select="validation:getDateError()" />
<xsl:param name="priceError" select="validation:getPriceError()" />
<xsl:param name="colorError" select="validation:getColorError()" />

<xsl:template match="/" priority="2">
    <xsl:choose>
        <xsl:when test="not($isValid)">
            <xsl:call-template name="addItem">
                <xsl:with-param name="nameError" select="$nameError" />
                <xsl:with-param name="producerError" select="$producerError" />
                <xsl:with-param name="modelError" select="$modelError" />
                <xsl:with-param name="dateError" select="$dateError" />
                <xsl:with-param name="priceError" select="$priceError" />
                <xsl:with-param name="colorError" select="$colorError" />
                <xsl:with-param name="not-in-stock" select="$not-in-stock" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <redirect:write select="$xmlPath">
                <xsl:call-template name="saveItem" />
            </redirect:write>
            <xsl:call-template name="returnToProducts" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

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

<xsl:template match="/xs:shop/xs:category[@name=$categoryName]/xs:subcategory[@name=$subcategoryName]">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()" />
        <xsl:element name="xs:product">
            <xsl:attribute name="name">
                <xsl:value-of select="$name" />
            </xsl:attribute>
            <xsl:attribute name="producer">
                <xsl:value-of select="$producer" />
            </xsl:attribute>
            <xsl:attribute name="model">
                <xsl:value-of select="$model" />
            </xsl:attribute>
            <xsl:element name="xs:date-of-issue">
                <xsl:value-of select="$date-of-issue" />
            </xsl:element>
            <xsl:element name="xs:color">
                <xsl:value-of select="$color" />
            </xsl:element>
            <xsl:choose>
                <xsl:when test="$not-in-stock">
                    <xsl:element name="xs:not-in-stock" />
                </xsl:when>
                <xsl:otherwise>
                    <xsl:element name="xs:price">
                        <xsl:value-of select="$price" />
                    </xsl:element>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:element>
    </xsl:copy>
</xsl:template>

<xsl:template name="returnToProducts">
    <html>
        <head>
            <meta http-equiv="refresh" content="0;url=controller.do?command=productsList&amp;categoryName={$categoryName}&amp;subcategoryName={$subcategoryName}" />
        </head>
    </html>
</xsl:template>

</xsl:stylesheet>

ตัวอย่างไฟล์ XML ของฉัน

<xs:shop xmlns:xs="http://www.example.org/products" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/products shop.xsd">    
<xs:category name="bicycle">
    <xs:subcategory name="frame">
        <xs:product name="Soda FR" producer="NS" model="fr445">
            <xs:date-of-issue>10-05-2012</xs:date-of-issue>
            <xs:color>white</xs:color>
            <xs:price>520$</xs:price>
        </xs:product>
        <xs:product name="Nucleon" producer="Nicolai" model="nc428">
            <xs:date-of-issue>10-05-2012</xs:date-of-issue>
            <xs:color>dark grey</xs:color>
            <xs:not-in-stock/>
        </xs:product>
    </xs:subcategory>
</xs:category>
</xs:shop>

person edthehead    schedule 13.02.2013    source แหล่งที่มา
comment
นั่นเป็นเอกสาร XML ฉบับเต็มของคุณหรือไม่? ใช้คำนำหน้า xs: ซึ่งไม่ได้ประกาศไว้ที่ใดเลย   -  person JLRishe    schedule 13.02.2013
comment
ขออภัย ฉันทำหายไปขณะโพสต์ ตอนนี้ XML ได้รับการอัปเดตแล้ว และดูเหมือนกับที่ฉันใช้อยู่   -  person edthehead    schedule 13.02.2013


คำตอบ (1)


ดังนั้น... ในที่สุดฉันก็ทำได้ ยังคงมีข้อผิดพลาดเล็กน้อย

ไม่ทราบรหัสระบบ; บรรทัด #-1; คอลัมน์ #-1; การสิ้นสุดไฟล์ก่อนกำหนด

แต่ฉันหวังว่าจะแก้ไขได้ไม่ยากนัก

ดังที่คุณเห็นด้านล่าง ขณะนี้สตรีมเอาต์พุตถูกเขียนไปยัง StringWriter จากนั้น StringWriter จะถูกเขียนลงในไฟล์ XML ฉันยังไม่เข้าใจว่าทำไมถึงไม่มีผลลัพธ์ (ไฟล์เพิ่งถูกล้าง) หากคุณส่งไฟล์ไปที่ StreamResult อาจเป็นเพียงมือของฉันที่งอกออกมาจากที่ผิด พรุ่งนี้ฉันจะทำการวิจัยเล็กน้อย

ดังนั้นเมธอดExecutiveWrite(...)จึงเปลี่ยนไปมาก

    protected void executeWrite(PrintWriter out, ReadWriteLock readWriteLock, Transformer transformer)
        throws HandledException {

    Lock readLock = readWriteLock.readLock();
    StringWriter outWriter = new StringWriter();
    Transformer t = null;
    try {
        readLock.lock();
        StreamSource xmlStream = new StreamSource(/*path to XML*/);
        t = transformer;
        t.transform(xmlStream, new StreamResult(outWriter));
    } catch (TransformerException e) {
        ExceptionHandler.logAndThrow(e, logger);
    } finally {
        readLock.unlock();
    }

    Lock writeLock = readWriteLock.writeLock();
    FileWriter fileWriter = null;
    try {
        writeLock.lock();
        fileWriter = new FileWriter(new File(/*path to XML*/));
        fileWriter.write(outWriter.toString());
    } catch (IOException e) {
        ExceptionHandler.logAndThrow(e, logger);
    } finally {
        if (fileWriter != null) {
            try {
                fileWriter.close();
            } catch (IOException e) {
                ExceptionHandler.logAndThrow(e, logger);
            }
        }
        writeLock.unlock();
    }
}
person edthehead    schedule 14.02.2013
comment
ฉันอยากจะแนะนำสองสิ่งที่จะช่วยให้คุณก้าวต่อไป จนกว่าคุณจะได้ผลลัพธ์ที่ต้องการ ฉันจะเก็บโค้ดให้เรียบง่ายที่สุดเท่าที่จะทำได้ เมื่อคุณได้รับไฟล์แล้ว ปัญหาจะต้องอยู่ใน xsl มีเพียงบางส่วนเท่านั้นที่โพสต์ไว้ที่นี่ บางทีคุณสามารถลด xsl ของคุณเพื่อแยกส่วนที่ทำให้เอาต์พุตของคุณว่างเปล่าได้ - person Jan; 15.02.2013