XSL:无法用换行符替换字符



我有一个要求,我必须读取一些数据,然后重新格式化,使数据的每一部分都在新行中。例如,我拥有的数据是:

dataLine1*dataLine2*dataLine3*dataLine4*dataLine5


我期望的结果是:

dataLine1
*dataLine2
*dataLine3
*.dataLine4
**dataLine5

因此,基本上,它可以归结为将*替换为"newlinecharater*"。然而,我仍然处于XSL的初学者水平,不确定如何实现这一点。

我的代码是:

<!-- START: Code to replace the data within a string since I am working on XSLT 0.1 -->
   <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>
<!-- END: Code to replace the data within a string since I am working on XSLT 0.1 -->  
<xsl:variable name="CPC" select="./_line_item_comment"/>
<xsl:variable name="newline"><xsl:text>&#10;</xsl:text></xsl:variable>
  <xsl:variable name="myVar">
    <xsl:call-template name="string-replace-all">
      <xsl:with-param name="text" select="substring-after($CPC,'!$!')" />
      <xsl:with-param name="replace" select="'*'" />
      <xsl:with-param name="by" select="$newline" /> <!-- Here I have to give the newline character -->
    </xsl:call-template>
  </xsl:variable>
  <xsl:value-of select="$myVar"/>

提前感谢!

最佳,
Shikhar


输入XML:

