从样式表中提取xml属性
下面是示例xml表,我想用下面的样式表获取transitLine、transitRoute和stop元素的所有属性,但是我得到了错误的输出从样式表中提取xml属性,xml,xslt,xpath,Xml,Xslt,Xpath,下面是示例xml表,我想用下面的样式表获取transitLine、transitRoute和stop元素的所有属性,但是我得到了错误的输出 <xsl:template match="/"> transitLine,transitRoute,routeProfile,links <xsl:for-each select="//transitLine"> <xsl:value-of select="conc
<xsl:template match="/">
transitLine,transitRoute,routeProfile,links
<xsl:for-each select="//transitLine">
<xsl:value-of select="concat(@id,'
')"/>
<xsl:for-each select="//transitRoute">
<xsl:value-of select="concat(',',@id,'
')"/>
</xsl:for-each>
<xsl:for-each select="//stop">
<xsl:value-of select="concat(',',@refId,'
')"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
sample.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE transitSchedule SYSTEM "http://www.matsim.org/files/dtd/transitSchedule_v1.dtd">
<transitSchedule>
<transitStops>
<stopFacility id="1" x="-53196.450154726146" y="-3755010.0058102254" />
<stopFacility id="1.1" x="-53196.450154726146" y="-3755010.0058102254" />
<stopFacility id="1.2" x="-53196.450154726146" y="-3755010.0058102254" />
</transitStops>
<transitLine id="1001">
<transitRoute id="1001_0">
<transportMode>bus</transportMode>
<routeProfile>
<stop refId="180" Offset="00:00:00"/>
<stop refId="58" Offset="00:03:00"/>
<stop refId="152" Offset="00:05:00"/>
</routeProfile>
</transitRoute>
<transitRoute id="1001_1">
<transportMode>bus</transportMode>
<routeProfile>
<stop refId="190" Offset="00:00:00"/>
<stop refId="58" Offset="00:03:00" />
<stop refId="153" Offset="00:05:00"/>
</routeProfile>
</transitRoute>
</transitLine>
<transitLine id="10011">
<transitRoute id="10011_0">
<transportMode>bus</transportMode>
<routeProfile>
<stop refId="29.2" Offset="00:00:00" />
<stop refId="202" Offset="00:04:00" />
<stop refId="113" Offset="00:07:00" />
</routeProfile>
</transitRoute>
<transitRoute id="10011_1">
<transportMode>bus</transportMode>
<routeProfile>
<stop refId="29.2" Offset="00:00:00" />
<stop refId="191" Offset="00:04:00" />
<stop refId="187" Offset="00:07:00" />
</routeProfile>
</transitRoute>
</transitLine>
</transitSchedule>
<xsl:template match="/">
transitLine,transitRoute,routeProfile,links
<xsl:for-each select="//transitLine">
<xsl:value-of select="concat(@id,'
')"/>
<xsl:for-each select="//transitRoute">
<xsl:value-of select="concat(',',@id,'
')"/>
</xsl:for-each>
<xsl:for-each select="//stop">
<xsl:value-of select="concat(',',@refId,'
')"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
预期产出:
<xsl:template match="/">
transitLine,transitRoute,routeProfile,links
<xsl:for-each select="//transitLine">
<xsl:value-of select="concat(@id,'
')"/>
<xsl:for-each select="//transitRoute">
<xsl:value-of select="concat(',',@id,'
')"/>
</xsl:for-each>
<xsl:for-each select="//stop">
<xsl:value-of select="concat(',',@refId,'
')"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
transitLine" transitRoute routeProfile
1001"
1001_0
180
58
152
1001_1
90
10011 58
153
1001_0
29.2
202
113
1001_1
292
191
187
脚本失败的主要原因是您总是在每个for-each中 循环从整个源文件中获取特定名称的所有标记
<xsl:template match="/">
transitLine,transitRoute,routeProfile,links
<xsl:for-each select="//transitLine">
<xsl:value-of select="concat(@id,'
')"/>
<xsl:for-each select="//transitRoute">
<xsl:value-of select="concat(',',@id,'
')"/>
</xsl:for-each>
<xsl:for-each select="//stop">
<xsl:value-of select="concat(',',@refId,'
')"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
这种全取方法仅适用于最外层的环路。
对于嵌套循环,应仅获取当前元素的子元素
<xsl:template match="/">
transitLine,transitRoute,routeProfile,links
<xsl:for-each select="//transitLine">
<xsl:value-of select="concat(@id,'
')"/>
<xsl:for-each select="//transitRoute">
<xsl:value-of select="concat(',',@id,'
')"/>
</xsl:for-each>
<xsl:for-each select="//stop">
<xsl:value-of select="concat(',',@refId,'
')"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
该脚本应包含三个日益嵌套的循环:
<xsl:template match="/">
transitLine,transitRoute,routeProfile,links
<xsl:for-each select="//transitLine">
<xsl:value-of select="concat(@id,'
')"/>
<xsl:for-each select="//transitRoute">
<xsl:value-of select="concat(',',@id,'
')"/>
</xsl:for-each>
<xsl:for-each select="//stop">
<xsl:value-of select="concat(',',@refId,'
')"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
最外层-适用于所有传输线元素。
下一个用于当前传输线的子传输线元素。
和当前transitRoute中最嵌套的停止元素。
但是,这次XPath表达式应该包含。//,
因为停止元件不是电流晶体管的直接子元件。
最后一点:打印文本没有要求的前导空格,
但包含不必要的逗号
<xsl:template match="/">
transitLine,transitRoute,routeProfile,links
<xsl:for-each select="//transitLine">
<xsl:value-of select="concat(@id,'
')"/>
<xsl:for-each select="//transitRoute">
<xsl:value-of select="concat(',',@id,'
')"/>
</xsl:for-each>
<xsl:for-each select="//stop">
<xsl:value-of select="concat(',',@refId,'
')"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
因此,整个脚本应该如下所示:
<xsl:template match="/">
transitLine,transitRoute,routeProfile,links
<xsl:for-each select="//transitLine">
<xsl:value-of select="concat(@id,'
')"/>
<xsl:for-each select="//transitRoute">
<xsl:value-of select="concat(',',@id,'
')"/>
</xsl:for-each>
<xsl:for-each select="//stop">
<xsl:value-of select="concat(',',@refId,'
')"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/">
<xsl:text>transitLine transitRoute routeProfile
</xsl:text>
<xsl:for-each select="//transitLine">
<xsl:value-of select="concat(' ', @id,'
')"/>
<xsl:for-each select="transitRoute">
<xsl:value-of select="concat(' ', @id,'
')"/>
<xsl:for-each select=".//stop">
<xsl:value-of select="concat(' ',
@refId,'
')"/>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:transform>
它给出了所需的输出。您当前的输出是什么,正确的输出是什么?@JLRishe我已经编辑了问题以显示输出
<xsl:template match="/">
transitLine,transitRoute,routeProfile,links
<xsl:for-each select="//transitLine">
<xsl:value-of select="concat(@id,'
')"/>
<xsl:for-each select="//transitRoute">
<xsl:value-of select="concat(',',@id,'
')"/>
</xsl:for-each>
<xsl:for-each select="//stop">
<xsl:value-of select="concat(',',@refId,'
')"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>