在模式中使用'current()'函数将自定义模板应用于节点

马思

我正在实现一个系统,用户可以在Items集合上生成文档一个项目包含一个Name和一个ItemTypeId用户可以选择要应用于特定类型的项目的模板。这存储在一个组合对象中,该对象包含itemTypeId到TemplateId的映射。

因此,用于生成文档的XML数据如下所示:

XML:

<?xml version="1.0" encoding="UTF-8"?>
<data>
  <composition>
    <ItemType>
      <ItemTypeId>IT1</ItemTypeId>
      <TemplateId>T1</TemplateId>
    </ItemType>
    <ItemType>
      <ItemTypeId>IT2</ItemTypeId>
      <TemplateId>T3</TemplateId>
    </ItemType>
    <ItemType>
      <ItemTypeId>IT3</ItemTypeId>
      <TemplateId>T2</TemplateId>
    </ItemType>
    <ItemType>
      <ItemTypeId>IT4</ItemTypeId>
      <TemplateId>T1</TemplateId>
    </ItemType>
  </composition>
  <items>
    <item>
      <ItemTypeId>IT1</ItemTypeId>
      <Name>A</Name>
    </item>
    <item>
      <ItemTypeId>IT2</ItemTypeId>
      <Name>B</Name>
    </item>
    <item>
      <ItemTypeId>IT3</ItemTypeId>
      <Name>C</Name>
    </item>
    <item>
      <ItemTypeId>IT2</ItemTypeId>
      <Name>D</Name>
    </item>
  </items>
</data>

要生成文档,我想使用包含4个模板的XSLT。

XSLT:

<xsl:template match="/">
  <html>
  <body>
  <h2>Document</h2>
<xsl:for-each select="/data/items/item">
<xsl:variable name="itemTypeId" select="current()/ItemTypeId" />
  <xsl:apply-templates select="current()"/>
</xsl:for-each>
  </body>
</html>
</xsl:template>

<xsl:template match="item[/data/composition/ItemType[ItemTypeId = current()/ItemTypeId]/TemplateId='T1']">
 <h1>T1</h1>
 Item <xsl:value-of select="current()/Name"/> 
 with type <xsl:value-of select="current()/ItemTypeId"/><br/>
</xsl:template>

<xsl:template match="item[/data/composition/ItemType[ItemTypeId = current()/ItemTypeId]/TemplateId='T2']">
 <h1>T2</h1>
 Item <xsl:value-of select="current()/Name"/> 
 with type <xsl:value-of select="current()/ItemTypeId"/><br/>
</xsl:template>

<xsl:template match="item[/data/composition/ItemType[ItemTypeId = current()/ItemTypeId]/TemplateId='T3']">
 <h1>T3</h1>
 Item <xsl:value-of select="current()/Name"/> 
 with type <xsl:value-of select="current()/ItemTypeId"/><br/>
</xsl:template>

<xsl:template match="item[/data/composition/ItemType[ItemTypeId = current()/ItemTypeId]/TemplateId='T4']">
 <h1>T4</h1>
 Item <xsl:value-of select="current()/Name"/> 
 with type <xsl:value-of select="current()/ItemTypeId"/><br/>
</xsl:template>

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

我在匹配模式中使用current()函数查看合成,并查看是否需要应用模板。这可以在W3Schools的在线XSLT编辑器中完美运行,并生成以下文档:

输出(在W3Schools xslt编辑器上):

文档

T1

项目A,类型为IT1

T3

项目B,类型为IT2

T2

项目C,类型为IT3

T3

项目D,类型为IT2

但是,当我尝试在.Net应用程序中实现此功能时,出现以下错误:

'current()'函数不能在模式中使用。

在我看来,.NET不允许在匹配模式中使用current()有人知道解决这个问题的方法吗?还是有人可以指出我的逻辑缺陷?

编辑我使用System.Xml.Xsl中的标准.NET方法应用xslt,如下所示:

  XElement data = XElement.Parse("xml is entered here");
  XElement stylesheet = XElement.Parse("xslt is entered here");
  XslCompiledTransform compiledTransform = new XslCompiledTransform();
  compiledTransform.Load(stylesheet.CreateReader());
  // Create an xml writer to write to the result stream.
  using (XmlWriter writer = XmlWriter.Create(result, compiledTransform.OutputSettings))
  {
      // Apply the transformation.
      compiledTransform.Transform(data.CreateReader(), null, writer);
  }
  // Return the result.
  return result;
马丁·霍恩(Martin Honnen)

您可以使用一个键:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:key name="ref" match="data/composition/ItemType" use="ItemTypeId"/>

    <xsl:template match="/">
        <html>
            <body>
                <h2>Document</h2>

                    <xsl:apply-templates select="/data/items/item"/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="item[key('ref', ItemTypeId)/TemplateId='T1']">
        <h1>T1</h1>
        Item <xsl:value-of select="current()/Name"/> 
        with type <xsl:value-of select="current()/ItemTypeId"/><br/>
    </xsl:template>

    <xsl:template match="item[key('ref', ItemTypeId)/TemplateId='T2']">
        <h1>T2</h1>
        Item <xsl:value-of select="current()/Name"/> 
        with type <xsl:value-of select="current()/ItemTypeId"/><br/>
    </xsl:template>

    <xsl:template match="item[key('ref', ItemTypeId)/TemplateId='T3']">
        <h1>T3</h1>
        Item <xsl:value-of select="current()/Name"/> 
        with type <xsl:value-of select="current()/ItemTypeId"/><br/>
    </xsl:template>

    <xsl:template match="item[key('ref', ItemTypeId)/TemplateId='T4']">
        <h1>T4</h1>
        Item <xsl:value-of select="current()/Name"/> 
        with type <xsl:value-of select="current()/ItemTypeId"/><br/>
    </xsl:template>

    <xsl:template match="item">
    </xsl:template>
