英文:
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(. ,';'), '"', '')}">
<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.
<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(., ';')[2]" />
<xsl:value-of select="substring-before(
substring-after ( 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>
Current HTML structure
<!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>
Expected HTML structure
<!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>
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.
"IH_NO";"OP_NO";"IH_TITLE";"OP_ID"
"8000003034";"26202-2";replace;Coolant pump, replace;"26202"
"8000003052";"26202-3";replace;Fuel pump, replace;"26203"
"8000003058";"26202-4";replace;Hydraulic pump, replace;"26204"
答案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], '"', '')}">
<xsl:value-of select="$cells[3], $cells[4]" separator=";"/>
</IH_TITLE>
</xsl:for-each>
</xsl:variable>
英文:
Change the definition of the titles
variable to:
<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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论