XSLT 转换 - 将节点添加到现有元素

问题描述 投票:0回答:1

我正在尝试使用 XSLT 将额外的节点添加到 XML 文件,但我在命名空间方面遇到了麻烦。

输入XML

<?xml version="1.0" encoding="utf-8"?>
<AuditFile xmlns="urn:StandardAuditFile-Taxation-Financial:DK" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:doc="urn:schemas-OECD:schema-extensions:documentation">
  <Header>
    <AuditFileVersion>1.0</AuditFileVersion>
    <AuditFileCountry>DK</AuditFileCountry>
    <AuditFileDateCreated>2024-06-19</AuditFileDateCreated>
    <Company>
      <RegistrationNumber />
      <Name>usdk</Name>
      <Address>
        <StreetName />
        <Number />
        <City />
        <PostalCode />
        <Country>DK</Country>
        <AddressType>PostalAddress</AddressType>
      </Address>
      <Contact>
        <ContactPerson>
          <FirstName>NotUsed</FirstName>
          <LastName />
        </ContactPerson>
      </Contact>
      <TaxRegistration>
        <TaxRegistrationNumber />
        <TaxType>VAT</TaxType>
        <Country>DN</Country>
      </TaxRegistration>
    </Company>
    <DefaultCurrencyCode>EUR</DefaultCurrencyCode>
    <SelectionCriteria>
      <SelectionStartDate>2024-06-19</SelectionStartDate>
      <SelectionEndDate>2024-06-19</SelectionEndDate>
    </SelectionCriteria>
    <TaxAccountingBasis>Fakturaregnskab</TaxAccountingBasis>
    <TaxEntity>Afdeling</TaxEntity>
  </Header>
</AuditFile>

所需的输出(将电话添加到联系人)

<AuditFile xmlns="urn:StandardAuditFile-Taxation-Financial:DK" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:doc="urn:schemas-OECD:schema-extensions:documentation">
  <Header>
    <AuditFileVersion>1.0</AuditFileVersion>
    <AuditFileCountry>DK</AuditFileCountry>
    <AuditFileDateCreated>2024-06-19</AuditFileDateCreated>
    <Company>
      <RegistrationNumber />
      <Name>usdk</Name>
      <Address>
        <StreetName />
        <Number />
        <City />
        <PostalCode />
        <Country>DK</Country>
        <AddressType>PostalAddress</AddressType>
      </Address>
      <Contact>
        <ContactPerson>
          <FirstName>NotUsed</FirstName>
          <LastName />
        </ContactPerson>
        <Telephone>123456789</Telephone>
      </Contact>
      <TaxRegistration>
        <TaxRegistrationNumber />
        <TaxType>VAT</TaxType>
        <Country>DN</Country>
      </TaxRegistration>
    </Company>
    <DefaultCurrencyCode>EUR</DefaultCurrencyCode>
    <SelectionCriteria>
      <SelectionStartDate>2024-06-19</SelectionStartDate>
      <SelectionEndDate>2024-06-19</SelectionEndDate>
    </SelectionCriteria>
    <TaxAccountingBasis>Fakturaregnskab</TaxAccountingBasis>
    <TaxEntity>Afdeling</TaxEntity>
  </Header>
</AuditFile>

经过一番努力,我想出了以下 XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:faia="urn:StandardAuditFile-Taxation-Financial:DK">
  <xsl:variable name="urnStandardAuditFile">urn:OECD:StandardAuditFile-Taxation/2.00</xsl:variable>
  
  <xsl:template match="*">
    <xsl:element name="{local-name()}" namespace="{$urnStandardAuditFile}">
    <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
  </xsl:template>
  
  <xsl:template match="@*">
    <xsl:attribute name="{local-name()}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>
  
  <xsl:template match="text() | comment() | processing-instruction()">
    <xsl:copy/>
  </xsl:template>
  
  <xsl:template match="faia:Contact">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
      <Telephone></Telephone>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

但这不断给我提供带有命名空间的节点,这是我不想要的。

<Contact xmlns="urn:StandardAuditFile-Taxation-Financial:DK">
    <ContactPerson xmlns="urn:OECD:StandardAuditFile-Taxation/2.00">
        <FirstName>NotUsed</FirstName>
        <LastName/>
    </ContactPerson>
    <Telephone xmlns="urn:OECD:StandardAuditFile-Taxation/2.00">123456789</Telephone>
    <Telephone xmlns=""/>
</Contact>

任何帮助将非常感激。

xml xslt
1个回答
0
投票

我认为你需要做的只是:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dk="urn:StandardAuditFile-Taxation-Financial:DK"
xmlns="urn:StandardAuditFile-Taxation-Financial:DK">
<xsl:output method="xml" indent="yes"/>

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

<xsl:template match="dk:Contact">
    <xsl:copy>
        <xsl:apply-templates/>
        <Telephone>123456789</Telephone>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>
© www.soinside.com 2019 - 2024. All rights reserved.