【问题标题】:can't get some xml elements in <xsl:otherwise> block using xslt?无法使用 xslt 在 <xsl:otherwise> 块中获取一些 xml 元素?
【发布时间】:2012-06-22 06:17:59
【问题描述】:

我在块中获取了我需要的 xml 元素。但同时,我也放置了块来捕获其他 xml 元素。但这对我不起作用...

这是 XML 文档

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:body>
    <w:p>
        <w:pPr>
        </w:pPr>
        <w:r>
            <w:t>Text1-</w:t>
        </w:r>  
    </w:p>

    <w:p>
        <w:pPr>
            <w:pStyle w:val="Heading1" /> 
        </w:pPr>
        <w:r>
            <w:t>Text2-</w:t>
        </w:r>  
    </w:p>

    <w:p>
        <w:pPr>
        </w:pPr>
        <w:r>
            <w:t>Text3-</w:t>
        </w:r>  
    </w:p>

    <w:p>
        <w:pPr>
        </w:pPr>
        <w:r>
            <w:t>Text4-</w:t>
        </w:r>  
    </w:p>

    <w:p>
        <w:pPr>
            <w:pStyle w:val="Heading1" /> 
        </w:pPr>
        <w:r>
            <w:t>Text5-</w:t>
        </w:r>  
    </w:p>

</w:body>
</w:document>

这是 XSLT 文件

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
 exclude-result-prefixes="w">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <Document>

      <xsl:variable name="headingName" select="(//w:body/w:p/w:pPr/w:pStyle[starts-with(@w:val, 'Heading')])[1]/@w:val"/>
      <xsl:variable name="topLevelHeadings" select = "//w:body/w:p[w:pPr/w:pStyle/@w:val = $headingName]"/>

      <xsl:choose>

        <xsl:when test="$topLevelHeadings">
          <Heading>
          <xsl:apply-templates select="$topLevelHeadings">
          </xsl:apply-templates>
          </Heading>
        </xsl:when>

        <xsl:otherwise>
             <xsl:apply-templates select="w:p">
             </xsl:apply-templates>
        </xsl:otherwise>

      </xsl:choose>   

    </Document>
  </xsl:template>  

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

  <xsl:template match="w:r/w:t">
    <xsl:value-of select="." />
  </xsl:template>

</xsl:stylesheet>

我生成的输出是:

<Document>
  <Heading>
  <Paragraph>Text2-</Paragraph> 
  <Paragraph>Text5-</Paragraph> 
  </Heading>
</Document>

但我需要的输出是:

<Document>
      <Paragraph>Text1-</Paragraph> 
      <Heading>
           <Paragraph>Text2-</Paragraph> 
      </Heading>
      <Paragraph>Text3-</Paragraph> 
      <Paragraph>Text4-</Paragraph> 
      <Heading>
           <Paragraph>Text5-</Paragraph> 
      </Heading>
</Document>

我想,我的 Block 有点问题。所以,请指导我摆脱这个问题......

【问题讨论】:

    标签: xml xslt xpath xslt-2.0


    【解决方案1】:

    您的 xsl:choose 的问题在于它只使用一次,并且仅检查是否至少有一个 w:p 元素具有一个标题。因此,您只会得到一个 Heading 元素输出。为了让您 xsl:choose 工作,您确实需要在 w:p 模板中使用它

    <xsl:template match="w:p">
       <xsl:choose>
          <xsl:when test="w:pPr/w:pStyle[starts-with(@w:val, 'Heading')]">
             <Heading>
                <Paragraph>
                   <xsl:apply-templates/>
                </Paragraph>
             </Heading>
          </xsl:when>
          <xsl:otherwise>
             <Paragraph>
                <xsl:apply-templates/>
             </Paragraph>
          </xsl:otherwise>
       </xsl:choose>
    </xsl:template>
    

    但是,您实际上并不需要 xsl:choose。您可能只需要一个特定的模板来匹配具有匹配 w:pPr 元素的 w:p 元素

    <xsl:template match="w:p[w:pPr/w:pStyle[starts-with(@w:val, 'Heading')]]">
    

    然后您可以在此模板中输出 Heading 元素。然后,您将有一个单独的模板来匹配所有其他 w:p 元素以输出段落,如果您给它一个名称,您也可以从以前的模板中调用它来共享代码

    <xsl:template match="w:p" name="para"> 
    

    这是完整的 XSLT

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" exclude-result-prefixes="w">
       <xsl:output method="xml" indent="yes"/>
    
       <xsl:template match="/">
          <Document>
             <xsl:apply-templates/>
          </Document>
       </xsl:template>
    
       <xsl:template match="w:p[w:pPr/w:pStyle[starts-with(@w:val, 'Heading')]]">
          <Heading>
             <xsl:call-template name="para"/>
          </Heading>
       </xsl:template>
    
       <xsl:template match="w:p" name="para">
          <Paragraph>
             <xsl:apply-templates/>
          </Paragraph>
       </xsl:template>
    
       <xsl:template match="w:r/w:t">
          <xsl:value-of select="."/>
       </xsl:template>
    </xsl:stylesheet>
    

    当应用于您的输入 XML 时,以下是输出

    <Document>
       <Paragraph>Text1-</Paragraph>
       <Heading>
          <Paragraph>Text2-</Paragraph>
       </Heading>
       <Paragraph>Text3-</Paragraph>
       <Paragraph>Text4-</Paragraph>
       <Heading>
          <Paragraph>Text5-</Paragraph>
       </Heading>
    </Document>
    

    实际上,&lt;xsl:template match="w:r/w:t"&gt; 的最终模板并不是严格需要的,因为 XSLT 在匹配没有特定模板的元素时的默认行为是无论如何都会输出文本。

    【讨论】:

    • 谢谢蒂姆。我会检查一下。与此同时,你能告诉我我在这里使用的 有什么问题吗?我已经根据那个 块编写了很多代码。
    • 我已经扩展了我的答案,以解释为什么 xsl:choose 在您的情况下不起作用。通常最好使用模板匹配,因为它更符合 XSLT 作为功能语言的精神。它还有助于代码重用,并使内容更具可读性(例如,通过减少缩进和嵌套代码)。
    • 我可以使用这样的 not 语句
    • 如果你的意思是匹配没有 w:pStylew:p 元素,那么它会这样写:&lt;xsl:template match="w:p[not(w:pPr/w:pStyle[starts-with(@w:val, 'Heading')])]"&gt;跨度>
    【解决方案2】:

    作为错误的一个非常简单的说明,您的 xsl:otherwise 块执行&lt;xsl:apply-templates select="w:p"&gt;。当时的上下文节点是根节点(/),所以这是处理作为根节点子节点的 w:p 元素,并且没有这样的元素,所以什么也不做。

    您在这里处理一些非常复杂的 XML,而 XSLT 方面的经验很少。最好先解决一些更简单的问题 - 花一些时间阅读 XSLT 教科书或教程,然后研究示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-23
      • 1970-01-01
      • 2021-12-25
      • 2016-05-16
      • 1970-01-01
      • 1970-01-01
      • 2017-10-05
      相关资源
      最近更新 更多