XSLT 帮助 - 重复数据 [英] XSLT assistance - repeating data
问题描述
我遇到了一个问题,即没有与某些数据相关联的有用标签,数据包含在文档的开头,但我无法让它看起来像我想要的那样.
I have an issue where there is no useful tags associated with some data, the data is included at the head of the document, but I can not get this to look how I want.
我已经用 Column1 文本替换了标签,但是因为它们都被标记为 ,所以它没有多大帮助.我正在慢慢了解 XSLT 的工作原理,但这超出了我的知识范围.
I've had the Column1 text replacing the tags, but as they are all labelled , it doesn't help much. I'm slowly learning how XSLTs work, but this is beyond my knowledge.
如有任何帮助,我们将不胜感激.
Any assistance would be appreciated.
输入数据
<results>
<header>
<a>Column 1 Customer Name</a>
<a>Column 2 Customer Add</a>
<a>Column3</a>
<a>Column4</a>
</header>
<body>
<line>
<a>Data1</a>
<a>Data2</a>
<a>Data3</a>
<a>Data4</a>
</line>
<line>
<a>Data1</a>
<a>Data2</a>
<a>Data3</a>
<a>Data4</a>
</line>
</body>
</results>
>
必需的 XML
<?xml version="1.0"?>
<results>
<header>
<a>Column 1 Customer Name</a>
<a>Column 2 Customer Add</a>
<a>Column3</a>
<a>Column4</a>
</header>
<body>
<line>
<Column1CustomerName>Data1</Column1CustomerName>
<Column2CustomerAdd>Data2</Column2CustomerAdd>
<Column3>Data3</Column3>
<Column4>Data4</Column4>
</line>
<line>
<Column1CustomerName>Data1</Column1CustomerName>
<Column2CustomerAdd>Data2</Column2CustomerAdd>
<Column3>Data3</Column3>
<Column4>Data4</Column4>
</line>
</body>
</results>
根据用户的响应,我实现了以下 XSLT:
Following on from a users response, I have implemented the following XSLT:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<xsl:template match="/results">
<results>
<xsl:copy-of select="header"/>
<body>
<xsl:for-each select="body/line">
<line>
<xsl:for-each select="a">
<xsl:variable name="index" select="position()" />
<xsl:element name="{//header/a[$index]}">
<xsl:value-of select="." />
</xsl:element>
</xsl:for-each>
</line>
</xsl:for-each>
</body>
</results>
</xsl:template>
</xsl:stylesheet>
我现在的问题是标题是否包含空格,因为这会导致样式表尝试创建一个带有空格的 XML 标记.
My problem now is if a header contains a space, as this causes the style sheet to try and create an XML tag with a space in.
我已尝试使用已建议的替换功能.
I've tried using the replace function, which has been suggested.
<xsl:element name="{replace(//cols/c[$index],' ','')}">
而且我似乎弄错了,或者在错误的地方调用它.
And I seem to be getting this wrong, or calling this in the wrong place.
感谢大家的投入.
推荐答案
你需要
来解决这个问题.
You need <xsl:element>
to do the trick.
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<xsl:template match="/results">
<results>
<xsl:copy-of select="header"/>
<body>
<xsl:for-each select="body/line">
<line>
<xsl:for-each select="a">
<xsl:variable name="index" select="position()" />
<xsl:element name="{replace(//header/a[$index], ' ', '')}">
<xsl:value-of select="." />
</xsl:element>
</xsl:for-each>
</line>
</xsl:for-each>
</body>
</results>
</xsl:template>
</xsl:stylesheet>
这篇关于XSLT 帮助 - 重复数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!