【问题标题】:Add attribute to each xml node xslt将属性添加到每个 xml 节点 xslt
【发布时间】:2016-07-26 13:01:05
【问题描述】:

我正在尝试使用 xslt、please see example 或更低版本来处理 xml。另外我喜欢为每个节点添加一个属性,它代表节点的 xpath。但我得到了问题

XTDE0420:无法创建其父节点为文档节点的属性节点 (xpath)

我的示例 xml 是:

<?xml version="1.0" encoding="utf-16"?>
<shiporder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" orderid="orderid1">
  <orderperson>orderperson1</orderperson>
  <shipto>
    <name>name1</name>
    <address>address1</address>
    <city>city1</city>
    <country>country1</country>
  </shipto>
  <item>
    <title>title1</title>
    <note>note1</note>
    <quantity>1</quantity>
    <price>1</price>
  </item>
  <item>
    <title>title2</title>
    <note>note2</note>
    <quantity>79228162514264337593543950335</quantity>
    <price>-79228162514264337593543950335</price>
  </item>
  <item>
    <title>title3</title>
    <note>note3</note>
    <quantity>2</quantity>
    <price>79228162514264337593543950335</price>
  </item>
  <item>
    <title>title4</title>
    <note>note4</note>
    <quantity>79228162514264337593543950334</quantity>
    <price>0.9</price>
  </item>
  <item>
    <title>title5</title>
    <note>note5</note>
    <quantity>3</quantity>
    <price>1.1</price>
  </item>
</shiporder>

我用于转换的 xslt 表如下所示:

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

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:attribute name="xpath">
            <xsl:value-of select="func:getXpath(.)"/>
        </xsl:attribute>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

   <xsl:function name="func:createXPath" >
    <xsl:param name="pNode" as="node()"/>
    <xsl:value-of select="$pNode/ancestor-or-self::*/name()" separator="/"/>
  </xsl:function>

  <xsl:function name="func:getXpath">
  <xsl:param name="pNode" as="node()"/>
   <xsl:value-of select="$pNode/ancestor-or-self::*/(count(preceding-sibling::*) + 1)" separator="/" />
</xsl:function> 

</xsl:stylesheet>

另外,我喜欢将 name 和 counter 的函数结合起来,结果看起来像 nodeName[2]/nodeName[5]/..

【问题讨论】:

  • 请不要指望人们去远程站点阅读(谁知道怎么长)描述。请编辑您的 Q 以包含来自该站点的相关信息(并为真正好奇的人留下链接)。你应该阅读stackoverflow.com/help/mcve。祝你好运。
  • 请在您的问题中发布您的代码 - 并包括预期的输出。
  • 你也需要问一个问题。错误消息已经告诉你问题是什么,以及它发生在哪一行。您对该错误有什么具体问题?

标签: xslt


【解决方案1】:

代替:

 <xsl:template match="@*|node()">
    <xsl:attribute name="xpath">32</xsl:attribute>
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

尝试:

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:attribute name="xpath">32</xsl:attribute>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

注意:

  1. 你的输出方法应该是xml,而不是text

  2. 如果你想使用xsl:function,那么你的版本应该是2.0

  3. 您的第一个模板是多余的。

【讨论】:

  • Thx,您能否建议如何为siblings 添加计数器,使路径看起来像 nodeName[..]/odeName[..]?
  • @StellaMaris - 如果您真的想为每个元素添加包含 xpath 的属性,请尝试以下操作:xsltransform.net/6r5Gh4i/4
  • @StellaMaris 我建议您将此作为一个单独的问题提出。
  • @michael.hor257k 你说得对,我会在单独的帖子中提问。再次感谢。
  • @DanielHaley 当服务再次可用时,我会检查您的答案,也谢谢您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-04
  • 1970-01-01
  • 1970-01-01
  • 2017-04-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多