【问题标题】:How to remove xmlns="" from xslt output xml?如何从 xslt 输出 xml 中删除 xmlns=""?
【发布时间】:2019-10-09 11:14:21
【问题描述】:

我正在使用 xslt 从中生成 xml 文件。 问题是输出 xml 像这样添加了命名空间:xmlns=""。 我不想要它,因为 beanio.org 不适用于命名空间,我如何从输出 xml 中删除 xmlns=""? xslt

    <xsl:template match="/">
        <beanio xmlns="http://www.beanio.org/2012/03"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.beanio.org/2012/03 http://www.beanio.org/2012/03/mapping.xsd">

            <xsl:choose>
                <xsl:when test="/FeedFileConfig/@delimiter">
                    <xsl:element name="stream">
                        <xsl:attribute name="name">
                     <xsl:text>record</xsl:text>
                    </xsl:attribute>
                        <xsl:attribute name="format">
                     <xsl:text>csv</xsl:text>
                     </xsl:attribute>
                        <xsl:attribute name="ignoreUnidentifiedRecords">
                     <xsl:text>true</xsl:text>
                     </xsl:attribute>
                        <xsl:attribute name="xmlNamespace">
                     <xsl:text>http://www.beanio.org/2012/03</xsl:text>
                     </xsl:attribute>
                        <xsl:element name="parser">
                            <xsl:element name="property">
                                <xsl:attribute name="name">
                     <xsl:text>delimiter</xsl:text>
                    </xsl:attribute>
                                <xsl:attribute name="value">
                        <xsl:value-of select="/FeedFileConfig/@delimiter" />
                    </xsl:attribute>
                            </xsl:element>
                        </xsl:element>

                        <xsl:apply-templates />
                    </xsl:element>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:element name="stream">
                        <xsl:attribute name="name">
                         <xsl:text>record</xsl:text>
                    </xsl:attribute>
                        <xsl:attribute name="format">
                         <xsl:text>fixedlength</xsl:text>
                    </xsl:attribute>
                        <xsl:attribute name="ignoreUnidentifiedRecords">
                         <xsl:text>true</xsl:text>
                     </xsl:attribute>
                        <xsl:attribute name="xmlNamespace">
                     <xsl:text>http://www.beanio.org/2012/03</xsl:text>
                     </xsl:attribute>
                    </xsl:element>
                    <xsl:apply-templates />
                </xsl:otherwise>
            </xsl:choose>
        </beanio>
    </xsl:template>

    <xsl:template match="FeedFileConfig">
        <xsl:for-each select="/FeedFileConfig/Parsers/Parser">
            <xsl:element name="record">
                <xsl:attribute name="name">
                                  <xsl:value-of select="@name" />
                             </xsl:attribute>
                <xsl:attribute name="class">
                                  <xsl:value-of select="'map'" />
                             </xsl:attribute>
                <xsl:attribute name="xmlNamespace">
                     <xsl:text>http://www.beanio.org/2012/03</xsl:text>
                     </xsl:attribute>
                <xsl:for-each select="Fields/Field">
                    <xsl:variable name="varJsonPath">
                        <xsl:value-of select="replace(@jsonPath, '\$\.', '')" />
                    </xsl:variable>
                    <xsl:variable name="varIgnore">
                        <xsl:value-of select="@ignore" />
                    </xsl:variable>
                    <xsl:variable name="varLength">
                        <xsl:value-of select="@length" />
                    </xsl:variable>
                    <xsl:if test="@jsonPath != '$.recordType'">
                        <xsl:element name="field">
                            <xsl:attribute name="name">
                                                  <xsl:value-of select="$varJsonPath" />
                                             </xsl:attribute>
                            <xsl:if test="$varLength != ''">
                                <xsl:attribute name="length">
                                                  <xsl:value-of select="@length" />
                                             </xsl:attribute>
                            </xsl:if>
                            <xsl:if test="$varIgnore != ''">
                                <xsl:attribute name="ignore">
                                                      <xsl:value-of select="$varIgnore" />
                                                 </xsl:attribute>
                            </xsl:if>
                        </xsl:element>
                    </xsl:if>
                    <xsl:if test="@jsonPath = '$.recordType'">
                        <xsl:element name="field">
                            <xsl:attribute name="name">
                                                  <xsl:value-of select="$varJsonPath" />
                                             </xsl:attribute>
                            <xsl:attribute name="rid">
                                                  <xsl:value-of select="'true'" />
                                             </xsl:attribute>
                            <xsl:attribute name="regex">
                                                  <xsl:value-of
                                select="../../Filters/Filter/@regex" />
                                             </xsl:attribute>
                            <xsl:if test="$varLength != ''">
                                <xsl:attribute name="length">
                                                  <xsl:value-of select="@length" />
                                             </xsl:attribute>
                            </xsl:if>
                            <xsl:if test="$varIgnore != ''">
                                <xsl:attribute name="ignore">
                                            <xsl:value-of select="$varIgnore" />
                                        </xsl:attribute>
                            </xsl:if>
                        </xsl:element>
                    </xsl:if>
                </xsl:for-each>
            </xsl:element>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

