[英]XSLT creation from XML and load into CSV file
我需要將 XML 文件轉換為 CSV。
預期 Output:
tag1 tag2 Typearray
Name1 Name2 1
Name1 Name2 13
使用 XSLT 只能獲得一條記錄,而數組中的第二條記錄被拒絕。
tag1,tag2,CommOffer,TypeArray
name1,name2,1
你能告訴我如何使用 XSLT 讀取數組值嗎?
樣品 XML:
<Master2>
<Child1>
<tag1>name1</tag1>
<tag2>name2</tag2>
<TypeArray>
<value>1</value>
<value>13</value>
</TypeArray>
</Child1>
</Master2>
</Master1>
XSLT:
<xsl:template match="/">
<xsl:text>tag1,tag2,TypeArray</xsl:text>
<xsl:text>
</xsl:text>
<xsl:for-each select="Master1/Master2/Child1">
<xsl:call-template name="CsvEscape"><xsl:with-param name="value" select="normalize-
space(tag1)"/></xsl:call-template>
<xsl:text>,</xsl:text>
<xsl:call-template name="CsvEscape"><xsl:with-param name="value" select="normalize-
space(tag2)"/></xsl:call-template>
<xsl:text>,</xsl:text>
<xsl:call-template name="CsvEscape"><xsl:with-param name="value" select="TypeArray/value"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
很難從單個示例中推斷出所需的邏輯。 如果 - 看起來 - 您想為每個TypeArray/value
創建一行,並且在每一行中重復tag1
和tag2
值,您可以執行以下操作:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8" />
<xsl:template match="/Master1">
<!-- header -->
<xsl:text>tag1,tag2,TypeArray </xsl:text>
<!-- data -->
<xsl:for-each select="Master2/Child1">
<xsl:variable name="tags">
<xsl:value-of select="tag1"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="tag2"/>
<xsl:text>,</xsl:text>
</xsl:variable>
<xsl:for-each select="TypeArray/value">
<xsl:value-of select="$tags"/>
<xsl:value-of select="."/>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.