为嵌套节点编写XSL模板

时间:2018-10-17 22:37:30

标签: xml xslt

我具有以下XML结构,这是大XML的简化版本,我需要编写XSL模板以从特定节点(维度)获取数据。

我已经有一个XSL来进行转换。新增了一个节点<Dimensions>,它可以在一个节点内多次出现,如下所示。我需要从general/electronics/tv/description/Dimensions中读取数据,并将其内容(div标签内容)输出到结果HTML输出中。 <Dimensions>也可以出现在software/version/description/Dimensions节点或任何其他节点下。

所以我的问题是:
有没有一种通用的方式来编写模板,该模板可以读取<Dimensions>标记的内容并输出其内容?在为此标签编写XSL模板时,我需要一些帮助。

<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
  <cd>
    <title>Greatest Hits</title>
    <artist>Dolly Parton</artist>
    <country>USA</country>
    <company>RCA</company>
    <price>9.90</price>
    <year>1982</year>
  </cd>
  <DVD>
    <title>Still got the blues</title>
    <artist>Gary Moore</artist>
    <country>UK</country>
    <company>Virgin records</company>
    <price>10.20</price>
    <year>1990</year>
  </DVD>
  <DVD>
    <title>Eros</title>
    <artist>Eros Ramazzotti</artist>
    <country>EU</country>
    <company>BMG</company>
    <price>9.90</price>
    <year>1997</year>
  </DVD>
  <general>
  	<electronics>
  		<tv>
  			<name>samsung</name>
  			<model>2.1</model>
  		</tv>
  		<tv>
  			<name>vizio</name>
  			<model>2.1</model>
  			<description>
  				<span>PurColor: Enjoy millions of shades of color, fine tuned to create an incredibly vibrant picture</span>
  				<Dimensions> <div>3.6 width</div></Dimensions>
  				<span>Enjoy subtle details hidden in the shadows of color and darkness with precise lighting that reveals a lifelike picture.</span>
  				<Dimensions><div>4.8 height<div></Dimensions>
  			</description>
  		</tv>
  	</electronics>
  </general>
  <software>
  	<version>
    	<title>Sylvias Mother</title>
    	<artist>Dr.Hook</artist>
    	<country>UK</country>
    	<company>CBS</company>
    	<price>8.10</price>
    	<year>1973</year>
    	<description>
  			<span>Electric water kettle with 1.0-liter capacity and 1500 watts of power for fast results (120V ~60Hz)</span>
  			<Dimensions> <div>5.6 width</div></Dimensions>
  			<span>cordless design allows for easy filling and serving; power base with 30-inch power cord for flexible placement, plus cord wrap for compact storage.</span>
  			<Dimensions><div>5.8 height</div></Dimensions>
  		</description>
    <version>
  </software>
 
</catalog>

1 个答案:

答案 0 :(得分:0)

您可以使用XPath表达式检索所需元素的节点

/catalog/general/electronics/tv/description/Dimensions