2016-04-15 114 views
0

有人可以指导我如何编写XSLT1.0来创建输出,如下所示: <csvImportSchema> <payload> <test>**COPY VALUE**</test> <test2>2</test2> <test3>3</test3> <ean>1111111111</ean> <productId/> </payload> </csvImportSchema>XSLT复制一个节点值并创建一个新节点

<csvImportSchema> <payload> <test>COPY VALUE</test> <test2>2</test2> <test3>3</test3> <ean>1111111111</ean> <productId/> **<copied>COPY VALUE</copied>** </payload> <csvImportSchema>

回答

0

下面的XSLT提取**并将其拷贝之间的COPY VALUE串到<payload>标签的末端。然后COPY VALUE之前和之后的字符串将用作<copied>标记的前缀和后缀。

<?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="node()|@*"> 
    <xsl:copy> 
    <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="payload"> 
    <xsl:variable name="copyval" select="substring-before(substring-after(test/text(),'**'),'**')" /> 
    <xsl:variable name="valBefore" select="substring-before(test/text(),$copyval)" /> 
    <xsl:variable name="valAfter" select="substring-after(test/text(),$copyval)" /> 
    <xsl:copy> 
     <test><xsl:value-of select="$copyval" /></test> 
     <xsl:apply-templates select="node()[not(self::test)]|@*" /> 
     <xsl:value-of select="$valBefore" /><copied><xsl:value-of select="$copyval" /></copied><xsl:value-of select="$valAfter" /> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 
+0

我不认为那些星号真的存在。我不认为'node()[local-name()!='test']'是很好的做法 - 我会使用'node()[not(self :: test)]''。如果有必要的话。 –

+0

@ michael.hor257k:我用你建议的更好的做法改进了我的答案。关于星号:是的,我也想知道。但是这是所问的问题,我试图回答它给出的。如果作者对此发表评论,如果需要,我可以轻松删除这些行。 – zx485

相关问题