2016-04-15 180 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

相關問題