XSLT 1.0:搜索属性并添加到父元素 [英] XSLT 1.0: search an attribute and add to parent element
问题描述
XML 输入 1:
<product>Carneros</product><年>1997</年><价格>10.99</价格><字段属性="gccxml(msgid=237)"/><Field id="5" 属性=""/><Field id="6" 属性=""/><Field line="19"/></葡萄酒>
XML 输入 2:
<product>Carneros</product><年>1997</年><价格>10.99</价格><方法属性="gccxml(msgid=237)"><参数位置="f0:13"/></方法><Field line="19" attributes=""/></葡萄酒>
XML 输出:
<product>Carneros</product><年>1997</年><价格>10.99</价格><方法属性="gccxml(msgid=237)"><参数位置="f0:13"/></方法><Field line="19"/></葡萄酒>
任务逻辑如下:grad第一个外观属性内容(即attributes="gccxml(msgid=237)")
并将其添加到父元素中.请注意,attributes
可以位于 Field
节点或 Method
节点中.另外,将 attributes
重命名为 msgid
并将 gccxml(msgid=237)
转换为 msgid="237"
总结:XML 输出应该只更新带有 msgid="237"
基本上,通过从给定节点获取特定属性并将其复制到其父元素,可以很容易地进行硬编码".在这里,复杂性是通过字段"和方法"节点进行搜索,对属性"不为空的第一次出现进行分级,进行一些字符串操作并将其放置在父元素中.
基本上,通过从给定节点获取特定属性并将其复制到其父元素,可以很容易地进行硬编码".
在这里,复杂的是通过字段"和方法"节点进行搜索,对属性"不为空的第一次出现进行分级,进行一些字符串操作并将其置于父节点元素.
在这里,复杂性是通过字段"和方法"节点进行搜索,对属性"不为空的第一次出现进行分级,进行一些字符串操作并将其置于父元素中.
任务逻辑如下:grad第一个外观属性内容(即 attributes="gccxml(msgid=237)")并将其添加到父级元素.
这部分可以通过以下方式轻松完成:
XSLT 1.0
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/><xsl:strip-space elements="*"/><!-- 身份变换--><xsl:template match="@*|node()"><xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy></xsl:模板><xsl:template match="/*"><xsl:copy><xsl:copy-of select="@*"/><xsl:attribute name="msgid"><xsl:value-of select="//@attributes[string(.)][1]"/></xsl:attribute><xsl:apply-templates select="node()"/></xsl:copy></xsl:模板></xsl:stylesheet>
<块引用>
另外,从attributes重命名为msgid ...
我们已经做到了...
<块引用>... 并将 gccxml(msgid=237) 转换为 msgid="237"
这个转换的逻辑不是很清楚.
<小时>编辑
要提取 gccxml(msgid=
后的数字,请使用:
<xsl:value-of select="substring-before(substring-after(//@attributes[string(.)][1], 'msgid='), ')')"/>
XML Input 1:
<wine grape="chardonnay">
<product>Carneros</product>
<year>1997</year>
<price>10.99</price>
<Field attributes="gccxml(msgid=237)"/>
<Field id="5" attributes=""/>
<Field id="6" attributes=""/>
<Field line="19"/>
</wine>
XML Input 2:
<wine grape="chardonnay">
<product>Carneros</product>
<year>1997</year>
<price>10.99</price>
<Method attributes="gccxml(msgid=237)">
<Argument location="f0:13"/>
</Method>
<Field line="19" attributes=""/>
</wine>
XML output:
<wine grape="chardonnay" msgid="237">
<product>Carneros</product>
<year>1997</year>
<price>10.99</price>
<Method attributes="gccxml(msgid=237)">
<Argument location="f0:13"/>
</Method>
<Field line="19"/>
</wine>
The task logic is as follows:
grad the first appearance attribute content (i.e. attributes="gccxml(msgid=237)")
and add it in the parent element.
Notice that the attributes
can be in the Field
node or the Method
node.
In addition, rename from attributes
to msgid
and convert gccxml(msgid=237)
to msgid="237"
To conclude: the XML output should only update the parent element with msgid="237"
Basically, it is quite easy to do 'hard coded' by grabing a specific attribute from a given node and copy to its parent element.
In here, the complexity is to search thru the 'Field' and 'Method' nodes, to grad the first occurance where the 'attribute' is not null, to do some string manipulation and place it its the parent element.
Basically, it is quite easy to do 'hard coded' by grabing a specific attribute from a given node and copy to its parent element.
In here, the complexity is to search thru the 'Field' and 'Method' nodes, to grad the first occurance where the 'attribute' is not null, to do some string manipulation and place it its the parent element.
In here, the complexity is to search thru the 'Field' and 'Method' nodes, to grad the first occurance where the 'attribute' is not null, to do some string manipulation and place it its the parent element.
The task logic is as follows: grad the first appearance attribute content (i.e. attributes="gccxml(msgid=237)") and add it in the parent element.
This part can be accomplished easily by:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="msgid">
<xsl:value-of select="//@attributes[string(.)][1]"/>
</xsl:attribute>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
In addition, rename from attributes to msgid ...
We've done that already...
... and convert gccxml(msgid=237) to msgid="237"
The logic of this conversion is not quite clear.
Edit
To extract the number after gccxml(msgid=
use:
<xsl:value-of select="substring-before(substring-after(//@attributes[string(.)][1], 'msgid='), ')')"/>
这篇关于XSLT 1.0:搜索属性并添加到父元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!