Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取给定任意属性索引的XML元素的值 出身背景_Xml_Xslt_Indexing - Fatal编程技术网

获取给定任意属性索引的XML元素的值 出身背景

获取给定任意属性索引的XML元素的值 出身背景,xml,xslt,indexing,Xml,Xslt,Indexing,使用唯一标识符从XML元素列表中获取值 问题 给定以下格式的XML文档: <root xmlns:r="http://internet.com/network"> <r:equipment label="network"> <r:item id="1">computer</r:item> <r:item id="2">network cable</r:item> </r:equipment> <r:

使用唯一标识符从XML元素列表中获取值

问题 给定以下格式的XML文档:

<root xmlns:r="http://internet.com/network">
<r:equipment label="network">
  <r:item id="1">computer</r:item>
  <r:item id="2">network cable</r:item>
</r:equipment>
<r:equipment label="peripheral">
  <r:item id="3">printer</r:item>
  <r:item id="4">USB cable</r:item>
</r:equipment>

<r:install>
  <r:step action="identify"><r:item id="1" />, <r:item id="2" />, <r:item id="3" />, and <r:item id="4" /></r:step>
  <r:step action="unplug"><r:item id="2" /> from <r:item id="1" /></r:step>
  <r:step action="plug"><r:item id="4" /> into <r:item id="3" /></r:step>
  <r:step action="plug"><r:item id="4" /> into <r:item id="1" /></r:step>
</r:install>
</root>
当在XML文档中的任何位置应用一个模板(作为对所述值的引用)时,获取USB电缆值的相应select XPath表达式是什么


谢谢大家!

这是使用键从r:equipment元素中查找r:item值的理想情况

<xsl:key name="equipment" match="r:equipment/r:item" use="@id" />
因此,您可以使用@id属性查找r:equipment/r:item元素。要使用此键,假设您位于一个r:step/r:item元素上,您只需执行以下操作

<xsl:value-of select="key('equipment', @id)" />
这是完整的XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:r="http://internet.com/network" exclude-result-prefixes="r">
   <xsl:output method="html" indent="yes"/>

   <xsl:key name="equipment" match="r:equipment/r:item" use="@id" />

   <xsl:template match="/root">
      <xsl:apply-templates select="r:install"/>
   </xsl:template>

   <xsl:template match="r:install">
      <p> Installation </p>
      <ol>
         <xsl:apply-templates select="r:step" />
      </ol>
   </xsl:template>

   <xsl:template match="r:step">
      <li>
         <xsl:value-of select="concat(@action, ' ')" />
         <xsl:apply-templates select="node()" />
      </li>
   </xsl:template>

   <xsl:template match="r:item"> 
      <xsl:value-of select="key('equipment', @id)" />
   </xsl:template>
</xsl:stylesheet>
当应用到源XML时,会生成以下内容

<p> Installation </p>
<ol>
<li>identify computer, 
            network cable, 
            printer, and 
            USB cable</li>
<li>unplug network cable from 
            computer</li>
<li>plug USB cable into 
            printer</li>
<li>plug USB cable into 
            computer</li>
</ol>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:r="http://internet.com/network" exclude-result-prefixes="r">
   <xsl:output method="html" indent="yes"/>

   <xsl:key name="equipment" match="r:equipment/r:item" use="@id" />

   <xsl:template match="/root">
      <xsl:apply-templates select="r:install"/>
   </xsl:template>

   <xsl:template match="r:install">
      <p> Installation </p>
      <ol>
         <xsl:apply-templates select="r:step" />
      </ol>
   </xsl:template>

   <xsl:template match="r:step">
      <li>
         <xsl:value-of select="concat(@action, ' ')" />
         <xsl:apply-templates select="node()" />
      </li>
   </xsl:template>

   <xsl:template match="r:item"> 
      <xsl:value-of select="key('equipment', @id)" />
   </xsl:template>
</xsl:stylesheet>
<p> Installation </p>
<ol>
<li>identify computer, 
            network cable, 
            printer, and 
            USB cable</li>
<li>unplug network cable from 
            computer</li>
<li>plug USB cable into 
            printer</li>
<li>plug USB cable into 
            computer</li>
</ol>