Using XSL to get all tags before a tag with given contents occurs
maybe somebody take me some advice how to correct this program. I should take from fields number before +
. Sometimes this field comes at the 17th line(field id 17), sometimes 14, 19 and etc...Others fields are also filled. Right now my program takes this number from field id 17. How to correct it?
Data example:
<document>
<line id="0">
<field id="0"><![CDATA[MAR5555]]></field>
<field id="1"><![CDATA[12314124141241]]></field>
<field id="2"><![CDATA[AAS]]></field>
<field id="3"><![CDATA[FOR12312]]></field>
<field id="4"/>
<field id="5"/>
<field id="6"/>
<field id="7"/>
<field id="8"/>
<field id="9"/>
<field id="10"/>
<field id="11"/>
<field id="12"/>
<field id="13"/>
<field id="14"/>
<field id="15"/>
<field id="16"/>
<field id="17"><![CDATA[0072972+1313113123123]]></field>
<field id="18"><![CDATA[5353]]></field>
<field id="19"><![CDATA[444444]]></field>
<field id="20"/>
<field id="21"/>
</line>
<line id="1">
<field id="0"><![CDATA[MAR6435]]></field>
<field id="1"><![CDATA[car123]]></field>
<field id="2"><![CDATA[sds]]></field>
<field id="3"><![CDATA[fest]]></field>
<field id="4"/>
<field id="5"/>
<field id="6"/>
<field id="7"/>
<field id="8"/>
<field id="9"/>
<field id="10"/>
<field id="11"/>
<field id="12"/>
<field id="13"/>
<field id="14"/>
<field id="15"><![CDATA[0000062+0dadasd]]></field>
<field id="16"><![CDATA[0d2]]></field>
<field id="17"><![CDATA[cccc]]></field>
<field id="18"/>
<field id="19"><![CDATA[000000]]></field>
<field id="20"/>
<field id="21"/>
</line>
<line id="2">
<field id="0"><![CDATA[MAR6435]]></field>
<field id="1"><![CDATA[sss]]></field>
<field id="2"><![CDATA[1231231]]></field>
<field id="3"><![CDATA[45123]]></field>
<field id="4"/>
<field id="5"/>
<field id="6"/>
<field id="7"/>
<field id="8"/>
<field id="9"/>
<field id="10"/>
<field id="11"/>
<field id="12"/>
<field id="13"/&g开发者_StackOverflowt;
<field id="14"/>
<field id="15"><![CDATA[0000062+0sdsd]]></field>
<field id="16"><![CDATA[0f2]]></field>
<field id="17"><![CDATA[843252dsd]]></field>
<field id="18"/>
<field id="19"><![CDATA[000000]]></field>
<field id="20"/>
<field id="21"/>
</line>
</document>
My program example:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:key name="kLine" match="line" use="substring-before(field[@id='17'],'+')"/>
<xsl:template match="/*">
<document>
<xsl:apply-templates select="line[contains(field[@id='0'], 'MAR')][count(. | key('kLine', substring-before(field[@id='17'],'+'))[1]) = 1]"/>
</document>
</xsl:template>
<xsl:template match="line">
<type-MAR>
<document>
<xsl:value-of select="substring-before(field[@id='17'],'+')"/>
</document>
</type-MAR>
</xsl:template>
</xsl:stylesheet>
Result:
<type-MAR>
<document>
0072972
</document>
<document>
0000062
</document>
</type-MAR>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:key name="kLine" match="line" use="
substring-before(
field[contains(., '+')]
,'+'
)"/>
<xsl:template match="/*">
<document>
<xsl:apply-templates select="
line
[contains(field[@id='0'], 'MAR')]
[count(
. | key(
'kLine',
substring-before(
field[contains(., '+')]
,'+')
)
[1]
) = 1
]"/>
</document>
</xsl:template>
<xsl:template match="line">
<type-MAR>
<document>
<xsl:value-of select="
substring-before(
field[contains(., '+')],
'+'
)
"/>
</document>
</type-MAR>
</xsl:template>
</xsl:stylesheet>
Well-formed XML output against your sample is:
<document>
<type-MAR>
<document>0072972</document>
</type-MAR>
<type-MAR>
<document>0000062</document>
</type-MAR>
</document>
精彩评论