如何对xslt中的元素(列)进行排序以将xml文件转换为csv格式
非常感谢任何创建此类xsl文件的好主意,感谢您理解我的问题并在这方面帮助我。I.此XSLT 1.0转换:如何对xslt中的元素(列)进行排序以将xml文件转换为csv格式,xslt,xslt-2.0,xslt-1.0,xslcompiledtransform,Xslt,Xslt 2.0,Xslt 1.0,Xslcompiledtransform,非常感谢任何创建此类xsl文件的好主意,感谢您理解我的问题并在这方面帮助我。I.此XSLT 1.0转换: Col2 Col1 Col50 Col4 ... ... ... .... <Report p1:schemaLocation="Customer details http://reportserver?%2fCustomer details&rs%3aFormat=XML&rc%3aSchema=True"
Col2 Col1 Col50 Col4
... ... ... ....
<Report
p1:schemaLocation="Customer details http://reportserver?%2fCustomer details&rs%3aFormat=XML&rc%3aSchema=True"
Name="Customer details"
xmlns:p1="http://www.w3.org/2001/XMLSchema-instance"
xmlns="Customer details">
<table2>
<Detail_Collection>
<Detail Col1="aaa1" col1_SeqID="2" col1_Include="1"
Col2="aaa2" col2_SeqID="1" col2_Include="1"
Col3="aaa3" col3_SeqID="" col3_Include="0"
Col4="aaa4" col4_SeqID="4" col4_Include="1"
Col5="aaa5" col5_SeqID="" col5_Include="0"
Col50="aaa50" col50_SeqID="3" col50_Include="1"
/>
</Detail_Collection>
</table2>
</Report>
number($s) = number($s)
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:c="Customer details">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="c:Detail">
<xsl:apply-templates select=
"@*[ends-with(name(),'_SeqID')
and . castable as xs:integer]">
<xsl:sort select="xs:integer(.)"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="@*">
<xsl:if test="not(position()=1)">,</xsl:if>
<xsl:value-of select=
"../@*
[name()
eq
concat('Col',translate(name(current()),'col_SeqID',''))]"/>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:c="Customer details">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="c:Detail">
<xsl:for-each select=
"@*[substring(name(), string-length(name())-5)
= '_SeqID'
and
number(.) = number(.)
]
">
<xsl:sort data-type="number"/>
<xsl:value-of select=
"concat('Col',
substring-before(substring(name(current()),4),
'_')
)
"/>
<xsl:text>	</xsl:text>
</xsl:for-each>
<xsl:text> </xsl:text>
<xsl:apply-templates select=
"@*[substring(name(), string-length(name())-5)
= '_SeqID'
and
number(.) = number(.)
]
">
<xsl:sort data-type="number"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="@*">
<xsl:if test="not(position()=1)">,</xsl:if>
<xsl:value-of select=
"../@*
[name()
=
concat('Col',substring-before(substring(name(current()),4),'_'))
]"/>
</xsl:template>
</xsl:stylesheet>
说明:
Col2 Col1 Col50 Col4
... ... ... ....
<Report
p1:schemaLocation="Customer details http://reportserver?%2fCustomer details&rs%3aFormat=XML&rc%3aSchema=True"
Name="Customer details"
xmlns:p1="http://www.w3.org/2001/XMLSchema-instance"
xmlns="Customer details">
<table2>
<Detail_Collection>
<Detail Col1="aaa1" col1_SeqID="2" col1_Include="1"
Col2="aaa2" col2_SeqID="1" col2_Include="1"
Col3="aaa3" col3_SeqID="" col3_Include="0"
Col4="aaa4" col4_SeqID="4" col4_Include="1"
Col5="aaa5" col5_SeqID="" col5_Include="0"
Col50="aaa50" col50_SeqID="3" col50_Include="1"
/>
</Detail_Collection>
</table2>
</Report>
number($s) = number($s)
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:c="Customer details">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="c:Detail">
<xsl:apply-templates select=
"@*[ends-with(name(),'_SeqID')
and . castable as xs:integer]">
<xsl:sort select="xs:integer(.)"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="@*">
<xsl:if test="not(position()=1)">,</xsl:if>
<xsl:value-of select=
"../@*
[name()
eq
concat('Col',translate(name(current()),'col_SeqID',''))]"/>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:c="Customer details">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="c:Detail">
<xsl:for-each select=
"@*[substring(name(), string-length(name())-5)
= '_SeqID'
and
number(.) = number(.)
]
">
<xsl:sort data-type="number"/>
<xsl:value-of select=
"concat('Col',
substring-before(substring(name(current()),4),
'_')
)
"/>
<xsl:text>	</xsl:text>
</xsl:for-each>
<xsl:text> </xsl:text>
<xsl:apply-templates select=
"@*[substring(name(), string-length(name())-5)
= '_SeqID'
and
number(.) = number(.)
]
">
<xsl:sort data-type="number"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="@*">
<xsl:if test="not(position()=1)">,</xsl:if>
<xsl:value-of select=
"../@*
[name()
=
concat('Col',substring-before(substring(name(current()),4),'_'))
]"/>
</xsl:template>
</xsl:stylesheet>
substring($s1, string-length($s1) - string-length($s2) +1) = $s2
.2。适当使用
、子字符串()
、名称()
和当前()
.3。使用字符串$s
可转换为数字的事实,当且仅当:
__
II。XSLT 2.0解决方案:
Col2 Col1 Col50 Col4
... ... ... ....
<Report
p1:schemaLocation="Customer details http://reportserver?%2fCustomer details&rs%3aFormat=XML&rc%3aSchema=True"
Name="Customer details"
xmlns:p1="http://www.w3.org/2001/XMLSchema-instance"
xmlns="Customer details">
<table2>
<Detail_Collection>
<Detail Col1="aaa1" col1_SeqID="2" col1_Include="1"
Col2="aaa2" col2_SeqID="1" col2_Include="1"
Col3="aaa3" col3_SeqID="" col3_Include="0"
Col4="aaa4" col4_SeqID="4" col4_Include="1"
Col5="aaa5" col5_SeqID="" col5_Include="0"
Col50="aaa50" col50_SeqID="3" col50_Include="1"
/>
</Detail_Collection>
</table2>
</Report>
number($s) = number($s)
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:c="Customer details">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="c:Detail">
<xsl:apply-templates select=
"@*[ends-with(name(),'_SeqID')
and . castable as xs:integer]">
<xsl:sort select="xs:integer(.)"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="@*">
<xsl:if test="not(position()=1)">,</xsl:if>
<xsl:value-of select=
"../@*
[name()
eq
concat('Col',translate(name(current()),'col_SeqID',''))]"/>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:c="Customer details">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="c:Detail">
<xsl:for-each select=
"@*[substring(name(), string-length(name())-5)
= '_SeqID'
and
number(.) = number(.)
]
">
<xsl:sort data-type="number"/>
<xsl:value-of select=
"concat('Col',
substring-before(substring(name(current()),4),
'_')
)
"/>
<xsl:text>	</xsl:text>
</xsl:for-each>
<xsl:text> </xsl:text>
<xsl:apply-templates select=
"@*[substring(name(), string-length(name())-5)
= '_SeqID'
and
number(.) = number(.)
]
">
<xsl:sort data-type="number"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="@*">
<xsl:if test="not(position()=1)">,</xsl:if>
<xsl:value-of select=
"../@*
[name()
=
concat('Col',substring-before(substring(name(current()),4),'_'))
]"/>
</xsl:template>
</xsl:stylesheet>
更新:@desi要求也生成标题
这里是更新后的XSLT 1.0转换(如图所示,@desi仅限于使用XSLT 1.0),它实现了以下功能:
aaa2,aaa1,aaa50,aaa4
非常感谢,迪米特!!!。这是很有帮助的,而且很受欢迎。你的回答很有礼貌,也很简单。如果我想在第一行中插入标题,如何在xslt文件中提及这些标题,请建议我。非常感谢。我目前只使用XSL1.0版本,因为我的.net应用程序只接受1种格式(即1.0版)。再次感谢你的回答。嗨,迪米特里,我不知道如何再次跟进这个问题。我正在尝试在我已更新的xml文件上执行解决方案中的xsl脚本(快照附在下面的链接中)。脚本工作正常,直到在xml文件中找到“Detail”元素,然后它只循环所有“Detail”元素,但不做任何事情或获取任何值。如果我需要修改xsl(1.0)脚本,请帮助我。