<?xml version="1.0" encoding="UTF-8"?>
<transaction>
    <data_xml currency="USD" language="English">
        <document buyer_company_id="4118171" buyer_user_id="48522367" data_type="3" document_id="4180721" document_number="3" document_var_name="line_process" supplier_company_id="4118171">
            <config label="Configurable" repeatable="true">
                <_config_attr_info />
            </config>
            <model label="Model" repeatable="true">
                <_model_id label="Model ID" />
                <_model_segment_id label="Model Segment ID" />
                <_model_product_line_id label="Model Product Line ID" />
                <_model_supplier_company_id label="Model Supplier Company ID" />
                <_model_name label="Model Name" />
                <_model_segment_name label="Model Segment Name" />
                <_model_product_line_name label="Model Product Line Name" />
                <_model_product_line_attributes />
                <_model_base_price display_precision="2" label="Model Base Price" />
                <_model_attributes />
                <_model_supplier_company_name label="Model Supplier Company Name" />
                <_model_bom label="Model BOM" />
                <_model_variable_name label="Model Variable Name" />
                <_model_product_line_var_name label="Model Product Line Variable Name" />
                <_model_segment_var_name label="Model Segment Variable Name" />
            </model>
            <part label="Part" repeatable="true">
                <_partner_entity_id label="Partner Entity Id" />
                <_part_units label="Part Units" />
                <_part_id label="Part ID">73454089</_part_id>
                <_part_supplier_company_id label="Part Supplier Company ID">4118171</_part_supplier_company_id>
                <_part_number label="Part Number">SS-29794-29</_part_number>
                <_part_desc label="Part Description">TUBULAR-INTERMEDIATE JT,LS,20, 20.000 X 0.625 W, LS BOX X 20.000 X 0.625 LS PIN,API 5L X56 SAW, 36 FT NOM</_part_desc>
                <_part_base_price display_precision="2" label="Part Base Price">0</_part_base_price>
                <_part_lead_time label="Part Lead Time">N/A</_part_lead_time>
                <_part_supplier_company_name label="Part Supplier Company Name">GEOG Energy Installation Field Services</_part_supplier_company_name>
                <_part_custom_field1 label="Family" />
                <_part_custom_field2 label="Cost" />
                <_part_custom_field3 label="Max Discount" />
                <_part_custom_field4 label="Maintenance %" />
                <_part_custom_field5 label="Part Custom Field 5" />
                <_part_custom_field6 label="Part Custom Field6" />
                <_part_custom_field7 label="Part Custom Field7" />
                <_part_custom_field8 label="Part Custom Field8" />
                <_part_custom_field9 label="Part Custom Field9">Offshore</_part_custom_field9>
                <_part_custom_field10 label="Part Custom Field10">SC&amp;P</_part_custom_field10>
                <_part_custom_field11 label="Part Custom Field11" />
                <_part_custom_field12 label="Part Custom Field12" />
                <_part_custom_field13 label="Part Custom Field13" />
                <_part_custom_field14 label="Part Custom Field14" />
                <_part_custom_field15 label="Part Custom Field15" />
                <_part_drawing_position label="Drawing Position" />
                <_part_custom_field16 label="Part Custom Field 16" />
                <_part_custom_field17 label="Part Custom Field17" />
                <_part_direct_buy label="Direct Buy">1</_part_direct_buy>
                <_part_extended_desc_1 label="Part Extended Description 1" />
                <_part_extended_desc_2 label="Part Extended Description 2" />
                <_part_custom_field18 label="Part Custom Field18" />
                <_part_custom_field19 label="Part Custom Field19" />
                <_part_custom_field20 label="Part Custom Field20" />
                <_part_custom_field21 label="Part Custom Field21">29794-29</_part_custom_field21>
                <_part_custom_field22 label="Part Custom Field22" />
                <_part_custom_field23 label="Part Custom Field23" />
                <_part_custom_field24 label="Part Custom Field24" />
                <_part_custom_field25 label="Part Custom Field25" />
                <_part_custom_field26 label="Part Custom Field26" />
                <_part_custom_field27 label="Part Custom Field27" />
                <_part_custom_field28 label="Part Custom Field28" />
                <_part_custom_field29 label="Part Custom Field29" />
                <_part_custom_field30 label="Part Custom Field30" />
            </part>
            <price label="Price" repeatable="true">
                <_price_quantity label="Quantity">1</_price_quantity>
                <_price_override_quantity label="Override Quantity">false</_price_override_quantity>
                <_price_list_price_each display_precision="2" label="Guideline Price">0</_price_list_price_each>
                <_price_override_price display_value="No" label="Override Price">No</_price_override_price>
                <_price_subtotal display_precision="2" label="Subtotal">0</_price_subtotal>
                <_price_discount_percent label="Discount %">0.00</_price_discount_percent>
                <_price_total_discount display_precision="2" label="Total Discount">0</_price_total_discount>
                <_price_net_price display_precision="2" label="Net Price">0</_price_net_price>
                <_price_override_discount display_value="None" label="Override Discount">None</_price_override_discount>
                <_price_discount_comment label="Discount Comment" />
                <_price_configurable_price display_precision="2" label="Configurable Price">0</_price_configurable_price>
                <_price_component_prices />
            </price>
            <_document_number label="Document Number">3</_document_number>
            <_parent_doc_number label="Parent Document Number">2</_parent_doc_number>
            <_parent_line_item label="Parent Line Item">SC&amp;P</_parent_line_item>
            <sfProductId_line label="sfProduct Id" />
            <_sequence_number label="Line #">2</_sequence_number>
            <costEa_line display_precision="2" label="Cost Ea">0</costEa_line>
            <extendedCost_line display_precision="2" label="Extended Cost">0</extendedCost_line>
            <extendedList_line display_precision="2" label="Extended List">0</extendedList_line>
            <discount_line label="Discount">0.0</discount_line>
            <discountType_line display_value="%" label="Discount Type">%</discountType_line>
            <lockStandardDiscount_line display_value="True" label="Lock Standard Discount">true</lockStandardDiscount_line>
            <maxDiscountPercent_line label="Max Discount %">0.0</maxDiscountPercent_line>
            <discount2_line label="Discount2 (Display)" />
            <extendedDiscount_line display_precision="2" label="Extended Discount">0</extendedDiscount_line>
            <netPriceEach_line display_precision="2" label="Net Price Each">0</netPriceEach_line>
            <netMarginAmt_line display_precision="2" label="Net Margin">0</netMarginAmt_line>
            <netMarginAmtEach_line display_precision="2" label="Net Margin Each" />
            <netMarginPercent_line label="Net Margin %">0.0</netMarginPercent_line>
            <optional_line display_value="No" label="Optional">false</optional_line>
            <_price_book_var_name label="Price Book Variable Name">_default_price_book</_price_book_var_name>
            <_is_line_item_mandatory label="Is Line Item Mandatory">true</_is_line_item_mandatory>
            <extendedNetPrice_line display_precision="2" label="Extended Net Price">0</extendedNetPrice_line>
            <listPrice_line display_precision="2" label="List Price">0</listPrice_line>
            <customGroup_line display_value="1" label="Custom Group">1</customGroup_line>
            <approval_line display_value="No" label="Approval">false</approval_line>
            <discountPercent_line label="Discount %">0.0</discountPercent_line>
            <_original_document_number label="Original Document Number">-1</_original_document_number>
            <discountAmt_line display_precision="2" label="Discount $">0</discountAmt_line>
            <family_line label="Family" />
            <_reconfiguration_date label="Reconfiguration Date">1970-01-01 00:00:00</_reconfiguration_date>
            <_last_modified_date label="Last Modified Date">2015-12-20 04:07:33</_last_modified_date>
            <maintenancePercent_line label="Maintenance %">0.0</maintenancePercent_line>
            <_is_recommended_price label="Is Price Recommended">true</_is_recommended_price>
            <maintenanceSubtotal_line display_precision="2" label="Maintenance Subtotal">0</maintenanceSubtotal_line>
            <outputGroupName_line label="Output Group Name" />
            <outputGroupDescription_line label="Output Group Description" />
            <outputGroupSubtotal_line label="Output Group Subtotal" />
            <outputGroupNumber_line label="Output Group Number" />
            <outputListPrice_line display_precision="2" label="Output List Price" />
            <outputNetPriceEach_line display_precision="2" label="Output Net Price Each" />
            <outputExtendedNetPrice_line display_precision="2" label="Output Extended Net Price" />
            <outputGroupDiscount_line display_precision="2" label="outputGroupDiscount_line" />
            <outputSequenceNumber_line label="Output Sequence Number" />
            <tech1 label="tech1" />
            <currency label="Currency" />
            <model_line label="Model" />
            <_line_item_comment label="Line Item Comments">bb!$!20" INTERMEDIATE JOINT
