从CSV文件中获取IH_TITLE的正确值,并将其注入到元素中。

huangapple go评论53阅读模式
英文:

Fetching the correct value of IH_TITLE from csv file and inject in span element

问题

需要从CSV文件中的IH_No与span元素的id属性匹配来在span元素中注入标题。一旦HTML span元素的id属性与CSV文件中的IH_No匹配,那么应该在span元素中注入相应的IH_Title值。

已经有了部分解决方案,只是无法仅获取span元素中的IH_TITLE值,而是整行都被获取。

注意:这只是原始CSV文件的一部分,而且数字将约为100,000个左右,因此需要一种无需使用多个条件来处理CSV中的IH_NO的解决方案。

以下是部分XSL代码,用于部分工作的情况,当输入具有IH_NO和IH_TITLE时。然而,如果CSV文件中有多列,则不起作用。

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>

<xsl:key name="title" match="IH_TITLE" use="@IH_NO" />

<xsl:variable name="titles">
    <xsl:for-each select="tokenize(unparsed-text('test.csv'), '\n')">
        <IH_TITLE IH_NO="{translate(substring-before(. ,';'), '&quot;', '')}">
            <xsl:value-of select="tokenize(., ';')[3]" />
        </IH_TITLE>
    </xsl:for-each>
</xsl:variable>

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

<xsl:template match="span[@class='ih']">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:value-of select="key('title', substring-after(@id, 'ih-'), $titles)" />
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

当前的HTML结构如下:

<!DOCTYPE html>
<html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <div class="chapter">            
            <h1 class="title">
                <span class="ih" id="ih-8000003034"></span>
            </h1>
         </div>
        <div class="chapter">            
            <h1 class="title">
                <span class="ih" id="ih-8000003052"></span>
            </h1>
        </div>
        <div class="chapter">            
            <h1 class="title">
                <span class="ih" id="ih-8000003058"></span>
            </h1>
        </div>
    </body>
</html>

期望的HTML结构如下:

<!DOCTYPE html>
<html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <div class="chapter">            
            <h1 class="title">
                <span class="ih" id="ih-8000003034">replace Hydraulic pump, replace</span>
            </h1>
         </div>
        <div class "chapter">
            <h1 class="title">
                <span class="ih" id="ih-8000003052">replace;Hydraulic pump, replace</span>
            </h1>
        </div>
        <div class="chapter">            
            <h1 class="title">
                <span class="ih" id="ih-8000003058">replace;Hydraulic pump, replace</span>
            </h1>
        </div>
    </body>
</html>

CSV文件的文件名为test.csv,需要将此CSV文件导入到XSL中,并将值与span元素匹配,然后获取span元素中的IH_Title值。

英文:

Need to inject title in span element from csv file matching id attribute of span element with IH_No in csv file. Once HTML span element id attribute matches with IH_No in csv file then corresponding IH_Title value in csv should be injected in span element

I have got the partial solution it just that I am not able to fetch just the IH_TITLE value in span element instead the whole row is getting fetched.

Note: This is just chunk of the original csv file moreover the numbers will be 100K approx, So need a solution without having to use multiple conditions for IH_NO in csv.

The below XSl is working partially, when input the input has just IH_NO and IH_TITLE, However its is not working if it has multiple columns in csv file.

&lt;xsl:stylesheet version=&quot;2.0&quot; 
xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
&lt;xsl:strip-space elements=&quot;*&quot;/&gt;

&lt;xsl:key name=&quot;title&quot; match=&quot;IH_TITLE&quot; use=&quot;@IH_NO&quot; /&gt;

