2013-09-16 2 views
1

Мы пытаемся добавить еще два пространства имен/атрибутов в наш XML-файл. Структура - это внешнее определение (XSD), и мы хотели бы знать, было бы возможно, если бы мы могли добавить два новых пространства имен/имен через XSLT, или это должно быть включено во внешнюю дефиницию? (Конечно, обновление внешней дефиниции является самым простым выходом из положения.)Добавление атрибутов пространства имен в XML

Я уже смотрел на несколько вопросов здесь, как:
adding attribute to the node
Adding namespace to child elements using xslt
XSLT transforming is throwing error

Но я все еще невежественный как это сделать. Я девственник в отношении XSLT - никакого опыта вообще. Хотелось бы узнать, возможно ли это через XSLT.

As-это

<ns2:ProcessCommunication xmlns:ns2="http://URL"> 
    <ns2:communication> 
     <ns2:CommunicationTemplateAbbreviation>INV</ns2:CommunicationTemplateAbbreviation> 
     <ns2:CommunicationValues> 
     <ns2:CommunicationValue> 
      <ns2:FinancialValue>205029</ns2:FinancialValue> 
      <ns2:Title>Net</ns2:Title> 
     </ns2:CommunicationValue> 
     </ns2:CommunicationValues> 
     <ns2:CustomFields> 
     <ns2:CustomField> 
      <ns2:Name>SomeValue</ns2:Name> 
      <ns2:Answer> 
       <ns2:Value>1</ns2:Value> 
      </ns2:Answer> 
     </ns2:CustomField> 
     <ns2:CustomField> 
      <ns2:Name>Transaction Currency</ns2:Name> 
      <ns2:Answer> 
       <ns2:Value>EUR</ns2:Value> 
      </ns2:Answer> 
     </ns2:CustomField> 
     </ns2:CustomFields> 
    </ns2:communication> 
</ns2:ProcessCommunication> 

To-быть:

<ns2:ProcessCommunication xmlns:ns2="http://URL"> 
    <ns2:communication> 
     <ns2:CommunicationTemplateAbbreviation>INV</ns2:CommunicationTemplateAbbreviation> 
     <ns2:CommunicationValues> 
     <ns2:CommunicationValue> 
      <ns2:FinancialValue>205029</ns2:FinancialValue> 
      <ns2:Title>Net</ns2:Title> 
     </ns2:CommunicationValue> 
     </ns2:CommunicationValues> 
     <ns2:CustomFields> 
     <ns2:CustomField> 
      <ns2:Name>SomeValue</ns2:Name> 
      <ns2:Answer> 
       <ns2:Value i:type="a:string" xmlns:a="http://www.w3.org/2001/XMLSchema">1</ns2:Value> 
      </ns2:Answer> 
     </ns2:CustomField> 
     <ns2:CustomField> 
      <ns2:Name>Transaction Currency</ns2:Name> 
      <ns2:Answer> 
       <ns2:Value i:type="a:string" xmlns:a="http://www.w3.org/2001/XMLSchema">EUR</ns2:Value> 
      </ns2:Answer> 
     </ns2:CustomField> 
     </ns2:CustomFields> 
    </ns2:communication> 
</ns2:ProcessCommunication> 

Там в дополнительный

я: тип = "а: строка" XMLNS: а = "HTTP: // www.w3.org/2001/XMLSchema "

в узле Значение.

Я могу идти только здесь, что довольно бесполезно.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:i="http://www.w3.org/2001/XMLSchema-instance" > 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 

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

<xsl:template match="node()"> 
<xsl:copy> 
     <xsl:attribute name="i:type">a:string</xsl:attribute> 
     <xsl:apply-templates select="node()|@*"/> 
</xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 

Я стараюсь использовать <xsl:template match="Answer/Value">, и это не сработает.

ответ

1

Вы просто пропустили декларацию пространства имен на самом XSLT. Это должно работать:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://URL"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 

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

<xsl:template match="ns2:Answer/ns2:Value"> 
<xsl:copy> 
     <xsl:attribute name="i:type">a:string</xsl:attribute> 
     <xsl:attribute name="xmlns:a">http://www.w3.org/2001/XMLSchema</xsl:attribute> 
     <xsl:apply-templates select="node()|@*"/> 
</xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 
+0

Спасибо! Но требование было: я: тип = "A: строка" XMLNS: а = "http://www.w3.org/2001/XMLSchema" и выход стал: Xmlns: я = "HTTP : //www.w3.org/2001/XMLSchema-instance "i: type =" a: string " Так должно быть," xmlns: a ". Это даже логично? Или что-то не так с их конца? – Arvin

+0

@Arvin Я добавил атрибут 'xmlns: a', и теперь он должен дать правильный результат. –

Смежные вопросы