Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.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
Html XSL重复相同的XML元素/节点,工作不正常_Html_Xml_Xslt_Xpath_Stylesheet - Fatal编程技术网

Html XSL重复相同的XML元素/节点,工作不正常

Html XSL重复相同的XML元素/节点,工作不正常,html,xml,xslt,xpath,stylesheet,Html,Xml,Xslt,Xpath,Stylesheet,这就是我的XML的样子。这非常简单,只需列出一组指向其他XML文件的链接,每个链接都有不同的名称/标题,当然: <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="index.xsl"?> <playToc> <play>a_and_c.xml</play> <play>all_well.xml</p

这就是我的XML的样子。这非常简单,只需列出一组指向其他XML文件的链接,每个链接都有不同的名称/标题,当然:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="index.xsl"?>
<playToc>
    <play>a_and_c.xml</play>
    <play>all_well.xml</play>
    <play>as_you.xml</play>
    <play>com_err.xml</play>
    <play>coriolan.xml</play>
    <play>cymbelin.xml</play>
    <name>Title 1</name>
    <name>Title 2</name>
    <name>Title 3</name>
    <name>Title 4</name>
    <name>Title 5</name>
    <name>Title 6</name>
</playToc>
Title 1
Title 2
Title 3
Title 4
Title 5
Title 6
当我想把它作为输出时,当然:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="index.xsl"?>
<playToc>
    <play>a_and_c.xml</play>
    <play>all_well.xml</play>
    <play>as_you.xml</play>
    <play>com_err.xml</play>
    <play>coriolan.xml</play>
    <play>cymbelin.xml</play>
    <name>Title 1</name>
    <name>Title 2</name>
    <name>Title 3</name>
    <name>Title 4</name>
    <name>Title 5</name>
    <name>Title 6</name>
</playToc>
Title 1
Title 2
Title 3
Title 4
Title 5
Title 6

任何帮助都太好了!非常感谢

XML输入的结构很差,但您可以通过

<xsl:template match="play">
  <xsl:variable name="pos" select="position()"/>
  <a href="{.}">
    <xsl:value-of select="../name[position() = $pos]"/>
  </a>
  <br/>
</xsl:template>


确保将
保留在另一个模板中,否则使用
position()
的方法将不起作用

应用于提供的源XML文档时

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="play">
   <xsl:variable name="vPos" select="position()"/>
     <a href="{.}">
      <xsl:value-of select="../name[$vPos]"/>
     </a><br />
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>
<playToc>
    <play>a_and_c.xml</play>
    <play>all_well.xml</play>
    <play>as_you.xml</play>
    <play>com_err.xml</play>
    <play>coriolan.xml</play>
    <play>cymbelin.xml</play>
    <name>Title 1</name>
    <name>Title 2</name>
    <name>Title 3</name>
    <name>Title 4</name>
    <name>Title 5</name>
    <name>Title 6</name>
</playToc>
<a href="a_and_c.xml">Title 1</a>
<br/>
<a href="all_well.xml">Title 2</a>
<br/>
<a href="as_you.xml">Title 3</a>
<br/>
<a href="com_err.xml">Title 4</a>
<br/>
<a href="coriolan.xml">Title 5</a>
<br/>
<a href="cymbelin.xml">Title 6</a>
<br/>

a_和_c.xml
all_well.xml
as_you.xml
com_err.xml
coriolan.xml
cymbelin.xml
标题1
标题2
标题3
标题4
标题5
标题6
生成所需的正确结果

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="play">
   <xsl:variable name="vPos" select="position()"/>
     <a href="{.}">
      <xsl:value-of select="../name[$vPos]"/>
     </a><br />
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>
<playToc>
    <play>a_and_c.xml</play>
    <play>all_well.xml</play>
    <play>as_you.xml</play>
    <play>com_err.xml</play>
    <play>coriolan.xml</play>
    <play>cymbelin.xml</play>
    <name>Title 1</name>
    <name>Title 2</name>
    <name>Title 3</name>
    <name>Title 4</name>
    <name>Title 5</name>
    <name>Title 6</name>
</playToc>
<a href="a_and_c.xml">Title 1</a>
<br/>
<a href="all_well.xml">Title 2</a>
<br/>
<a href="as_you.xml">Title 3</a>
<br/>
<a href="com_err.xml">Title 4</a>
<br/>
<a href="coriolan.xml">Title 5</a>
<br/>
<a href="cymbelin.xml">Title 6</a>
<br/>







注意事项

  • 根本不需要使用
    xsl:apply-templates

  • 只有一个模板


  • Martin Honnen,根本不需要使用
    xsl:apply templates
    。这只是我的XML的一个片段,所以我必须对我的一堆XML使用apply templates。@当然,您需要将此解决方案调整到您的真实代码中——希望这很容易。