如何使用XSLT 1.0用列表的特定元素的文本创建属性值



我想创建一个属性并为其分配一个值,即节点的第一个,第二个和最后一个元素的文本。

这里是XML

<?xml version="1.0" encoding="UTF-8"?>
<WORK>
<TASKS>
<OFFICE PosA="1" PosB="2" Homeoffice="yes" name="Accountant">
<SECTION>
<business Key="Code OFFICE" type="Position"> BOSS </business>
</SECTION>
<information>
<var>this is </var> 
<var>the information </var> 
<var>not this </var> 
<var>neither this </var> 
<var>I want </var> 
</information>
</OFFICE>
<OFFICE PosA="10" PosB="11" Homeoffice="NO" name="Engineer">
<SECTION>
<business Key="Code OFFICE" type="Position"> TECH </business>
</SECTION>
<information>
<var>this is </var> 
<var>the information </var> 
<var>not this </var> 
<var>neither this </var> 
<var>I want </var>
</information>
</OFFICE>
</TASKS>
</WORK>

我希望这个在输出xml中:

<?xml version="1.0" encoding="UTF-8"?>
<Branch>
<Company complete="this is the information I want"> </Company>
<Company complete="this is the information I want"> </Company>
</Branch>

我有这个XSLT,但是它不起作用

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ex="http://exslt.org/dates-and-times">
<xsl:output version="1.0" encoding="UTF-8" standalone="yes" indent="yes" method="html"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<Branch>
<xsl:for-each select="WORK/TASK/OFFICE">
<Company>
<xsl:attribute name="Complete">
<xsl:value-of select="information/var[1]/normalize- space(text())"/>
<xsl:value-of select="information/var[2]/normalize- space(text())"/>
<xsl:value-of select="information/var[5]/normalize- space(text())"/>
</xsl:attribute>
</Company>
</Branch>
</xsl:template>
</xsl:stylesheet>

我如何重写xslt来得到我想要的?任何帮助都太好了!

首先,我必须修复代码中的一些小错误:

  • 缺少xsl:for-each结束标签
  • TASK应该是TASKS
  • 不支持HTML 1.0版本
  • 删除"normalize- space()"(!)

您已经使用了2.0结构—在"/"运营商——所以你要改变这个如果你使用XSLT 1.0(标记在你的问题建议)。它必须是normalize-space(information/var[1])。不需要调用text()

但我不知道为什么你使用normalize-space()时,你显然需要保留输入数据中的最后一个空格。