英文:
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.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:xhtml="http://www.w3.org/1999/xhtml" exclude-result-prefixes="xs" version="2.0">
<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>
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</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";"IH_TITLE";
"8000003034";replace;Coolant pump, replace;
"8000003052";replace;Fuel pump, replace;
"8000003058";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(., ';'), '"', '')}">
<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
<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>
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:
<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>
This is a well-formed XML fragment that can be interrogated using the key()
function to return the value of the supplied key.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论