2012-10-20 24 views
8

Vấn đề của tôi là cách thêm không gian tên và tiền tố cho tất cả các phần tử và thuộc tính bằng cách sử dụng XSLT? xml của tôi đầu vào như là ....Làm cách nào để thêm không gian tên và tiền tố cho tất cả các phần tử và thuộc tính bằng cách sử dụng XSLT?

<ProcessCreditMemo xmlns='CreditMemo' 
        xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
        xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' 
        xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> 
<ORDER_HEADERDetails> 
    <ORDER_HEADER> 
    <NAME>0010185214</NAME> 

là ...

<ns0:ProcessCreditMemo xmlns='CreditMemo' 
         xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
         xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' 
         xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' 
         xmlns:ns0="http://tempuri.org/"> 
<ns0:ORDER_HEADERDetails> 
    <ns0:ORDER_HEADER> 
    <ns0:NAME>0010185214</NAME> 

tôi cần thêm tiền tố "ns0:" cho tất cả các yếu tố và các thuộc tính, và thêm namespace "xmlns :. ns0 = "http://tempuri.org/" trong tiêu đề "ProcessCreditMemo"

tôi cố gắng để xây dựng một XSLT để làm điều đó ...

<xsl:stylesheet version="1.0" 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<xsl:output method="xml" encoding="UTF-8" indent="yes"/> 
<xsl:template match="node()|text()|@*"> 
    <xsl:copy> 
     <xsl:if test="local-name()='ProcessCreditMemo'"> 
      <xsl:attribute name="xmlns" namespace="http://tempuri.org/" /> 
     </xsl:if> 

nhưng kết quả XML sao chép tiền tố có giá trị rỗng.

<ProcessCreditMemo xmlns="CreditMemo" 
        xmlns:ns0="http://tempuri.org/" 
        xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
        xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        ns0:xmlns=""> 

Trả lời

13

này chuyển đổi:

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

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

<xsl:template match="*"> 
    <xsl:element name="ns0:{name()}" namespace="http://tempuri.org/"> 
    <xsl:copy-of select="namespace::*"/> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:element> 
</xsl:template> 
</xsl:stylesheet> 

khi bôi lên (điều chỉnh) được cung cấp đầu vào (bị thay đổi nghiêm trọng, XML không đầy đủ):

<ProcessCreditMemo xmlns='CreditMemo' 
    xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' 
    xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> 
    <ORDER_HEADERDetails> 
    <ORDER_HEADER> 
     <NAME>0010185214</NAME> 
    </ORDER_HEADER> 
    </ORDER_HEADERDetails> 
</ProcessCreditMemo> 

tạo ra truy nã, kết quả chính xác (không phải kết quả mong muốn không đúng định dạng/không đầy đủ được cung cấp):

<ns0:ProcessCreditMemo xmlns:ns0="http://tempuri.org/" xmlns="CreditMemo" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <ns0:ORDER_HEADERDetails> 
     <ns0:ORDER_HEADER> 
     <ns0:NAME>0010185214</ns0:NAME> 
     </ns0:ORDER_HEADER> 
    </ns0:ORDER_HEADERDetails> 
</ns0:ProcessCreditMemo> 
+0

Tôi đang sử dụng một đoạn mã tương tự trong XSLT của tôi, nhưng thực của tôi giữ hiển thị thông báo lỗi xpath là không hợp lệ cho .... .. Tuy nhiên, việc chuyển đổi xml hoạt động hoàn hảo. –

Các vấn đề liên quan