问题
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.
回答1:
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='), ')')"/>
来源:https://stackoverflow.com/questions/31123564/xslt-1-0-search-an-attribute-and-add-to-parent-element