&lt;xsl:variable name=&quot;titles&quot;&gt;
    &lt;xsl:for-each select=&quot;tokenize(unparsed-text(&#39;test.csv&#39;), &#39;\n&#39;)&quot;&gt;
        &lt;IH_TITLE IH_NO=&quot;{translate(substring-before(., &#39;;&#39;), &#39;&amp;quot;&#39;, &#39;&#39;)}&quot;&gt;
            &lt;xsl:value-of select=&quot;tokenize(., &#39;;&#39;)[2]&quot; /&gt;
            &lt;xsl:value-of select=&quot;substring-before(
                    substring-after ( substring-after (.,  &#39;;&#39;),  &#39;;&#39;)
                    , &#39;;&#39;)&quot;/&gt;
        &lt;/IH_TITLE&gt;
    &lt;/xsl:for-each&gt;
&lt;/xsl:variable&gt;

&lt;!-- identity transform --&gt;
&lt;xsl:template match=&quot;@*|node()&quot;&gt;
    &lt;xsl:copy&gt;
        &lt;xsl:apply-templates select=&quot;@*|node()&quot;/&gt;
    &lt;/xsl:copy&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match=&quot;span[@class=&#39;ih&#39;]&quot;&gt;
    &lt;xsl:copy&gt;
        &lt;xsl:copy-of select=&quot;@*&quot;/&gt;
        &lt;xsl:value-of select=&quot;key(&#39;title&#39;, substring-after(@id, &#39;ih-&#39;), $titles)&quot; /&gt;
    &lt;/xsl:copy&gt;
&lt;/xsl:template&gt;

&lt;/xsl:stylesheet&gt;

Current HTML structure

&lt;!DOCTYPE html&gt;
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Test&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;div class=&quot;chapter&quot;&gt;            
            &lt;h1 class=&quot;title&quot;&gt;
                &lt;span class=&quot;ih&quot; id=&quot;ih-8000003034&quot;&gt;&lt;/span&gt;
            &lt;/h1&gt;
         &lt;/div&gt;
        &lt;div class=&quot;chapter&quot;&gt;            
            &lt;h1 class=&quot;title&quot;&gt;
                &lt;span class=&quot;ih&quot; id=&quot;ih-8000003052&quot;&gt;&lt;/span&gt;
            &lt;/h1&gt;
        &lt;/div&gt;
        &lt;div class=&quot;chapter&quot;&gt;            
            &lt;h1 class=&quot;title&quot;&gt;
                &lt;span class=&quot;ih&quot; id=&quot;ih-8000003058&quot;&gt;&lt;/span&gt;
            &lt;/h1&gt;
        &lt;/div&gt;
    &lt;/body&gt;


&lt;/html&gt;

Expected HTML structure

&lt;!DOCTYPE html&gt;
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Test&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;div class=&quot;chapter&quot;&gt;            
            &lt;h1 class=&quot;title&quot;&gt;
                &lt;span class=&quot;ih&quot; id=&quot;ih-8000003034&quot;&gt;replace Hydraulic pump, replace&lt;/span&gt;
            &lt;/h1&gt;
         &lt;/div&gt;
        &lt;div class=&quot;chapter&quot;&gt;            
            &lt;h1 class=&quot;title&quot;&gt;
                &lt;span class=&quot;ih&quot; id=&quot;ih-8000003052&quot;&gt;replace;Hydraulic pump, replace&lt;/span&gt;
            &lt;/h1&gt;
        &lt;/div&gt;
        &lt;div class=&quot;chapter&quot;&gt;            
            &lt;h1 class=&quot;title&quot;&gt;
                &lt;span class=&quot;ih&quot; id=&quot;ih-8000003058&quot;&gt;replace;Hydraulic pump, replace&lt;/span&gt;
            &lt;/h1&gt;
        &lt;/div&gt;
    &lt;/body&gt;
&lt;/html&gt;

Filename for csv is test.csv, Need to import this csv file in xsl and match the values with span element and fetch the IH_Title values in span element.

&quot;IH_NO&quot;;&quot;OP_NO&quot;;&quot;IH_TITLE&quot;;&quot;OP_ID&quot;
&quot;8000003034&quot;;&quot;26202-2&quot;;replace;Coolant pump, replace;&quot;26202&quot;
&quot;8000003052&quot;;&quot;26202-3&quot;;replace;Fuel pump, replace;&quot;26203&quot;
&quot;8000003058&quot;;&quot;26202-4&quot;;replace;Hydraulic pump, replace;&quot;26204&quot;

答案1

得分: 0

titles 变量的定义更改为:

<xsl:variable name="titles">
    <xsl:for-each select="tokenize(unparsed-text('test.csv'), '\n')">
        <xsl:variable name="cells" select="tokenize(., ';')" />
        <IH_TITLE IH_NO="{translate($cells[1], '&quot;', '')}">
            <xsl:value-of select="$cells[3], $cells[4]" separator=";"/>
        </IH_TITLE>
    </xsl:for-each>
</xsl:variable>
英文:

Change the definition of the titles variable to:

&lt;xsl:variable name=&quot;titles&quot;&gt;
    &lt;xsl:for-each select=&quot;tokenize(unparsed-text(&#39;test.csv&#39;), &#39;\n&#39;)&quot;&gt;
    	&lt;xsl:variable name=&quot;cells&quot; select=&quot;tokenize(., &#39;;&#39;)&quot; /&gt;
        &lt;IH_TITLE IH_NO=&quot;{translate($cells[1], &#39;&amp;quot;&#39;, &#39;&#39;)}&quot;&gt;
            &lt;xsl:value-of select=&quot;$cells[3], $cells[4]&quot; separator=&quot;;&quot;/&gt;
        &lt;/IH_TITLE&gt;
    &lt;/xsl:for-each&gt;
&lt;/xsl:variable&gt;

huangapple
  • 本文由 发表于 2023年2月16日 16:00:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469325.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定