2017-04-14 120 views
0

我是新手编写XSL.I熟悉10年前使用DOM解析器处理XML,但技术发生了变化。我想我仍然可以用Java DOM解析器完成这个工作,但是这对系统来说会是开销。xml转换为xml转换用于重复标记

我有一个以下格式的源XML。标签“值”发生变化。我需要将其转换为第二个xml格式。

XML来源:

<Part> 
    <PartNumber>XYZ</PartNumber> 
    <ProductLines> 
     <Value>P58</Value> 
     <Value>P84</Value> 
     <Value>P88</Value> 
     <Value>P99</Value> 
    </ProductLines> 
    </Part> 

XML目标要

<Part> 
    <PartNumber>XYZ</PartNumber> 
    <ProductLines>P58,P84,P88,P99</ProductLines> 
    </Part> 

我想下面XSL,不工作:从XML大师们需要咨询。

<?xml version="1.0" encoding="UTF-8"?> 
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
     xmlns:xs="http://www.w3.org/2001/XMLSchema" 
     exclude-result-prefixes="xs" 
     version="1.0"> 
     <xsl:output method="xml"/> 
    <xsl:template match="node()|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*"></xsl:apply-templates> 
     </xsl:copy> 
    </xsl:template> 
     <xsl:template match="ProductLines"> 
      <xsl:for-each select="Value"> 
       <xsl:value-of select="."></xsl:value-of> 
      </xsl:for-each> 
     </xsl:template> 
     <xsl:template match="ProductLines/node()"/> 
    </xsl:stylesheet> 
+1

什么是 '不工作'?你能详细说明具体问题吗?无论如何,这通常有助于让你更接近解决方案。 – jediz

回答

0

你可以使用这样的事情来串联值:

<xsl:variable name="concatenated_value"> 
    <xsl:for-each select="Value"> 
     <xsl:value-of select="."/> 
     <xsl:if test="position() != last()"> 
      <xsl:value-of select="','"/> 
     </xsl:if> 
    </xsl:for-each> 
</xsl:variable> 
+0

是的,这是完美的。 –