从非上下文元素中提取属性值

时间:2015-11-20 12:41:33

标签: xml xslt xpath

我想要的是从属性中获取价值。

这是输入xml:



SelectSingleNode("./span")
SelectSingleNode("span")




这是样式表:



<?xml version="1.0" encoding="UTF-8"?>

<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rpt="http://openoffice.org/2005/report" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:grddl="http://www.w3.org/2003/g/data-view#" xmlns:officeooo="http://openoffice.org/2009/office" xmlns:tableooo="http://openoffice.org/2009/table" xmlns:drawooo="http://openoffice.org/2010/draw" xmlns:calcext="urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0" xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0" xmlns:css3t="http://www.w3.org/TR/css3-text/" office:version="1.2">
	<office:scripts/>
	<office:font-face-decls>
		<style:font-face style:name="Mangal1" svg:font-family="Mangal"/>
		<style:font-face style:name="Calibri" svg:font-family="Calibri" style:font-family-generic="roman" style:font-pitch="variable"/>
		<style:font-face style:name="Calibri Light" svg:font-family="&apos;Calibri Light&apos;" style:font-family-generic="roman" style:font-pitch="variable"/>
		<style:font-face style:name="Cambria" svg:font-family="Cambria" style:font-family-generic="roman" style:font-pitch="variable"/>
		<style:font-face style:name="DeusEx" svg:font-family="DeusEx" style:font-family-generic="roman" style:font-pitch="variable"/>
		<style:font-face style:name="Liberation Serif" svg:font-family="&apos;Liberation Serif&apos;" style:font-family-generic="roman" style:font-pitch="variable"/>
		<style:font-face style:name="Liberation Sans" svg:font-family="&apos;Liberation Sans&apos;" style:font-family-generic="swiss" style:font-pitch="variable"/>
		<style:font-face style:name="F" svg:font-family="" style:font-family-generic="system" style:font-pitch="variable"/>
		<style:font-face style:name="Arial" svg:font-family="Arial" style:font-family-generic="system" style:font-pitch="variable"/>
		<style:font-face style:name="Mangal" svg:font-family="Mangal" style:font-family-generic="system" style:font-pitch="variable"/>
		<style:font-face style:name="Microsoft YaHei" svg:font-family="&apos;Microsoft YaHei&apos;" style:font-family-generic="system" style:font-pitch="variable"/>
		<style:font-face style:name="SimSun" svg:font-family="SimSun" style:font-family-generic="system" style:font-pitch="variable"/>
		<style:font-face style:name="Times New Roman" svg:font-family="&apos;Times New Roman&apos;" style:font-family-generic="system" style:font-pitch="variable"/>
	</office:font-face-decls>
	<office:automatic-styles>
		<style:style style:name="P1" style:family="paragraph" style:parent-style-name="Standard">
			<style:text-properties fo:font-size="5pt" fo:language="bg" fo:country="BG" officeooo:paragraph-rsid="00118747" style:font-size-asian="5pt" style:font-size-complex="5pt" style:language-complex="ar" style:country-complex="SA"/>
		</style:style>
		<style:style style:name="P2" style:family="paragraph" style:parent-style-name="Standard">
			<style:paragraph-properties fo:text-align="center" style:justify-single-word="false" style:writing-mode="lr-tb"/>
			<style:text-properties officeooo:paragraph-rsid="00118747"/>
		</style:style>
		<style:style style:name="P3" style:family="paragraph" style:parent-style-name="Standard">
			<style:paragraph-properties fo:text-align="center" style:justify-single-word="false" style:writing-mode="lr-tb"/>
			<style:text-properties fo:color="#000000" style:font-name="Cambria" fo:language="bg" fo:country="BG" officeooo:paragraph-rsid="00118747" style:font-name-complex="Arial"/>
		</style:style>
		<style:style style:name="P4" style:family="paragraph" style:parent-style-name="Standard">
			<style:paragraph-properties fo:text-align="center" style:justify-single-word="false" style:writing-mode="lr-tb"/>
			<style:text-properties fo:color="#000000" style:font-name="Cambria" officeooo:paragraph-rsid="00118747" style:font-name-complex="Arial"/>
		</style:style>
		<style:style style:name="P5" style:family="paragraph" style:parent-style-name="Standard">
			<style:paragraph-properties fo:text-align="start" style:justify-single-word="false" style:writing-mode="lr-tb"/>
			<style:text-properties officeooo:paragraph-rsid="00118747"/>
		</style:style>
		<style:style style:name="P6" style:family="paragraph" style:parent-style-name="Style2">
			<style:paragraph-properties fo:margin-top="0cm" fo:margin-bottom="0cm" style:contextual-spacing="false"/>
			<style:text-properties fo:language="bg" fo:country="BG" officeooo:paragraph-rsid="00118747"/>
		</style:style>
		<style:style style:name="T1" style:family="text">
			<style:text-properties fo:font-size="20pt" style:font-size-asian="20pt"/>
		</style:style>
		<style:style style:name="T2" style:family="text">
			<style:text-properties fo:font-size="20pt" fo:language="bg" fo:country="BG" style:font-size-asian="20pt"/>
		</style:style>
		<style:style style:name="T3" style:family="text">
			<style:text-properties fo:color="#b22f2a" style:font-name="Calibri" fo:language="bg" fo:country="BG"/>
		</style:style>
		<style:style style:name="T4" style:family="text">
			<style:text-properties fo:color="#000000" style:font-name="Cambria" fo:language="bg" fo:country="BG" style:font-name-complex="Arial"/>
		</style:style>
		<style:style style:name="T5" style:family="text">
			<style:text-properties fo:color="#000000" style:font-name="Cambria" fo:language="en" fo:country="US" style:font-name-complex="Arial"/>
		</style:style>
		<style:style style:name="T6" style:family="text">
			<style:text-properties fo:color="#000000" style:font-name="Cambria" style:font-name-complex="Arial"/>
		</style:style>
	</office:automatic-styles>
	<office:body>
		<office:text>
			<text:sequence-decls>
				<text:sequence-decl text:display-outline-level="0" text:name="Illustration"/>
				<text:sequence-decl text:display-outline-level="0" text:name="Table"/>
				<text:sequence-decl text:display-outline-level="0" text:name="Text"/>
				<text:sequence-decl text:display-outline-level="0" text:name="Drawing"/>
			</text:sequence-decls>
			<text:p text:style-name="P6">
				<text:span text:style-name="T1"/>
			</text:p>
			<text:p text:style-name="Title">
				<text:span text:style-name="T4">Запознаване със</text:span>
				<text:span text:style-name="T5">JTAG</text:span>
				<text:span text:style-name="T4">протокол за програмиране на</text:span>
				<text:span text:style-name="T5">MSP430</text:span>
				<text:span text:style-name="T4">Запознаване с функционалността на</text:span>
				<text:span text:style-name="T5">Real Time Clock.</text:span>
			</text:p>
			<text:p text:style-name="P5">
				<text:span text:style-name="T5"/>
			</text:p>
			<text:p text:style-name="Subtitle">
				<text:span text:style-name="T4">Създаване на</text:span>
				<text:span text:style-name="Bold_20_Style">
					<text:span text:style-name="T4">часовник</text:span>
				</text:span>
				<text:span text:style-name="T4">с календар в реално време.</text:span>
			</text:p>
			<text:p text:style-name="P5">
				<text:span text:style-name="T4"/>
			</text:p>
			<text:h text:style-name="Heading_20_1" text:outline-level="1">
				<text:span text:style-name="T4">Запознаване с</text:span>
				<text:span text:style-name="T5">UART</text:span>
				<text:span text:style-name="T4">периферията на</text:span>
				<text:span text:style-name="T5">MSP430F5438.</text:span>
			</text:h>
			<text:p text:style-name="P5">
				<text:span text:style-name="T5"/>
			</text:p>
			<text:h text:style-name="Heading_20_2" text:outline-level="2">
				<text:span text:style-name="T4">Създаване на график за аларма и реализиране на прекъсвания по зададени час и дата.</text:span>
			</text:h>
		</office:text>
	</office:body>
