XSLT从另一个节点选择属性
这是我的XML文件:
<root>
<headers>
<header value="type1" />
<header value="type3" />
<header value="type2" />
</headers>
<data type1="data1_1" type2="data1_2" type3="data1_3" />
<data type1="data2_1" type2="data2_2" type3="data2_3" />
</root>
我想生成CSV,仅在标题部分中列出标题。这是我的XSLT文件:
<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="no" encoding="unicode" />
<xsl:template match="root">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="headers">
<xsl:apply-templates/><xsl:text>
</xsl:text>
</xsl:template>
<xsl:template name="separator">
<xsl:text>	</xsl:text>
</xsl:template>
<!-- display first line header work ok -->
<xsl:template match="header">
<xsl:value-of select="@value" />
<xsl:if test="position() != last()">
<xsl:call-template name="separator" />
</xsl:if>
</xsl:template>
<xsl:key name="mykey" match="header" use="@value" />
<!-- For each data parameters check if exist in header and display with the good order ! NOT WORK -->
<xsl:template match="data">
<xsl:for-each select="@*">
<xsl:if test="key('mykey', name())">
<xsl:value-of select="." />
<xsl:if test="position() != last()">
<xsl:call-template name="separator" />
</xsl:if>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
我想要此输出格式(CSV):
type1 type3 type2
data1_1 data1_3 data1_2
data2_1 data2_3 data2_2
但是我的输出是(数据Type2不在正确的标题下):
type1 type3 type2
data1_1 data1_2 data1_3
data2_1 data2_2 data2_3
This is my XML file :
<root>
<headers>
<header value="type1" />
<header value="type3" />
<header value="type2" />
</headers>
<data type1="data1_1" type2="data1_2" type3="data1_3" />
<data type1="data2_1" type2="data2_2" type3="data2_3" />
</root>
And I want to generate CSV with only headers listed in headers section. This is my xslt file :
<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="no" encoding="unicode" />
<xsl:template match="root">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="headers">
<xsl:apply-templates/><xsl:text>
</xsl:text>
</xsl:template>
<xsl:template name="separator">
<xsl:text> </xsl:text>
</xsl:template>
<!-- display first line header work ok -->
<xsl:template match="header">
<xsl:value-of select="@value" />
<xsl:if test="position() != last()">
<xsl:call-template name="separator" />
</xsl:if>
</xsl:template>
<xsl:key name="mykey" match="header" use="@value" />
<!-- For each data parameters check if exist in header and display with the good order ! NOT WORK -->
<xsl:template match="data">
<xsl:for-each select="@*">
<xsl:if test="key('mykey', name())">
<xsl:value-of select="." />
<xsl:if test="position() != last()">
<xsl:call-template name="separator" />
</xsl:if>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
I want this output format (CSV):
type1 type3 type2
data1_1 data1_3 data1_2
data2_1 data2_3 data2_2
But my output is ( data type2 is not under correct header ):
type1 type3 type2
data1_1 data1_2 data1_3
data2_1 data2_2 data2_3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会简单地做:
xslt 1.0
替代(也许更有效),您可以做:
XSLT 1.0
btw,结果是一个选项卡分离的文件,而不是CSV 。
I would do simply:
XSLT 1.0
Alternatively (and perhaps a bit more efficiently), you could do:
XSLT 1.0
BTW, the result is a tab-separated file, not CSV.
属性没有设置顺序。元素可以。因此,让我们使用这些元素提供您的订购:
输出:
标题在单独的行上,因为我没有更改代码的那一部分。
Attributes have no set order. Elements do. So let's use the elements to provide your ordering:
Output:
The headers are on separate lines because I didn't change that part of your code.
这是您的XSLT,其中XSLT的次要修复程序可以执行您想要的工作:
Here is your xslt with minor fixes that does what you want: