XSLT 忽略模板中的元素?

发布于 2024-10-20 21:21:46 字数 1359 浏览 5 评论 0原文

我有以下 XSLT:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:html="http://www.w3.org/TR/REC-html40"
      xmlns:fo="http://www.w3.org/1999/XSL/Format"  
      xmlns:o="urn:schemas-microsoft-com:office:office"     
      xmlns:x="urn:schemas-microsoft-com:office:excel"  
      xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
    <xsl:output method="xml" indent="yes" />
        <xsl:strip-space elements="*" />

        <xsl:template match="ss:Workbook/o:DocumentProperties/o:*"/>
        <xsl:template match="ss:Workbook/x:ExcelWorkbook/x:*"/>
        <xsl:template match="ss:Workbook/x:ExcelWorkbook/x:*"/>
        <xsl:template match="ss:Workbook/ss:Worksheet/x:WorksheetOptions/x:*"/>
        <xsl:template match="ss:Workbook/ss:DocumentProperties/ss:*"/>

        <xsl:template match='ss:Workbook/ss:Worksheet/ss:Table'>
        <grade-dist>
            <xsl:apply-templates select='ss:Workbook/ss:Worksheet/ss:Table'/>
        </grade-dist>
        </xsl:template> 

        <xsl:template match='ss:Workbook/ss:Worksheet/ss:Table'>
....

我的 XML 输出正常,但我没有: 在其中,似乎它完全忽略了两者,知道为什么吗?

谢谢,

I have the following XSLT:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:html="http://www.w3.org/TR/REC-html40"
      xmlns:fo="http://www.w3.org/1999/XSL/Format"  
      xmlns:o="urn:schemas-microsoft-com:office:office"     
      xmlns:x="urn:schemas-microsoft-com:office:excel"  
      xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
    <xsl:output method="xml" indent="yes" />
        <xsl:strip-space elements="*" />

        <xsl:template match="ss:Workbook/o:DocumentProperties/o:*"/>
        <xsl:template match="ss:Workbook/x:ExcelWorkbook/x:*"/>
        <xsl:template match="ss:Workbook/x:ExcelWorkbook/x:*"/>
        <xsl:template match="ss:Workbook/ss:Worksheet/x:WorksheetOptions/x:*"/>
        <xsl:template match="ss:Workbook/ss:DocumentProperties/ss:*"/>

        <xsl:template match='ss:Workbook/ss:Worksheet/ss:Table'>
        <grade-dist>
            <xsl:apply-templates select='ss:Workbook/ss:Worksheet/ss:Table'/>
        </grade-dist>
        </xsl:template> 

        <xsl:template match='ss:Workbook/ss:Worksheet/ss:Table'>
....

My XML outputs fine, but I dont have the: <grade-dist> and </grade-dist>
in it, seems like it ignores both completely, any idea why?

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冧九 2024-10-27 21:21:46

您有两个具有完全相同匹配模式的模板: 'ss:Workbook/ss:Worksheet/ss:Table'

根据 XSLT 规范,这是一个可恢复的错误,此处观察到的恢复是模板最后才被选中。

另一个观察结果是,指令

<xsl:apply-templates select='ss:Workbook/ss:Worksheet/ss:Table'/> 

在匹配 'sss:Workbook/ss:Worksheet/ss:Table' 的模板内,很可能是错误的 -- 不太可能有 'ss:Workbook/ss:Worksheet/ss:Table' 元素具有 'ss:Workbook/ss:Worksheet/ss:Table'< /code> 曾祖父母。

我认为你想要的是这样的

<xsl:template match='ss:Workbook/ss:Worksheet/ss:Table'>
    <grade-dist>
        <xsl:apply-templates select="." mode="pr2"/>
    </grade-dist>
</xsl:template>

<xsl:template mode="pr2" match='ss:Workbook/ss:Worksheet/ss:Table'>
 <!-- Some necessary processing -->
</xsl:template>

或者只是

<xsl:template match='ss:Workbook/ss:Worksheet/ss:Table'>
    <grade-dist>
      <!-- Do the processing here -->
    </grade-dist>
</xsl:template>

You have two templates with exactly the same match pattern: 'ss:Workbook/ss:Worksheet/ss:Table'

According to the XSLT spec this is a recoverable error and the recovery observed here is that the template that comes last get chosen.

Another observation is that the instruction:

<xsl:apply-templates select='ss:Workbook/ss:Worksheet/ss:Table'/> 

inside the template matching 'ss:Workbook/ss:Worksheet/ss:Table', is most probably wrong -- it is unlikely that there would be 'ss:Workbook/ss:Worksheet/ss:Table' elements that have a 'ss:Workbook/ss:Worksheet/ss:Table' grand-grand-parent.

I think what you want is something like this:

<xsl:template match='ss:Workbook/ss:Worksheet/ss:Table'>
    <grade-dist>
        <xsl:apply-templates select="." mode="pr2"/>
    </grade-dist>
</xsl:template>

<xsl:template mode="pr2" match='ss:Workbook/ss:Worksheet/ss:Table'>
 <!-- Some necessary processing -->
</xsl:template>

or just:

<xsl:template match='ss:Workbook/ss:Worksheet/ss:Table'>
    <grade-dist>
      <!-- Do the processing here -->
    </grade-dist>
</xsl:template>
七禾 2024-10-27 21:21:46

看起来好像您有两个匹配条件相同的模板(即没有模式或优先级属性来区分它们)。如果处理器绕过第一个并处理第二个,因为它“获胜”,那么这将产生您正在观察的效果(因为我假设等级距离内应用模板的目的是调用第二个模板) 。

It looks as if you have two templates whose match criteria are identical (i.e., no mode or priority attributes to differentiate them). If the processor is bypassing the first one and processing the second because it "wins," then this would have the effect you are observing (since I presume the purpose of the apply-templates inside the grade-dist is to invoke the second template).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文