</office:document-content>
&#13;
&#13;
&#13;

这就是我得到的:

&#13;
&#13;
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" version="1.0">
	<xsl:output method="xml"/>
	<!-- -->
	<xsl:template name="return-lang">
		<xsl:param name="style-name"/>
		<xsl:value-of select="/office:document-content//style:style[@style:name='$style-name']/style:text-properties/@fo:language"/>
	</xsl:template>
	<!-- -->
	<xsl:template name="extract-title">
		<xsl:for-each select="office:body/office:text/text:p[@text:style-name='Title']">
			<para style-name="Title">
				<!--Title is a constant sinse it is a precondition-->
				<xsl:for-each select="text:span">
					<xsl:variable name="language">
						<xsl:call-template name="return-lang">
							<xsl:with-param name="style-name" select="string(@text:style-name)"/>
						</xsl:call-template>
					</xsl:variable>
					<span xml:lan="{$language}">
						<xsl:value-of select="."/>
					</span>
				</xsl:for-each>
			</para>
		</xsl:for-each>
	</xsl:template>
	<!-- -->
	<xsl:template match="/office:document-content">
		<text>
			<xsl:call-template name="extract-title"/>
		</text>
	</xsl:template>
	<!-- -->
	<xsl:template match="text:sequence-decls">
		<ofis>
			<xsl:call-template name="extract-title"/>
		</ofis>
	</xsl:template>
	<!--	<xsl:template match="@*|node()">
		<xsl:copy>
			<xsl:apply-templates select="@*|node()"/>
		</xsl:copy>
	</xsl:template>                      /office:document-content//style:style[@style:name='{$style-name}']/@style:family-->
</xsl:stylesheet>
&#13;
&#13;
&#13;

这就是我的期望:

&#13;
&#13;
<text xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0">
    <para style-name="Title">
        <span xml:lan="">Запознаване със</span>
        <span xml:lan="">JTAG</span>
        <span xml:lan="">протокол за програмиране на</span>
        <span xml:lan="">MSP430</span>
        <span xml:lan="">Запознаване с функционалността на</span>
        <span xml:lan="">Real Time Clock.</span>
    </para>
</text>
&#13;
&#13;
&#13; 在我看来,扩展<text> <para style-name="Title"> <span xml:lang="bg">Запознаване със</span> <span xml:lang="en">JTAG</span> <span xml:lang="bg">протокол за програмиране на</span> <span xml:lang="en">MSP430</span> <span xml:lang="bg">Запознаване с функционалността на</span> <span xml:lang="en">Real Time Clock.</span> </para> </text>参数时必定存在一些问题。 当我用style-name替换[@style:name='$style-name']时,我会获得相应[@style:name='T4']属性的值。请原谅我糟糕的英语。 请帮忙。

1 个答案:

答案 0 :(得分:1)

@style:name='$style-name'更改为@style:name=$style-name