使用循环函数使用 xsl 将复杂的 xml 转换为 csv
我这里有一个 xml 文件..
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE dependencies SYSTEM "http://depfind.sourceforge.net/dtd/dependencies.dtd">
<dependencies>
<package confirmed="yes">
<name>com.infopro.Dependency</name>
<class confirmed="yes">
<name>com.infopro.Dependency.A</name>
<outbound type="class" confirmed="no">java.lang.Object</outbound>
<outbound type="class" confirmed="no">javax.ejb.Stateless</outbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.B.B()</inbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.B.a</inbound>
<feature confirmed="yes">
<name>com.infopro.Dependency.A.A()</name>
<outbound type="feature" confirmed="yes">com.infopro.Dependency.A.b</outbound>
<outbound type="class" confirmed="yes">com.infopro.Dependency.B</outbound>
<outbound type="feature" confirmed="yes">com.infopro.Dependency.B.B()</outbound>
<outbound type="feature" confirmed="no">java.lang.Object.Object()</outbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.B.B()</inbound>
</feature>
<feature confirmed="yes">
<name>com.infopro.Dependency.A.b</name>
<outbound type="class" confirmed="yes">com.infopro.Dependency.B</outbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.A.A()</inbound>
</feature>
<feature confirmed="yes">
<name>com.infopro.Dependency.A.check(java.lang.String, java.lang.String)</name>
<outbound type="feature" confirmed="yes">com.infopro.Dependency.A.stat</outbound>
<outbound type="class" confirmed="no">java.lang.String</outbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.A.doStatus()</inbound>
</feature>
<feature confirmed="yes">
<name>com.infopro.Dependency.A.doStatus()</name>
<outbound type="feature" confirmed="yes">com.infopro.Dependency.A.check(java.lang.String, java.lang.String)</outbound>
<outbound type="feature" confirmed="yes">com.infopro.Dependency.A.name</outbound>
<outbound type="feature" confirmed="yes">com.infopro.Dependency.A.user_id</outbound>
<outbound type="class" confirmed="no">java.lang.String</outbound>
</feature>
<feature confirmed="yes">
<name>com.infopro.Dependency.A.name</name>
<outbound type="class" confirmed="no">java.lang.String</outbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.A.doStatus()</inbound>
</feature>
<feature confirmed="yes">
<name>com.infopro.Dependency.A.stat</name>
<outbound type="class" confirmed="no">java.lang.String</outbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.A.check(java.lang.String, java.lang.String)</inbound>
</feature>
<feature confirmed="yes">
<name>com.infopro.Dependency.A.user_id</name>
<outbound type="class" confirmed="no">java.lang.String</outbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.A.doStatus()</inbound>
</feature>
</class>
<class confirmed="yes">
<name>com.infopro.Dependency.B</name>
<outbound type="class" confirmed="yes">com.infopro.Dependency.C</outbound>
<outbound type="class" confirmed="no">java.lang.Object</outbound>
<outbound type="class" confirmed="no">javax.ejb.Stateless</outbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.A.A()</inbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.A.b</inbound>
<feature confirmed="yes">
<name>com.infopro.Dependency.B.B()</name>
<outbound type="class" confirmed="yes">com.infopro.Dependency.A</outbound>
<outbound type="feature" confirmed="yes">com.infopro.Dependency.A.A()</outbound>
<outbound type="feature" confirmed="yes">com.infopro.Dependency.B.a</outbound>
<outbound type="feature" confirmed="no">java.lang.Object.Object()</outbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.A.A()</inbound>
</feature>
<feature confirmed="yes">
<name>com.infopro.Dependency.B.a</name>
<outbound type="class" confirmed="yes">com.infopro.Dependency.A</outbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.B.B()</inbound>
</feature>
<feature confirmed="yes">
<name>com.infopro.Dependency.B.check(java.lang.String, java.lang.String)</name>
<outbound type="feature" confirmed="yes">com.infopro.Dependency.B.stat</outbound>
<outbound type="class" confirmed="no">java.lang.String</outbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.B.doStatus()</inbound>
</feature>
<feature confirmed="yes">
<name>com.infopro.Dependency.B.doStatus()</name>
<outbound type="feature" confirmed="yes">com.infopro.Dependency.B.check(java.lang.String, java.lang.String)</outbound>
<outbound type="feature" confirmed="yes">com.infopro.Dependency.B.name</outbound>
<outbound type="feature" confirmed="yes">com.infopro.Dependency.B.user_id</outbound>
<outbound type="class" confirmed="no">java.lang.String</outbound>
</feature>
<feature confirmed="yes">
<name>com.infopro.Dependency.B.name</name>
<outbound type="class" confirmed="no">java.lang.String</outbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.B.doStatus()</inbound>
</feature>
<feature confirmed="yes">
<name>com.infopro.Dependency.B.stat</name>
<outbound type="class" confirmed="no">java.lang.String</outbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.B.check(java.lang.String, java.lang.String)</inbound>
</feature>
<feature confirmed="yes">
<name>com.infopro.Dependency.B.user_id</name>
<outbound type="class" confirmed="no">java.lang.String</outbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.B.doStatus()</inbound>
</feature>
</class>
<class confirmed="yes">
<name>com.infopro.Dependency.C</name>
<outbound type="class" confirmed="no">java.lang.Object</outbound>
<outbound type="class" confirmed="no">javax.ejb.Remote</outbound>
<inbound type="class" confirmed="yes">com.infopro.Dependency.B</inbound>
<feature confirmed="yes">
<name>com.infopro.Dependency.C.doStatus()</name>
</feature>
</class>
</package>
<package confirmed="no">
<name>java.lang</name>
<class confirmed="no">
<name>java.lang.Object</name>
<inbound type="class" confirmed="yes">com.infopro.Dependency.A</inbound>
<inbound type="class" confirmed="yes">com.infopro.Dependency.B</inbound>
<inbound type="class" confirmed="yes">com.infopro.Dependency.C</inbound>
<feature confirmed="no">
<name>java.lang.Object.Object()</name>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.A.A()</inbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.B.B()</inbound>
</feature>
</class>
<class confirmed="no">
<name>java.lang.String</name>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.A.check(java.lang.String, java.lang.String)</inbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.A.doStatus()</inbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.A.name</inbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.A.stat</inbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.A.user_id</inbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.B.check(java.lang.String, java.lang.String)</inbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.B.doStatus()</inbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.B.name</inbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.B.stat</inbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.B.user_id</inbound>
</class>
</package>
<package confirmed="no">
<name>javax.ejb</name>
<class confirmed="no">
<name>javax.ejb.Remote</name>
<inbound type="class" confirmed="yes">com.infopro.Dependency.C</inbound>
</class>
<class confirmed="no">
<name>javax.ejb.Stateless</name>
<inbound type="class" confirmed="yes">com.infopro.Dependency.A</inbound>
<inbound type="class" confirmed="yes">com.infopro.Dependency.B</inbound>
</class>
</package>
</dependencies>
我想以这种格式创建 csv 文件的输出..
classname1, outbound.
classname1, outbound.
. .
. .
classname1, inbound.
. .
. .
classname2, outbound.
. .
classname2, inbound.
等等..
现在我面临的挑战是每个班级的入站和出站数量都不同且各不相同。所以我必须为此指定循环。而且我只需要类名包含“com”关键字或字符串,不包括包名、功能名称。我需要执行此 xsl,以便对于我生成的所有类似 xml,我可以将其更改为 csv 表形式,以便进一步处理我需要的数据。
我创建的 xsl 如下所示,但我仍然面临一些错误和问题
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="class[contains(.//text(), 'com')]">
<xsl:text></xsl:text><xsl:value-of select="name"/><xsl:if test="@confirmed='no'"> *</xsl:if><xsl:text> </xsl:text>
<xsl:for-each select="outbound[contains(.//text(), 'com')]">
<xsl:text disable-output-escaping="yes"> , </xsl:text><xsl:value-of select="."/><xsl:if test="@confirmed='no'"> *</xsl:if><xsl:text>
</xsl:text>
</xsl:for-each>
<xsl:for-each select="inbound[contains(.//text(), 'com')]">
<xsl:text disable-output-escaping="yes"> , </xsl:text><xsl:value-of select="."/><xsl:if test="@confirmed='no'"> *</xsl:if><xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
,并且输出为
com.infopro.Dependencycom.infopro.Dependency.A , com.infopro.Dependency.B.B()
, com.infopro.Dependency.B.a
com.infopro.Dependency.B , com.infopro.Dependency.C
, com.infopro.Dependency.A.A()
, com.infopro.Dependency.A.b
com.infopro.Dependency.C , com.infopro.Dependency.B
java.langjava.lang.Objectcom.infopro.Dependency.Acom.infopro.Dependency.Bcom.infopro.Dependency.Cjava.lang.Object.Object()com.infopro.Dependency.A.A()com.infopro.Dependency.B.B()java.lang.Stringcom.infopro.Dependency.A.check(java.lang.String, java.lang.String)com.infopro.Dependency.A.doStatus()com.infopro.Dependency.A.namecom.infopro.Dependency.A.statcom.infopro.Dependency.A.user_idcom.infopro.Dependency.B.check(java.lang.String, java.lang.String)com.infopro.Dependency.B.doStatus()com.infopro.Dependency.B.namecom.infopro.Dependency.B.statcom.infopro.Dependency.B.user_idjavax.ejbjavax.ejb.Remotecom.infopro.Dependency.Cjavax.ejb.Statelesscom.infopro.Dependency.Acom.infopro.Dependency.B
类似表格的排列几乎发生,但包名称的问题仍然坚持第一个类名称,以及最后一些不需要的 java 字符串的长字符串。 希望得到指导和帮助。谢谢
I having a xml file here..
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE dependencies SYSTEM "http://depfind.sourceforge.net/dtd/dependencies.dtd">
<dependencies>
<package confirmed="yes">
<name>com.infopro.Dependency</name>
<class confirmed="yes">
<name>com.infopro.Dependency.A</name>
<outbound type="class" confirmed="no">java.lang.Object</outbound>
<outbound type="class" confirmed="no">javax.ejb.Stateless</outbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.B.B()</inbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.B.a</inbound>
<feature confirmed="yes">
<name>com.infopro.Dependency.A.A()</name>
<outbound type="feature" confirmed="yes">com.infopro.Dependency.A.b</outbound>
<outbound type="class" confirmed="yes">com.infopro.Dependency.B</outbound>
<outbound type="feature" confirmed="yes">com.infopro.Dependency.B.B()</outbound>
<outbound type="feature" confirmed="no">java.lang.Object.Object()</outbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.B.B()</inbound>
</feature>
<feature confirmed="yes">
<name>com.infopro.Dependency.A.b</name>
<outbound type="class" confirmed="yes">com.infopro.Dependency.B</outbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.A.A()</inbound>
</feature>
<feature confirmed="yes">
<name>com.infopro.Dependency.A.check(java.lang.String, java.lang.String)</name>
<outbound type="feature" confirmed="yes">com.infopro.Dependency.A.stat</outbound>
<outbound type="class" confirmed="no">java.lang.String</outbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.A.doStatus()</inbound>
</feature>
<feature confirmed="yes">
<name>com.infopro.Dependency.A.doStatus()</name>
<outbound type="feature" confirmed="yes">com.infopro.Dependency.A.check(java.lang.String, java.lang.String)</outbound>
<outbound type="feature" confirmed="yes">com.infopro.Dependency.A.name</outbound>
<outbound type="feature" confirmed="yes">com.infopro.Dependency.A.user_id</outbound>
<outbound type="class" confirmed="no">java.lang.String</outbound>
</feature>
<feature confirmed="yes">
<name>com.infopro.Dependency.A.name</name>
<outbound type="class" confirmed="no">java.lang.String</outbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.A.doStatus()</inbound>
</feature>
<feature confirmed="yes">
<name>com.infopro.Dependency.A.stat</name>
<outbound type="class" confirmed="no">java.lang.String</outbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.A.check(java.lang.String, java.lang.String)</inbound>
</feature>
<feature confirmed="yes">
<name>com.infopro.Dependency.A.user_id</name>
<outbound type="class" confirmed="no">java.lang.String</outbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.A.doStatus()</inbound>
</feature>
</class>
<class confirmed="yes">
<name>com.infopro.Dependency.B</name>
<outbound type="class" confirmed="yes">com.infopro.Dependency.C</outbound>
<outbound type="class" confirmed="no">java.lang.Object</outbound>
<outbound type="class" confirmed="no">javax.ejb.Stateless</outbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.A.A()</inbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.A.b</inbound>
<feature confirmed="yes">
<name>com.infopro.Dependency.B.B()</name>
<outbound type="class" confirmed="yes">com.infopro.Dependency.A</outbound>
<outbound type="feature" confirmed="yes">com.infopro.Dependency.A.A()</outbound>
<outbound type="feature" confirmed="yes">com.infopro.Dependency.B.a</outbound>
<outbound type="feature" confirmed="no">java.lang.Object.Object()</outbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.A.A()</inbound>
</feature>
<feature confirmed="yes">
<name>com.infopro.Dependency.B.a</name>
<outbound type="class" confirmed="yes">com.infopro.Dependency.A</outbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.B.B()</inbound>
</feature>
<feature confirmed="yes">
<name>com.infopro.Dependency.B.check(java.lang.String, java.lang.String)</name>
<outbound type="feature" confirmed="yes">com.infopro.Dependency.B.stat</outbound>
<outbound type="class" confirmed="no">java.lang.String</outbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.B.doStatus()</inbound>
</feature>
<feature confirmed="yes">
<name>com.infopro.Dependency.B.doStatus()</name>
<outbound type="feature" confirmed="yes">com.infopro.Dependency.B.check(java.lang.String, java.lang.String)</outbound>
<outbound type="feature" confirmed="yes">com.infopro.Dependency.B.name</outbound>
<outbound type="feature" confirmed="yes">com.infopro.Dependency.B.user_id</outbound>
<outbound type="class" confirmed="no">java.lang.String</outbound>
</feature>
<feature confirmed="yes">
<name>com.infopro.Dependency.B.name</name>
<outbound type="class" confirmed="no">java.lang.String</outbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.B.doStatus()</inbound>
</feature>
<feature confirmed="yes">
<name>com.infopro.Dependency.B.stat</name>
<outbound type="class" confirmed="no">java.lang.String</outbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.B.check(java.lang.String, java.lang.String)</inbound>
</feature>
<feature confirmed="yes">
<name>com.infopro.Dependency.B.user_id</name>
<outbound type="class" confirmed="no">java.lang.String</outbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.B.doStatus()</inbound>
</feature>
</class>
<class confirmed="yes">
<name>com.infopro.Dependency.C</name>
<outbound type="class" confirmed="no">java.lang.Object</outbound>
<outbound type="class" confirmed="no">javax.ejb.Remote</outbound>
<inbound type="class" confirmed="yes">com.infopro.Dependency.B</inbound>
<feature confirmed="yes">
<name>com.infopro.Dependency.C.doStatus()</name>
</feature>
</class>
</package>
<package confirmed="no">
<name>java.lang</name>
<class confirmed="no">
<name>java.lang.Object</name>
<inbound type="class" confirmed="yes">com.infopro.Dependency.A</inbound>
<inbound type="class" confirmed="yes">com.infopro.Dependency.B</inbound>
<inbound type="class" confirmed="yes">com.infopro.Dependency.C</inbound>
<feature confirmed="no">
<name>java.lang.Object.Object()</name>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.A.A()</inbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.B.B()</inbound>
</feature>
</class>
<class confirmed="no">
<name>java.lang.String</name>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.A.check(java.lang.String, java.lang.String)</inbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.A.doStatus()</inbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.A.name</inbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.A.stat</inbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.A.user_id</inbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.B.check(java.lang.String, java.lang.String)</inbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.B.doStatus()</inbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.B.name</inbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.B.stat</inbound>
<inbound type="feature" confirmed="yes">com.infopro.Dependency.B.user_id</inbound>
</class>
</package>
<package confirmed="no">
<name>javax.ejb</name>
<class confirmed="no">
<name>javax.ejb.Remote</name>
<inbound type="class" confirmed="yes">com.infopro.Dependency.C</inbound>
</class>
<class confirmed="no">
<name>javax.ejb.Stateless</name>
<inbound type="class" confirmed="yes">com.infopro.Dependency.A</inbound>
<inbound type="class" confirmed="yes">com.infopro.Dependency.B</inbound>
</class>
</package>
</dependencies>
I want to create a output of csv file in this format..
classname1, outbound.
classname1, outbound.
. .
. .
classname1, inbound.
. .
. .
classname2, outbound.
. .
classname2, inbound.
and so on..
Now the challenge for me is the quantity of inbound and outbound for each class are different and varies. so i have to specify looping for that. And also i just need class name contains "com" keyword or string inside excluding package name, feature name all. I need do this xsl so that for all similiar xml i generate i can change it to csv table form for my further process of data that i needed.
The xsl i created shown below but i stil facing some bugs and problems
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="class[contains(.//text(), 'com')]">
<xsl:text></xsl:text><xsl:value-of select="name"/><xsl:if test="@confirmed='no'"> *</xsl:if><xsl:text> </xsl:text>
<xsl:for-each select="outbound[contains(.//text(), 'com')]">
<xsl:text disable-output-escaping="yes"> , </xsl:text><xsl:value-of select="."/><xsl:if test="@confirmed='no'"> *</xsl:if><xsl:text>
</xsl:text>
</xsl:for-each>
<xsl:for-each select="inbound[contains(.//text(), 'com')]">
<xsl:text disable-output-escaping="yes"> , </xsl:text><xsl:value-of select="."/><xsl:if test="@confirmed='no'"> *</xsl:if><xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
And the output as
com.infopro.Dependencycom.infopro.Dependency.A , com.infopro.Dependency.B.B()
, com.infopro.Dependency.B.a
com.infopro.Dependency.B , com.infopro.Dependency.C
, com.infopro.Dependency.A.A()
, com.infopro.Dependency.A.b
com.infopro.Dependency.C , com.infopro.Dependency.B
java.langjava.lang.Objectcom.infopro.Dependency.Acom.infopro.Dependency.Bcom.infopro.Dependency.Cjava.lang.Object.Object()com.infopro.Dependency.A.A()com.infopro.Dependency.B.B()java.lang.Stringcom.infopro.Dependency.A.check(java.lang.String, java.lang.String)com.infopro.Dependency.A.doStatus()com.infopro.Dependency.A.namecom.infopro.Dependency.A.statcom.infopro.Dependency.A.user_idcom.infopro.Dependency.B.check(java.lang.String, java.lang.String)com.infopro.Dependency.B.doStatus()com.infopro.Dependency.B.namecom.infopro.Dependency.B.statcom.infopro.Dependency.B.user_idjavax.ejbjavax.ejb.Remotecom.infopro.Dependency.Cjavax.ejb.Statelesscom.infopro.Dependency.Acom.infopro.Dependency.B
The table like arrangement almost occur but having problem with package name still coming stick with first class name, and some long string of unwanted java strings at the end.
Hope for guidance and help. thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,您不必执行任何显式循环。您可以使用模板完成您想做的所有事情。
要有效地编写 XSLT,您必须从转换的角度进行思考。想一想,“我想将输入中的某些内容转换为输出中的某些内容。”那些东西是什么?您没有在问题中阐明它们,并且您可能没有在设计中阐明它们。
看起来您想要的是将每个
package
元素转换为输出中的一行。所以你需要一个模板。每行的第一个内容是包名称(即name
元素的文本内容),最后一个内容是换行符。因此,从这里开始:如果您在输入文档中运行该转换(假设您已将其放入实际的样式表中),您应该获得所有包名称的简单列表作为输出。
现在,您想要将
outbound
和inbound
元素的内容添加为每行上的附加逗号分隔元素。有什么转变?如果confirmed
属性等于no
,则每个元素都需要转换为包含逗号、元素文本内容和星号的文本。这很容易完成:只需创建这些元素的模板,并将它们应用到
package
模板中。由于您只希望显示文本内容以con.
开头的元素,因此在应用模板时添加谓词:对于这种设计和实现方法,有两点很重要:首先,您使用模板来变换节点,而不是尝试使用并非为其设计的语言来进行过程编程。
其次,您仅转换已明确选择的节点。现在输出末尾出现垃圾的原因是您已将模板应用到了您认为没有应用到的节点:您没有创建应用于文档根的模板,并且因此,XSLT 处理器将内置模板应用于它找到的每个元素。
Well, you don't have to do any explicit looping. You can do everything you're trying to do with templates.
To effectively write XSLT, you have to think in terms of transformation. Think, "I want to transform some thing in the input into some thing in the output." What are those things? You haven't articulated them in your question, and it's possible that you haven't articulated them in your design.
It seems like what you want is to convert each
package
element into a line in the output. So you'll need a template for that. The first thing on each row is the package name (i.e. the text content of thename
element), and the last thing will be a newline. So start with this:If you run that transform in your input document, (assuming you've put it into an actual stylesheet), you should get a simple list of all of the package names as the output.
Now you want to add the contents of the
outbound
andinbound
elements as additional comma-separated elements on each line. What's the transformation? Each of these elements needs to be transformed into a piece of text containing a comma, the element's text content, and an asterisk if theconfirmed
attribute equalsno
.That's easily accomplished: just create a template these elements, and apply them within the
package
template. Since you only want the elements whose text content begins withcon.
to appear, add predicates when applying the templates:Two things that are important about this approach to design and implementation: First, you are using templates to transform nodes, instead of trying to use procedural programming in a language that isn't designed for it.
Second, you're only transforming the nodes that you've explicitly selected. The reason you're getting garbage at the end of your output now is that you've applied templates to nodes that you didn't think you were applying them to: you didn't create a template that's applied to the document root, and so the XSLT processor is applying built-in templates to every element it finds.