Xml 每个问题的XSLT

Xml 每个问题的XSLT,xml,xslt,Xml,Xslt,我正在寻找一种将此代码输出为html的方法 <legal> <heading>Poo</heading> <g>fart <a href="http://www.bclaws.ca/EPLibraries/bclaws_new/document/ID/freeside/96165_01">Freedom of Information and Protection of Privacy Act</a> (RSBC 19

我正在寻找一种将此代码输出为html的方法

<legal>
  <heading>Poo</heading>
  <g>fart <a href="http://www.bclaws.ca/EPLibraries/bclaws_new/document/ID/freeside/96165_01">Freedom of Information and Protection of Privacy Act</a> (RSBC 1996 ch. 165):</g>
  <foo>
    <bar>a </bar>
    <bar>b </bar>
    <bar>c </bar>
    <bar>d </bar>
    <bar>e </bar>
    <bar>f </bar>
  </foo>
  <g> faf </g>
  <g> faf </g>
  <g> faf </g>
  <g> faf </g>
  <g> faf </g>
  <g> faf </g>
  <g> faf </g>
    <foo>
      <bar> a </bar>
      <bar> b </bar>
      <bar> c </bar>
    </foo>
  <g> asfd </g>
  <g> asfd </g>
  <g> asfd </g>
  <g> asfd </g>
  <g> asfd </g>
  <g> asfd </g>
  <g> asfd </g>
    <foo>
      <bar> a </bar>
      <bar> b </bar>
      <bar> c </bar>
    </foo>
</legal>

你想要什么

<xsl:variable name="legal" select="document('legal.xml')/legal"/>
<xsl:for-each select="$legal/heading">
<h3><xsl:value-of select="."/></h3>
<xsl:for-each select="../g"><p><xsl:value-of select="."/></p></xsl:for-each>
  <ul>
    <xsl:for-each select="$legal/foo/bar">
      <li><xsl:value-of select="."/></li>
    </xsl:for-each>
  </ul>
</xsl:for-each>


我建议将您显示的整个代码片段替换为以下内容:

<xsl:apply-templates select="document('legal.xml')/legal" />

并添加以下模板:

<xsl:template match="heading">
  <h3><xsl:apply-templates /></h3>
</xsl:template>

<xsl:template match="g">
  <p><xsl:apply-templates /></p>
</xsl:template>

<xsl:template match="foo">
  <ul>
    <xsl:apply-templates />
  </ul>
</xsl:template>

<xsl:template match="bar">
  <li><xsl:apply-templates /></li>
</xsl:template>


  • 此时获得重复的原因是,您迭代每个头,然后在该循环的每个迭代中,处理相同的
    g
    元素,并迭代相同的
    foo
    元素,而不管它们在哪个头之后。在您的源代码和html输出中,
    g
    /
    p
    元素不是
    header
    /
    h3
    元素的子元素,在您的代码中这样对待它们是没有意义的。

    实际上,他并没有迭代所有
    g
    元素。。。他正在输出第一个
    g
    元素的字符串值(这是
    legal
    的子元素)<代码>的值仅获取其select中的第一个节点。假设XSLT1.0.Ah是正确的,但它仍在迭代所有
    foo
    <xsl:template match="heading">
      <h3><xsl:apply-templates /></h3>
    </xsl:template>
    
    <xsl:template match="g">
      <p><xsl:apply-templates /></p>
    </xsl:template>
    
    <xsl:template match="foo">
      <ul>
        <xsl:apply-templates />
      </ul>
    </xsl:template>
    
    <xsl:template match="bar">
      <li><xsl:apply-templates /></li>
    </xsl:template>