* 20" x 0.625 "API 5L GR X-56 SAW pipe
* 40+/- 2FT  in length/joint
* LS (70 KSI), box up x pin down
* 100% Volumetric or radiographic inspection of connector girth weld to API 1104
* Connectors supplied with protectors</_line_item_comment>
            <_line_item_spare_rule_var_name label="Spare Rule recommended the Line Item">0</_line_item_spare_rule_var_name>
            <totalGenricCost_line display_precision="2" label="TotalGenricCost" />
            <fServFirmPriceMSADisc_line label="Special Pricing Agreement Discount" />
            <fServFirmPriceLTACustNum_line label="SPA Customer #" />
            <mSADiscountRepairs_line label="Special Pricing Agreement Discount">0.0</mSADiscountRepairs_line>
            <lTACustomerRepairs_line label="SPA Customer #" />
            <freightCostRepairs_line display_precision="2" label="Freight Cost" />
            <eS104MarkupRepairs_line label="Freight Markup (%)">20.0</eS104MarkupRepairs_line>
            <freightPriceRepairs_line display_precision="2" label="Freight Price" />
            <mSADiscountRepGen_line label="Special Pricing Agreement Discount">0.0</mSADiscountRepGen_line>
            <lTACustomerRepGen_line label="SPA Customer #" />
            <freightCostRepGen_line display_precision="2" label="Freight Cost" />
            <eS104MarkupRepGen_line label="Freight Markup (%)">20.0</eS104MarkupRepGen_line>
            <unitListInSelectedCurrencyMCS_line display_precision="2" label="Unit List in Selected Currency" />
            <mSADiscountMCS_line label="Special Pricing Agreement Discount" />
            <lTACustomerMCS_line label="MSA Customer #" />
            <dOALevel_line label="DOA Level">0</dOALevel_line>
            <currency_line display_value="USD" label="Currency">USD</currency_line>
            <deviationReason_line label="Deviation Reason" />
            <standardTasksTextArea_line label="Standard Tasks" />
            <editStandardTasksTxtArea_line label="Edit Standard Tasks" />
            <editStandardTasksReason display_value="Published Standard" label="Reason">Published Standard</editStandardTasksReason>
            <costFSFirmPrice_line display_precision="2" label="Cost Adjustment" />
            <options_line display_value="False" label="Display Only">false</options_line>
            <costGrid_line display_precision="2" label="Cost">0</costGrid_line>
            <cMGrid_line label="CM%" />
            <editStandardTasksRichText_line label="Edit Standard Tasks" />
            <standardTasksRTF_line label="Standard Tasks" />
            <editStandardTasksCheck_line display_value="False" label="Edit Standard Tasks">false</editStandardTasksCheck_line>
            <cycleTime_line label="Cycle Time" />
            <eS104Markup label="Freight Markup" />
            <materialPricingGroup label="Material Pricing Group">EM</materialPricingGroup>
            <mORAtQuotation label="MOR at Quotation" />
            <sizeOfOrder label="Size of Order" />
            <targetCM label="Target CM%" />
            <cost2GT_line display_precision="2" label="Cost2" />
            <cost3GT_line display_precision="2" label="Cost3" />
            <cost1GT_line display_precision="2" label="Cost1" />
            <cost4GT_line display_precision="2" label="Cost4" />
            <cost5GT_line display_precision="2" label="Cost5" />
            <cost6GT_line display_precision="2" label="Cost6" />
            <cost7GT_line display_precision="2" label="Cost7" />
            <cost8GT_line display_precision="2" label="Cost8" />
            <cost9GT_line display_precision="2" label="Cost9" />
            <cost10GT_line display_precision="2" label="Cost10" />
            <deliveryTermsLine_line label="Delivery Terms" />
            <guidelinePriceMCSModel_line display_precision="2" label="Guideline Price" />
            <sellingPriceMCSModel_line display_precision="2" label="Selling Price" />
            <freightPriceMCSModel_line display_precision="2" label="Freight Price" />
            <workScopeForRepairs_line label="WorkScope" />
            <workScopeFlagRepairs_line display_value="False" label="WorkScope Flag">false</workScopeFlagRepairs_line>
            <wORKPACKAGEName_line label="WORKPACKAGE" />
            <opportunityCurrencyExchangeRate_line label="Opportunity Currency Exchange Rate">1.0</opportunityCurrencyExchangeRate_line>
            <modelNameCoreModel_line label="Model Name" />
            <currencyCoreModel_line label="Currency" />
            <mORCoreModel_line label="MOR for Model" />
            <guidelinePriceCoreMOdel_line display_precision="2" label="Guideline Price" />
            <sellingPriceCoreModel_line display_precision="2" label="Selling Price" />
            <proposalType_line label="Proposal Type">Budgetary</proposalType_line>
            <descriptionMCSNew_line label="Description" />
            <tILInfo_line label="TIL Info" />
            <genericScopeCost_line display_precision="2" label="Generic Scope Cost">0</genericScopeCost_line>
            <tILInfo2_line label="TIL Info" />
            <productInfo_Bently_Line label="ProductInfo_Bently_Line" />
            <modelOrLineDiscount display_value="Line" label="Model/Line">false</modelOrLineDiscount>
            <guidelinePrice_line display_precision="2" label="Guideline Price">0</guidelinePrice_line>
            <sellingPrice_line display_precision="2" label="Selling Price" />
            <parentModel_line label="Parent Model" />
            <parentProductLine_line label="Parent Product Line" />
            <unitPrice_line display_precision="2" label="Unit Price">0</unitPrice_line>
            <productTotal display_precision="2" label="product total" />
            <pGSGASRepairsWorkScopeTasksListRTE label="pGSGASRepairsWorkScopeTasksListRTE" />
            <pGSCMPartDesciptionSummup_forDocEng label="pGSCMPartDesciptionSummup_forDocEng" />
            <syncid_line label="Synchronization Id" />
            <externalId_line label="ExternalId">3-91845613</externalId_line>
            <productGpTotal display_precision="2" label="ProductGpTotal" />
            <opptyDriver_SS_line label="Oppty Driver" />
            <productService_SS_line label="Product Service" />
            <partNumber_line label="Part Number" />
            <extendedCompareDiscount_line label="extendedCompareDiscount_line">0.0</extendedCompareDiscount_line>
            <site_ds_line label="site_ds_line" />
            <warehouse_ds_line label="warehouse_ds_line" />
            <originalsite_ds_line label="OriginalSite" />
            <sequenceNum_ds_line label="SequenceNum" />
            <productCMPercent_line display_precision="2" label="Product CM%">0</productCMPercent_line>
            <cost_line display_precision="2" label="cost" />
            <dispenserBrandImage_line label="NOT IN USE" />
            <dispenserImageSubBrand_line label="NOT IN USE" />
            <quantity_SS_line display_precision="0" label="quantity_SS_line">1</quantity_SS_line>
            <sequenceNumber_bm_line label="Sequence Number" />
            <technicalDescription_DRL_line label="Technical Description" />
            <reconfigureFlag_line label="reconfigureFlag_line">false</reconfigureFlag_line>
            <controlsCostPriceReport_line label="Controls Cost Price Report" />
            <LineItemUOM_Line label="Line Item UOM" />
            <commentsSCP_SS_line label="commentsSCP_SS_line" />
            <nonstdPNReasonCode_SS_line label="NonstdPNReasonCode_SS_line">Customer Requirement (Quality Requirement)</nonstdPNReasonCode_SS_line>
            <grossMargin_line label="Gross Margin" />
            <priceLock_SS_line display_value="false" label="Price Lock">false</priceLock_SS_line>
            <_pricing_engine_info />
        </document>
    </data_xml>
