【发布时间】: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