</xsl:stylesheet>

看起来好像这四个模板可以更好地简化为单个模板

    <xsl:template match="item[key('ref', ItemTypeId)]">
        <h1><xsl:value-of select="key('ref', ItemTypeId)/TemplateId"/></h1>
        Item <xsl:value-of select="Name"/> 
        with type <xsl:value-of select="ItemTypeId"/><br/>
    </xsl:template>

或者,您需要切换到XSLT 2.0处理器,例如Saxon 9或XmlPrime,其中current()支持在模式中使用XSLT 2.0处理器

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何使用bootstrap / css将自定义宽度应用于表格标题?

来自分类Dev

如果使用apply和mutate_at满足条件,则将自定义函数应用于选择列中的数据

来自分类Dev

使用dplyr :: mutate(across())将多列应用于自定义函数

来自分类Dev

我应该在自定义函数中使用回调模式吗?节点js

来自分类Dev

我应该在自定义函数中使用回调模式吗?节点js

来自分类Dev

将自定义函数应用于数据框

来自分类Dev

使用 jQuery addClass 或自定义 LoadCSS 函数将 css 应用于单个文本框

来自分类Dev

无法在React Native中使用useRef()将current.focus()应用于按钮

来自分类Dev

使用本机句柄将CSS应用于Angular指令(自定义或不自定义)

来自分类Dev

我可以使用Git将更新补丁应用于自定义软件吗?

来自分类Dev

JavaFX:使用自定义样式表将文本颜色应用于TableCell吗?

来自分类Dev

JavaFX:使用自定义样式表将文本颜色应用于TableCell吗?

来自分类Dev

使用 JQuery 中的自定义数据属性将 css 应用于 tr

来自分类Dev

将自定义主题应用于Odoo应用

来自分类Dev

将自定义类与自定义模板容器一起使用

来自分类Dev

如何使用辅助函数将自定义类名称添加到Handlebars模板中的元素?

来自分类Dev

可以在XSLT之外的XPath中使用current()函数吗?

来自分类Dev

在postgres函数中使用now或current_timestamp

来自分类Dev

将自定义样式应用于微调器

来自分类Dev

无法将自定义listSelector应用于ListView

来自分类Dev

将自定义功能应用于熊猫df

来自分类Dev

将自定义累积功能应用于熊猫

来自分类Dev

将自定义累积功能应用于熊猫

来自分类Dev

将自定义函数应用于具有通用名称的任何数据集

来自分类Dev

将自定义累积函数应用于熊猫数据框

来自分类Dev

如何在PyMC中将自定义函数应用于变量?

来自分类Dev

如何将自定义函数应用于熊猫数据框的2列?

来自分类Dev

将自定义函数应用于pandas Series会产生AttributeError

来自分类Dev

将自定义函数应用于r中的每一行

Related 相关文章

  1. 1

    如何使用bootstrap / css将自定义宽度应用于表格标题?

  2. 2

    如果使用apply和mutate_at满足条件,则将自定义函数应用于选择列中的数据

  3. 3

    使用dplyr :: mutate(across())将多列应用于自定义函数

  4. 4

    我应该在自定义函数中使用回调模式吗?节点js

  5. 5

    我应该在自定义函数中使用回调模式吗?节点js

  6. 6

    将自定义函数应用于数据框

  7. 7

    使用 jQuery addClass 或自定义 LoadCSS 函数将 css 应用于单个文本框

  8. 8

    无法在React Native中使用useRef()将current.focus()应用于按钮

  9. 9

    使用本机句柄将CSS应用于Angular指令(自定义或不自定义)

  10. 10

    我可以使用Git将更新补丁应用于自定义软件吗?

  11. 11

    JavaFX:使用自定义样式表将文本颜色应用于TableCell吗?

  12. 12

    JavaFX:使用自定义样式表将文本颜色应用于TableCell吗?

  13. 13

    使用 JQuery 中的自定义数据属性将 css 应用于 tr

  14. 14

    将自定义主题应用于Odoo应用

  15. 15

    将自定义类与自定义模板容器一起使用

  16. 16

    如何使用辅助函数将自定义类名称添加到Handlebars模板中的元素?

  17. 17

    可以在XSLT之外的XPath中使用current()函数吗?

  18. 18

    在postgres函数中使用now或current_timestamp

  19. 19

    将自定义样式应用于微调器

  20. 20

    无法将自定义listSelector应用于ListView

  21. 21

    将自定义功能应用于熊猫df

  22. 22

    将自定义累积功能应用于熊猫

  23. 23

    将自定义累积功能应用于熊猫

  24. 24

    将自定义函数应用于具有通用名称的任何数据集

  25. 25

    将自定义累积函数应用于熊猫数据框

  26. 26

    如何在PyMC中将自定义函数应用于变量?

  27. 27

    如何将自定义函数应用于熊猫数据框的2列?

  28. 28

    将自定义函数应用于pandas Series会产生AttributeError

  29. 29

    将自定义函数应用于r中的每一行

热门标签

归档