【发布时间】:2019-04-29 06:08:41
【问题描述】:
使用 XSLT 1.0:
我有以下 xml 作为输入
<?xml version="1.0" encoding="utf-8" ?>
<Root>
<Sub1>
<Val1>A</Val1>
<Val3>C</Val3>
</Sub1>
<Sub2>
<Val2>NIL</Val2>
<Val4>D</Val4>
</Sub2>
</Root>
还有我的 XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/Root">
<xsl:element name ="New">
<xsl:apply-templates select="Sub1"/>
<xsl:apply-templates select="Sub2"/>
</xsl:element>
</xsl:template>
<xsl:template match="Sub1" >
<xsl:variable name='pr' select='.'/>
<xsl:element name="Myelement1" >
<!-- The value should be added to the output regardless-->
<xsl:value-of select="$pr/Val1"/>
<xsl:if test="number($pr/Val1) = $pr/Val1">
<!--TWO things should be done:
1- Check if <messages> is created in the output XML? if not it should be created
2- A message Should be added to <Messages> like <Message> Myelement1 is not numeric </Message>-->
</xsl:if>
</xsl:element>
</xsl:template>
<xsl:template match="Sub2" >
<xsl:variable name='pr' select='.'/>
<xsl:element name="Myelement2" >
<!-- The value should be added to the output regardless-->
<xsl:value-of select="$pr/Val2"/>
<xsl:if test="$pr/Val2='NIL'">
<!--TWO things should be done:
1- Check if <messages> is created in the output XML? if not it should be created
2- A message Should be added to <Messages> like <Message> Myelement2 is not valid </Message>-->
</xsl:if>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
当前 XSLT 生成以下输出:
<?xml version="1.0" encoding="utf-8"?>
<New>
<Myelement1>A</Myelement1>
<Myelement2>NIL</Myelement2>
</New>
但这是我真正想要的:
<?xml version="1.0" encoding="utf-8"?>
<New>
<Myelement1>A</Myelement1>
<Myelement2>NIL</Myelement2>
<Messages>
<Message> Myelement1 is not numeric </Message>
<Message> Myelement2 is not valid </Message>
</Messages>
</New>
我的问题是消息元素。
此元素是动态的,消息子元素的数量可能会有所不同,并且取决于源 XML 的内容。
它也可能是空的,但最大的问题是我想在运行时添加这些消息子元素,而 XSLT 正在使用多个模板等浏览源 xml 文件。
假设我有两个不同的模板来生成名为 Temp1 和 temp2 的 Myelement1 和 Myelement2,所以我想在消息元素下添加一些消息(不是常量消息,可以是任何东西),而应用的模板 (temp1) 是正在执行,然后在执行第二个模板 (temp2) 时会收到更多不同的消息(不是恒定消息)。
例如 如果 A 不是数字,则应在消息下添加如下消息
<message> myelement1 is not in a correct format < Message>
将消息元素视为日志或错误元素。
感谢您的帮助。
【问题讨论】:
-
抱歉,这还不清楚。您需要更详细地解释,使用示例 XML 和 XSL,
<Message>元素是如何生成的。 -
@JimGarrison 刚刚添加了一个例子
-
这很好,但它是如何生成的?添加消息的决定在哪里?
-
@JimGarrison ,我再次更新了问题。希望这一次,很清楚
-
如果我正确理解了您的问题 (?),您需要分两步执行此操作:首先,让您的模板写入变量。然后处理变量(将其转换为节点集后)并将两组消息合并为一组。要么改变你的整体方法。我不知道有一种方法可以让模板写入输出中的两个不同位置。