</transaction>

通常在HTML中,元素内部的换行符不会被呈现为换行符,这与XSLT无关,这只是HTML的工作方式。如果要在HTML中显式换行,请使用br元素,例如<br/>。因此,在您的模板中,用<xsl:copy-of select="$by"/>替换<xsl:value-of select="$by" />,并用<xsl:with-param name="by"><br/></xsl:with-param>调用模板,然后确保使用<xsl:copy-of select="$myVar"/>

以下是经过调整的样本:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<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:copy-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:template match="/">

<xsl:variable name="CPC" select="./_line_item_comment"/>
<xsl:variable name="newline"><xsl:text>&#10;</xsl:text></xsl:variable>
  <xsl:variable name="myVar">
    <xsl:call-template name="string-replace-all">
      <xsl:with-param name="text" select="substring-after($CPC,'!$!')" />
      <xsl:with-param name="replace" select="'*'" />
      <xsl:with-param name="by"><br/></xsl:with-param>
    </xsl:call-template>
  </xsl:variable>
  <xsl:copy-of select="$myVar"/>
</xsl:template>
</xsl:transform>

另一种选择是,HTML将换行符呈现为<pre style="white-space: pre">...</pre>中的换行符。因此,如果你想使用现有的代码,那么使用例如

<pre style="white-space: pre"><xsl:value-of select="$myVar"/></pre>

相关内容

  • 没有找到相关文章

最新更新