insert unbounded XML element using xslt
I did post many questions regarding inserting XML elements using XSLT, I am new to XSLT and I am learning while working on it. So thanks to everyone that helped me through.
Now, I came up with another question: so I am inserting a repeatable (unbounded) XML element so it will have the same xpath but different values for the element, when I put the xpath to match the template pattern it overrides the earlier elements that have been inserted. So is there a way to insert multiple elements into an exisiting XML using the same xpath? my input is the xpath where those elements sh开发者_如何学Goould be inserted and the elements values. For example my input is:
xpath: /root/child
element to insert: new_element
with the values: new1, new2 new3
so the output should look like:
<root>
<child>
<new_element>new1</new_element>
<new_element>new2</new_element>
<new_element>new3</new_element>
</child>
</root>
Thanks :)
I'm going probably to misunderstand your question, but it's hard to achieve your intents given the details in your post. I shouldn't guess here I know...anyway, are you interested in something like this...?
Example transform:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="newdata">
<new_element>new1</new_element>
<new_element>new2</new_element>
<new_element>new3</new_element>
</xsl:variable>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/root/child">
<xsl:copy>
<xsl:copy-of select="$newdata"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Given this input
<root>
<child/>
</root>
Returns:
<root>
<child>
<new_element>new1</new_element>
<new_element>new2</new_element>
<new_element>new3</new_element>
</child>
</root>
精彩评论