【问题标题】:Write XSL template for a nested node为嵌套节点编写 XSL 模板
【发布时间】:2018-10-17 22:37:29
【问题描述】:

我有以下 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>

【问题讨论】:

  • 到目前为止你尝试了什么?

标签: xml xslt


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2012-02-20
    • 1970-01-01
    • 1970-01-01
    • 2014-11-13
    • 2014-03-18
    • 1970-01-01
    • 2010-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多