代码之家  ›  专栏  ›  技术社区  ›  Chris James

XSLT,逐个处理元素

  •  0
  • Chris James  · 技术社区  · 14 年前

    我在XSLT方面很弱,所以这似乎很明显。这是一些XML示例

     <term>
      <name>cholecystocolonic fistula</name> 
      <definition>blah blah</definition> 
      <reference>cholecystocolostomy</reference> 
      </term>
    

    这是我刚才写的用来处理它的XSLT

    <xsl:template name="term">
        {
        "dictitle": "<xsl:value-of select="name" disable-output-escaping="yes" />",
        "html": "<xsl:value-of select="definition" disable-output-escaping="yes"/>",
        "reference": "<xsl:value-of select="reference" disable-output-escaping="yes"/>
    }
    </xsl:template>
    

    基本上,我是从XML创建JSON。

    需求现在已经改变了,所以XML现在可以有多个定义标记和引用标记。它们可以以任何顺序出现,即定义、引用、引用、定义、引用。

    如何更新XSLT以适应这种情况?可能值得一提的是,因为我的XSLT处理器使用.NET,所以我只能使用XSLT 1.0命令。

    多谢!

    编辑-澄清

    这是我想要创建的JSON类型,给出了以下XML示例

     <term>
      <name>cholecystocolonic fistula</name> 
      <definition>blah blah</definition> 
      <reference>cholecystocolostomy</reference>
      <definition>XSLT is not as easy as it should be</definition>
      <reference>qui</reference> 
      </term>
    
    {
        dictitle: "cholecystocolonic fistula",
        html: "blah blah",
        reference: "cholecystocolostomy",
        html: "XSLT is not as easy as it should be",
        reference: "qui"
    }
    
    3 回复  |  直到 14 年前
        1
  •  3
  •   Fil    14 年前

    JSON属性名必须是唯一的,因此不能像您概述的那样具有多个“引用”属性。

    XML没有这种限制;允许在同一级别上有多个“引用”节点。

    http://metajack.im/2010/02/01/json-versus-xml-not-as-simple-as-you-think/

        2
  •  -1
  •   Mrs Kensington    14 年前

    这应该生成您想要的JSON,但不幸的是,输出不是有效的JSON…

        <xsl:template match="/term">
        {
                <xsl:apply-templates select="*"/>
        }
        </xsl:template>
    
        <xsl:template match="name">
                "dictitle": "<xsl:value-of select="." disable-output-escaping="yes" />",
        </xsl:template>
        <xsl:template match="definition">
                "html": "<xsl:value-of select="." disable-output-escaping="yes"/>",
        </xsl:template>
        <xsl:template match="reference">
                "reference": "<xsl:value-of select="." disable-output-escaping="yes"/>
        </xsl:template>
    
        3
  •  -1
  •   Matti Virkkunen    14 年前

    您需要使用 for-each 元素。

    这将为您提供一个包含所有引用元素的数组:

    "references": [
        <xsl:for-each select="reference">
            <xsl:if test="position() &gt; 1">,</xsl:if>
            "<xsl:value-of select="." disable-output-escaping="yes" />"
        </xsl:for-each>
    ]
    

    如您所见,我为数组添加了[]并使用了 对于每一个 元素内部。选择的值 . 返回当前节点的内容。输出大致如下:

    "references": ["foo", "bar", "baz"]
    

    如果这个JSON结构不是您想要的,请告诉我。

    编辑:

    如果您想要保留顺序,您必须稍微更改一下JSON结构:

    "refsAndDefs": [
        <xsl:for-each select="reference|definition">
            <xsl:if test="position() &gt; 1">,</xsl:if>
            "<xsl:value-of select="name()" />": "<xsl:value-of select="." disable-output-escaping="yes" />"
        </xsl:for-each>
    ]
    

    结果将是

    "refsAndDefs": [
        {"reference": "foo"},
        {"definition": "bar"},
        {"definition": "baz"},
        {"reference": "baa"}
    ]
    

    等等。