Need to inject title in span element from csv file matching id attribute of span element with IH_No in csv file

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

Need to inject title in span element from csv file matching id attribute of span element with IH_No in csv file

问题

需要从CSV文件中匹配span元素的id属性与CSV文件中的IH_No,并将对应的IH_Title值注入到span元素中。

<xsl:variable name="inject" select="tokenize(unparsed-text('test.csv'), '\n,')"/>
<xsl:variable name="value" select="document($inject)/xsl:template/@match"/>
<xsl:variable name="valuet" select="($inject)/title"/>

<xsl:template match="xhtml:div/xhtml:h1[@class = 'title']/xhtml:span/@id">
    <xsl:text>Test1</xsl:text>
    <xsl:value-of select="$valuet"/>
    <xsl:if test="xhtml:h1[@class = 'title' and fn:substring(span/@id, 4) = tokenize($value, ',')[1]]">            
        <xsl:value-of select="$valuet"/>            
    </xsl:if>
</xsl:template>

当前的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,需要在XSL中导入此CSV文件并将其与span元素的值进行匹配,然后获取span元素中的IH_Title值。

"IH_NO";"IH_TITLE";
"8000003034";"replace;Coolant pump, replace";
"8000003052";"replace;Fuel pump, replace";
"8000003058";"replace;Hydraulic pump, replace";
英文:

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

Need your help.

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.

I tried the below XSL but somehow I could not get the logic right. I tried lot of different things moreover I could not understand how to fetch values from csv. This xsl may not be completely relevant.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;xsl:stylesheet xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
    xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot; xmlns:fn=&quot;http://www.w3.org/2005/xpath-functions&quot;
    xmlns:xhtml=&quot;http://www.w3.org/1999/xhtml&quot; exclude-result-prefixes=&quot;xs&quot; version=&quot;2.0&quot;&gt;

    &lt;xsl:variable name=&quot;inject&quot; select=&quot;tokenize(unparsed-text(&#39;test.csv&#39;), &#39;\n,&gt;&#39;)&quot;/&gt;
    &lt;xsl:variable name=&quot;value&quot; select=&quot;document($inject)/xsl:template/@match&quot;/&gt;
    &lt;xsl:variable name=&quot;valuet&quot; select=&quot;($inject)/title&quot;/&gt;

    &lt;xsl:template match=&quot;xhtml:div/xhtml:h1[@class = &#39;title&#39;]/xhtml:span/@id&quot;&gt;
        &lt;xsl:text&gt;Test1&lt;/xsl:text&gt;
        &lt;xsl:value-of select=&quot;$valuet&quot;/&gt;
        &lt;xsl:if test=&quot;xhtml:h1[@class = &#39;title&#39; and fn:substring(span/@id, 4) = tokenize($value, &#39;,&#39;)[1]]&quot;&gt;            
            &lt;xsl:value-of select=&quot;$valuet&quot;/&gt;            
        &lt;/xsl:if&gt;

    &lt;/xsl:template&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: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;IH_TITLE&quot;;
&quot;8000003034&quot;;replace;Coolant pump, replace;
&quot;8000003052&quot;;replace;Fuel pump, replace;
&quot;8000003058&quot;;replace;Hydraulic pump, replace;

答案1

得分: 0

您的问题难以理解。

假设HTML文档是XSL转换的输入,您可以使用以下XSLT 2.0代码来获得一个非常接近您所展示的结果:

<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="substring-after(., ';')" />
      </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>

我没有费心去除尾随的分号,但这应该足够简单。


附加解释:

$titles变量内部,.csv文件被标记为单独的行,并为每行创建了一个IH_TITLE元素。行的第一部分用于填充IH_NO属性(去掉引号),其余部分成为元素的内容。在给定的示例中,变量的内容如下:

<IH_TITLE IH_NO="IH_NO">"IH_TITLE";</IH_TITLE>
<IH_TITLE IH_NO="8000003034">replace;Coolant pump, replace;</IH_TITLE>
<IH_TITLE IH_NO="8000003052">replace;Fuel pump, replace;</IH_TITLE>
<IH_TITLE IH_NO="8000003058">replace;Hydraulic pump, replace;</IH_TITLE>

这是一个良好格式的XML片段,可以使用key()函数来查询提供的键的值。

英文:

Your question is difficult to understand.

Assuming that the HTML document is the input to the XSL transformation, you can get a result very close to the one you show by using:

XSLT 2.0

&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;substring-after(., &#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;

I did not bother removing the trailing semicolon but it should be simple enough.


Added explanation:

Inside the $titles variable, the .csv file is tokenized to individual lines and a IH_TITLE element is created for each line. The first part of the line is used to populate the IH_NO attribute (after stripping the quotes), and the rest becomes the content of the element. In the given example, the contents of the variable are:

 &lt;IH_TITLE IH_NO=&quot;IH_NO&quot;&gt;&quot;IH_TITLE&quot;;&lt;/IH_TITLE&gt;
 &lt;IH_TITLE IH_NO=&quot;8000003034&quot;&gt;replace;Coolant pump, replace;&lt;/IH_TITLE&gt;
 &lt;IH_TITLE IH_NO=&quot;8000003052&quot;&gt;replace;Fuel pump, replace;&lt;/IH_TITLE&gt;
 &lt;IH_TITLE IH_NO=&quot;8000003058&quot;&gt;replace;Hydraulic pump, replace;&lt;/IH_TITLE&gt;

This is a well-formed XML fragment that can be interrogated using the key() function to return the value of the supplied key.

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

发表评论

匿名网友

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

确定