2008-09-27 14 views
10

मेरे पास गैर-नामित तत्वों वाला एक XML दस्तावेज़ है, और मैं उन्हें नामस्थान जोड़ने के लिए XSLT का उपयोग करना चाहता हूं। अधिकांश तत्व नेमस्पेस ए में होंगे; कुछ नामस्थान बी में होंगे। मैं यह कैसे कर सकता हूं?तत्वों के लिए नामस्थान जोड़ें

उत्तर

13
foo.xml

<foo x="1"> 
    <bar y="2"> 
     <baz z="3"/> 
    </bar> 
    <a-special-element n="8"/> 
</foo> 

और foo.xsl

<xsl:template match="*"> 
     <xsl:element name="{local-name()}" namespace="A" > 
      <xsl:copy-of select="attribute::*"/> 
      <xsl:apply-templates /> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="a-special-element"> 
     <B:a-special-element xmlns:B="B"> 
      <xsl:apply-templates match="children()"/> 
     </B:a-special-element> 
    </xsl:template> 

</xsl:transform> 

साथ

मैं

<foo xmlns="A" x="1"> 
    <bar y="2"> 
     <baz z="3"/> 
    </bar> 
    <B:a-special-element xmlns:B="B"/> 
</foo> 

कि क्या आप के लिए देख रहे हैं?

+0

यूप; मैंने आपकी पोस्ट से पहले एक जवाब गुगल किया, और यह अनिवार्य रूप से वही था। एक अंतर यह है कि मैं का उपयोग कर रहा हूं, लेकिन मेरा मानना ​​है कि वे कार्यात्मक रूप से समान हैं। –

0

यहाँ मैं अब तक है:

<xsl:template match="*"> 
    <xsl:element name="{local-name()}" namespace="A" > 
     <xsl:apply-templates /> 
    </xsl:element> 
</xsl:template> 

<xsl:template match="a-special-element"> 
    <B:a-special-element xmlns:B="B"> 
     <xsl:apply-templates /> 
    </B:a-special-element> 
</xsl:template> 

यह लगभग काम करता है; समस्या यह है कि यह गुणों की प्रतिलिपि नहीं बना रहा है। मैंने इस प्रकार से पढ़ा है, xsl: तत्व के तत्व के सभी गुणों की प्रतिलिपि बनाने का कोई तरीका नहीं है (उपयोग-विशेषता-सेट इसे काटने के लिए प्रकट नहीं होता है)।

+1

आपने सही दस्तावेज नहीं पढ़ा है। बल का प्रयोग करें, कल्पना पढ़ें, यह बहुत अच्छी तरह से लिखित और सुलभ है। – ddaa

2

आपको इस नुस्खा के लिए दो मुख्य अवयवों की आवश्यकता होगी।

सॉस स्टॉक identity transform होगा, और मुख्य स्वाद namespace विशेषता xsl:element पर दिया जाएगा।

निम्नलिखित, अवांछित कोड, सभी तत्वों के लिए http://example.com/ नामस्थान जोड़ना चाहिए।

<xsl:template match="*"> 
    <xsl:element name="xmpl:{local-name()}" namespace="http://example.com/"> 
    <xsl:apply-templates select="@*|node()"/> 
    </xsl:element> 
</xsl:template> 

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

व्यक्तिगत संदेश: हैलो, जेनी टेनीसन। मुझे पता है कि आप इसे पढ़ रहे हैं।

संबंधित मुद्दे