سؤال

أحاول Dettctct / العمل حولها هذه علة في عناصر RSS. هذا يعني أنه يجب علي العثور على اسم مساحة اسم خاطئة وتغيير قيمته إلى مساحة الاسم الصحيحة. على سبيل المثال:

xmlns:media="http://search.yahoo.com/mrss" 

يجب أن يكون:

xmlns:media="http://search.yahoo.com/mrss/" 

كيف يمكنني التحول التي أعطيت org.w3c.document؟

Meanwile اكتشفت كيفية الحصول على جميع عناصر مساحة اسم معينة:

        XPathFactory xpf = XPathFactory.newInstance();
        XPath xpath = xpf.newXPath();
        XPathExpression expr = xpath.compile("//*[namespace-uri()='http://search.yahoo.com/mrss']");


        Object result = expr.evaluate(d, XPathConstants.NODESET);
        if (result != null) {
            NodeList nodes = (NodeList) result;
            for(int node=0;node<nodes.getLength();node++)
            {
                Node n = nodes.item(node);
                this.log.warn("Found old mediaRSS namespace declaration: "+n.getTextContent());
            }

        } 

الآن لا بد لي من معرفة كيفية تغيير مساحة اسم العقدة عبر Jaxp.

هل كانت مفيدة؟

المحلول 2

فقط من أجل الاكتمال:

رمز جافا:

Document d = out.outputW3CDom(converted);
            DOMSource oldDocument = new DOMSource(d);
            DOMResult newDocument = new DOMResult();
            TransformerFactory tf = TransformerFactory.newInstance();
            StreamSource xsltsource = new StreamSource(
                    getStream(MEDIA_RSS_TRANSFORM_XSL));
            Transformer transformer = tf.newTransformer(xsltsource);
            transformer.transform(oldDocument, newDocument);

private InputStream getStream(String fileName) {
    InputStream xslStream = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("/" + fileName);
    if (xslStream == null) {
        xslStream = Thread.currentThread().getContextClassLoader()      .getResourceAsStream(fileName);
        }
        return xslStream;
    }

STYLESHEET:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!--identity transform that will copy matched node/attribute to the output and apply templates for it's children and attached attributes-->
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="@*|*|text()" />
        </xsl:copy>
    </xsl:template>

    <!--Specialized template to match on elements with the incorrect namespace and generate a new element-->
    <xsl:template match="//*[namespace-uri()='http://search.yahoo.com/mrss']">
        <xsl:element name="{local-name()}" namespace="http://search.yahoo.com/mrss/" >
            <xsl:apply-templates select="@*|*|text()" />
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

شكر خاص من جرب هانسن له مساعدة مع XSLT.

نصائح أخرى

ربما يمكنك القيام بذلك مع XSLT، مع قاعدة مثل هذا:

<xsl:template match="media:*">
   <xsl:element name="local-name()" namespace="http://search.yahoo.com/mrss/">
      <xsl:apply-templates match="node()|@*"/>
   </xsl:element>
</xsl:template>

حيث تكون الوسائط ملزمة "http://search.yahoo.com/mrss.".

قد تضطر إلى تعديل بناء الجملة قليلا، حيث أكتب هذا دون مساعدة من مترجم. أيضا، ما ستحصل عليه ربما غير مهيأ للغاية (تصريحات مساحة الاسم على العديد من العناصر)، ولكن يجب أن يكون صحيحا محليا.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top