【问题标题】:xml to xml copy with replace of [field]xml 到 xml 复制并替换 [field]
【发布时间】:2012-07-27 16:00:09
【问题描述】:

嗨,伙计们,我对他的人有点麻烦我想做一个 xml 的完整副本并替换数据中的一个字段这是我一直在使用的代码,但它只转换第一个字段和不使用带有默认 xsl 转换的 eclipse 复制 xml im 来完成这项工作,因为它似乎是我无论如何都能正常工作的唯一处理器这里是 xsl 模板:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
    <xsl:variable name="replacedURL">
        <xsl:call-template name="string-replace-all">
            <xsl:with-param name="text" select="/hotels/hotel/hotel_link/text()"/>
            <xsl:with-param name="replace" select="'[[PARTNERID]]'"/>
            <xsl:with-param name="by" select="'13252'"/>
        </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="$replacedURL"/>
</xsl:template>
<xsl:template name="string-replace-all">
    <xsl:param name="text"/>
    <xsl:param name="replace"/>
    <xsl:param name="by"/>
    <xsl:choose>
        <xsl:when test="contains($text, $replace)">
            <xsl:value-of select="substring-before($text,$replace)"/>
            <xsl:value-of select="$by"/>
            <xsl:call-template name="string-replace-all">
                <xsl:with-param name="text" select="substring-after($text,$replace)"/>
                <xsl:with-param name="replace" select="$replace"/>
                <xsl:with-param name="by" select="$by"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
</xsl:stylesheet>

那么这是 xml 的示例

<hotel>
<hotel_ref>235128</hotel_ref>
<hotel_name>Afghanistan Dolores test - non bookable</hotel_name>
<hotel_star>2</hotel_star>
<hotel_address>afghanistan test</hotel_address>
<hotel_city>afghanistan</hotel_city>
<hotel_pcode /><hotel_county />
<hotel_country>Afghanistan</hotel_country>
<hotel_description>hotel description text</hotel_description>
 <alternate_description>hotel description text</alternate_description>
 <hotel_directions>kjlaklka</hotel_directions>
 <alternate_directions>kjlaklka</alternate_directions>
<hotel_link> http://www.website.com/en/p[[PARTNERID]]/hotel-reservations/235128_afghanistan-dolores-test-non-bookable-afghanistan.aspx</hotel_link>
<customerrating>0</customerrating>
<PricesFrom>10.0000</PricesFrom>
<MaxPrice>800.00</MaxPrice>
<CurrencyCode>GBP</CurrencyCode>
<images />
<geo_code><lat>34.53824</lat><long>69.18640</long></geo_code>
<hotel_facilities />
<checkin_time>12/07/2011 14:00:00</checkin_time>
<checkout_time>12/07/2011 11:00:00</checkout_time>
 <cancellationpolicy>2 days prior to stay</cancellationpolicy>
<cancellationtext /><accommodation_type /><hotel_appeals />
<hotel_star_accreditor />
<hotel_total_rooms>45</hotel_total_rooms>
<hotel_credit_cards>
<CreditCard><credit_card_id>4</credit_card_id><credit_card_name>American Express</credit_card_name></CreditCard><CreditCard><credit_card_id>5</credit_card_id><credit_card_name>Switch/Maestro</credit_card_name></CreditCard><CreditCard><credit_card_id>1</credit_card_id><credit_card_name>Visa/Delta</credit_card_name></CreditCard><CreditCard><credit_card_id>2</credit_card_id> <credit_card_name>Mastercard</credit_card_name></CreditCard></hotel_credit_cards>  
<hotel_city_taxes><CityTax><Type /><Value /><OptedIn /><IsCityTaxArea /></CityTax>
</hotel_city_taxes> 
</hotel>

整个文件大约 500 兆字节,我需要一个特殊的处理器来执行此操作,还是使用 xsl 转换内置的 eclipse 来完成这项工作

【问题讨论】:

  • AFAIK,oXygen 可以用作 Eclipse 插件。在这种情况下,您可以将 oXygen 与 Saxon 9.x 一起使用并运行我为您之前的问题提供的基于 XSLT 2.0 RegEx 的解决方案。

标签: xml xslt copy


【解决方案1】:

复制所有内容而只修改一小部分始终是身份模板的情况:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

  <!-- ...except for the text in <hotel_link> elements: -->
  <xsl:template match="hotel_link/text()">
    <xsl:call-template name="string-replace-all">
      <xsl:with-param name="text"    select="."/>
      <xsl:with-param name="replace" select="'[[PARTNERID]]'"/>
      <xsl:with-param name="by"      select="'13252'"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="string-replace-all">
    <xsl:param name="text"/>
    <xsl:param name="replace"/>
    <xsl:param name="by"/>
    <xsl:choose>
      <xsl:when test="contains($text, $replace)">
        <xsl:value-of select="substring-before($text,$replace)"/>
        <xsl:value-of select="$by"/>
        <xsl:call-template name="string-replace-all">
          <xsl:with-param name="text" select="substring-after($text,$replace)"/>
          <xsl:with-param name="replace" select="$replace"/>
          <xsl:with-param name="by" select="$by"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

string-replace-all 模板已经是taken from this answer。)

话虽如此,如果您的输入文件为 500 MB,则不应使用 XSLT 转换,因为这会在开始之前在内存中构建输入的完整 DOM。 DOM 总是比它所基于的 XML 文档大很多,所以在这里你可能会遇到内存问题。当然你也可以试试。

您应该改为编写一个小型 SAX 解析器。这将更快,内存效率更高。

【讨论】:

  • 这似乎工作我通过 eclipse xsl 转换运行它,使用 xalan 作为处理器我收到一个警告,在 60 次尝试后抱怨套接字连接失败我将 eclipse .ini 设置为 4GB 似乎在检查时这样做了文件小了 700KB,但我预计会丢失字符,所以我认为即使我有一个错误,它也能成功完成,谢谢你的帮助
猜你喜欢
  • 2022-01-26
  • 2011-12-18
  • 1970-01-01
  • 2014-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
相关资源
最近更新 更多