0
我需要在<DefaultCurrency>
之後添加或插入另一個元素和屬性<vat:IRmark Type="generic"/>
。而且,還有一個組是我需要添加前綴的<Group>
元素。我幾乎做到了,但元素的插入不起作用。這裏是我的示例測試文件:插入元素並使用xslt添加前綴
輸入文件:
<Data>
<Record>
<ID>123-AAA</ID>
<Date>2017-04-23</Date>
<Group>
<Hdr>
<ID>833-AAA</ID>
<DefaultCurrency>GBP</DefaultCurrency>
<Sender>truth</Sender>
</Hdr>
</Group>
</Record>
生成的輸出:
<Data>
<Record xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:vat="http://www.govtalk.gov.uk/taxation/vat/vatdeclaration/2" schemaLocation="http://www.govtalk.gov.uk/CM/envelope http://www.govtalk.gov.uk/taxation/vat/vatdeclaration/2">
<ID>123-AAA</ID>
<Date>2017-04-23</Date>
<vat:Group>
<vat:Hdr>
<vat:ID>833-AAA</vat:ID>
<vat:DefaultCurrency>GBP</vat:DefaultCurrency>
<vat:Sender>truth</vat:Sender>
</vat:Hdr>
</vat:Group>
</Record>
預期輸出:
<Data>
<Record xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:vat="http://www.govtalk.gov.uk/taxation/vat/vatdeclaration/2" schemaLocation="http://www.govtalk.gov.uk/CM/envelope http://www.govtalk.gov.uk/taxation/vat/vatdeclaration/2">
<ID>123-AAA</ID>
<Date>2017-04-23</Date>
<vat:Group>
<vat:Hdr>
<vat:ID>833-AAA</vat:ID>
<vat:DefaultCurrency>GBP</vat:DefaultCurrency>
<vat:IRmark Type="generic"/>
<vat:Sender>truth</vat:Sender>
</vat:Hdr>
</vat:Group>
</Record>
XSLT代碼:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:vat="http://www.govtalk.gov.uk/taxation/vat/vatdeclaration/2">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()[boolean(normalize-space())]|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="DefaultCurrency">
<xsl:copy-of select="."/>
<vat:IRmark>
<xsl:attribute name="Type">generic</xsl:attribute>
</vat:IRmark>
</xsl:template>
<xsl:template match="*[ancestor-or-self::Group]">
<xsl:element name="vat:{local-name()}">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="Record">
<Record xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:vat="http://www.govtalk.gov.uk/taxation/vat/vatdeclaration/2" schemaLocation="http://www.govtalk.gov.uk/CM/envelope http://www.govtalk.gov.uk/taxation/vat/vatdeclaration/2">
<xsl:apply-templates/>
</Record>
</xsl:template>
謝謝。
http://lenzconsulting.com/how-分配給您的模板匹配
DefaultCurrency
您可以在解決衝突的模板讀了xslt-works /#priority還有一個列出各種可能性的有用表格。 –