我正在使用 xslt 从中生成 xml 文件。 问题是输出 xml 像这样添加了命名空间:xmlns=""。 我不想要它,因为 beanio.org 不适用于命名空间,如何从输出 xml 中删除 xmlns=""?

<?xml version="1.0" encoding="UTF-8"?>
<beanio xmlns="http://www.beanio.org/2012/03"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.beanio.org/2012/03 http://www.beanio.org/2012/03/mapping.xsd">
   <stream name="record" format="csv" ignoreUnidentifiedRecords="true"
           xmlNamespace="http://www.beanio.org/2012/03">
      <parser>
         <property name="delimiter" value=";"/>
      </parser>
      <record xmlns="" name="asbmasteranddeltafile" class="map"
              xmlNamespace="http://www.beanio.org/2012/03">
         <field name="isin"/>
         <field name="status"/>
         <field name="instrumentCategory"/>
         <field name="issueDescription"/>
         <field name="cfiCode"/>
         <field name="fisn"/>
         <field name="preliminaryTermsProspectus"/>
         <field name="nominalValue"/>
         <field name="issueCurrency"/>
         <field name="smallestDenomination"/>
         <field name="conversionRatioContractSize"/>
         <field name="maturityExpirationDate"/>
         <field name="exercisePrice"/>
         <field name="exercisePriceCurrency"/>
         <field name="underlying"/>
         <field name="typeOfInterest"/>
         <field name="interestRate"/>
         <field name="interestPaymentDate"/>
         <field name="interestFrequency"/>
         <field name="firstInterestPaymentDate"/>
         <field name="additionalInformation"/>
         <field name="marketIdentifierCodeMic"/>
         <field name="leadManagerName"/>
         <field name="leadManagerLei"/>
         <field name="fundManagerName"/>
         <field name="fundManagerLei"/>
         <field name="centralSecurityDepositoryCsdNameCentralSecurityDepositoryCsdLei"/>
         <field name="issuerNameLong"/>
         <field name="issuerLei"/>
         <field name="issuerNameShort"/>
         <field name="issuerSupranational"/>
         <field name="issuerHeadquartersAddress1"/>
         <field name="issuerHeadquartersAddress2"/>
         <field name="issuerHeadquartersStateProvince"/>
         <field name="issuerHeadquartersPostCode"/>
         <field name="issuerHeadquartersCity"/>
         <field name="issuerHeadquartersCountry"/>
         <field name="issuerLegalRegistrationAddress1"/>
         <field name="issuerLegalRegistrationAddress2"/>
         <field name="issuerLegalRegistrationStateProvince"/>
         <field name="issuerLegalRegistrationPostCode"/>
         <field name="issuerLegalRegistrationCity"/>
         <field name="issuerLegalRegistrationCountry"/>
         <field name="createDate"/>
         <field name="modifyDate"/>
      </record>
   </stream>
</beanio>

我正在使用 xslt 从中生成 xml 文件。 问题是输出 xml 像这样添加了命名空间:xmlns=""。 我不想要它,因为 beanio.org 不适用于命名空间,如何从输出 xml 中删除 xmlns=""?

【问题讨论】:

  • 请编辑您的问题,以便我们清楚地看到您的输入和预期输出。

标签: xml xslt


【解决方案1】:

如果您希望所有结果元素都位于某个命名空间中,那么通常您将相关的命名空间声明 xmlns="http://www.beanio.org/2012/03" 放在样式表 &lt;xsl:stylesheet xmlns="http://www.beanio.org/2012/03" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" ...&gt; 的根元素上,而不仅仅是像您目前所做的那样放在单个模板中,这样,它仅适用于单个模板内的元素。

【讨论】:

  • 谢谢你..你的建议对我有用。我添加了这样的样式表来解决它> w3.org/1999/XSL/Transform" xmlns="beanio.org/2012/03">
猜你喜欢
  • 2015-12-31
  • 2015-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多