Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 拆分逗号分隔值的XSL转换_Xml_Xslt_Csv_Tags - Fatal编程技术网

Xml 拆分逗号分隔值的XSL转换

Xml 拆分逗号分隔值的XSL转换,xml,xslt,csv,tags,Xml,Xslt,Csv,Tags,我有一个以下格式的XML示例: <Records> <Record> <Name>Test</Name> <IDS>Test-1, Test-2, Test-3</IDS> <Type>Type1</Type> </Record> <Record> <Name>XML</Name> <IDS>Test-4, Test

我有一个以下格式的XML示例:

<Records>
 <Record>
  <Name>Test</Name>
  <IDS>Test-1, Test-2, Test-3</IDS>
  <Type>Type1</Type>
 </Record>
 <Record>
  <Name>XML</Name>
  <IDS>Test-4, Test-5</IDS>
  <Type>XMLType</Type>
 </Record>
</Records>

试验
测试1、测试2、测试3
类型1
XML
测试4,测试5
XMLType
我想从IDs标记中提取逗号分隔的值,以便在输出XML中清楚地设置它们。在本例中,由于总共有5个ID,我将有5个记录,其中3个在一个组中,2个在另一个组中

基本上,我的输出应该是这样的:

<Records>
 <Record>
  <Name>Test</Name>
  <ID>Test-1</ID>
 <Record>
 <Record>
  <Name>Test</Name>
  <ID>Test-2</ID>
 </Record>
 ..
 ..
 <Record>
  <Name>XML</Name>
  <ID>Test-5</ID>
 </Record>
</Records>
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:strip-space elements="*" />
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!-- ignore IDS in Record -->
    <xsl:template match="Record/IDS" />

    <!-- ignore Type in Record -->
    <xsl:template match="Record/Type" />


    <xsl:template match="Record">
        <xsl:call-template name="splitIDS">
            <xsl:with-param name="ids" select="IDS" />
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="splitIDS">
        <xsl:param name="ids"/>
        <xsl:choose>
            <xsl:when test="contains($ids,',')">
                <xsl:call-template name="splitIDS">
                    <xsl:with-param name="ids" select="substring-before($ids,',')"/>
                </xsl:call-template>
                <xsl:call-template name="splitIDS">
                    <xsl:with-param name="ids" select="substring-after($ids,' ')"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy>
                    <xsl:apply-templates />
                    <ID>
                        <xsl:value-of select="$ids"/>
                    </ID>
                </xsl:copy>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="Records">
        <xsl:copy>
            <xsl:apply-templates select="Record" />
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

试验
测试-1
试验
测试-2
..
..
XML
测试-5
如何拆分值以生成此类输出?

这里是一个基于“身份转换”的xslt-1.0解决方案。
IDS的内容通过对splitIDS的递归调用进行剥离。 记录/类型将被忽略,因为它们不在输出示例中。
试着这样做:

<Records>
 <Record>
  <Name>Test</Name>
  <ID>Test-1</ID>
 <Record>
 <Record>
  <Name>Test</Name>
  <ID>Test-2</ID>
 </Record>
 ..
 ..
 <Record>
  <Name>XML</Name>
  <ID>Test-5</ID>
 </Record>
</Records>
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:strip-space elements="*" />
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!-- ignore IDS in Record -->
    <xsl:template match="Record/IDS" />

    <!-- ignore Type in Record -->
    <xsl:template match="Record/Type" />


    <xsl:template match="Record">
        <xsl:call-template name="splitIDS">
            <xsl:with-param name="ids" select="IDS" />
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="splitIDS">
        <xsl:param name="ids"/>
        <xsl:choose>
            <xsl:when test="contains($ids,',')">
                <xsl:call-template name="splitIDS">
                    <xsl:with-param name="ids" select="substring-before($ids,',')"/>
                </xsl:call-template>
                <xsl:call-template name="splitIDS">
                    <xsl:with-param name="ids" select="substring-after($ids,' ')"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy>
                    <xsl:apply-templates />
                    <ID>
                        <xsl:value-of select="$ids"/>
                    </ID>
                </xsl:copy>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="Records">
        <xsl:copy>
            <xsl:apply-templates select="Record" />
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

将生成以下输出:

<?xml version="1.0"?>
<Records>
  <Record>
    <Name>Test</Name>
    <ID>Test-1</ID>
  </Record>
  <Record>
    <Name>Test</Name>
    <ID>Test-2</ID>
  </Record>
  <Record>
    <Name>Test</Name>
    <ID>Test-3</ID>
  </Record>
  <Record>
    <Name>XML</Name>
    <ID>Test-4</ID>
  </Record>
  <Record>
    <Name>XML</Name>
    <ID>Test-5</ID>
  </Record>
</Records>

试验
测试-1
试验
测试-2
试验
测试-3
XML
测试-4
XML
测试-5

非常感谢,这帮助我拆分了值。现在如何更改XML中存在的标记的名称?比如说,我想包括
,但以
的名义,我该怎么做呢?例如,
更改为:
是,这将为我提供测试-1。如何将特定值放入值属性中
使用
xsl:attribute
或更短的
它实际上会将整个ID返回给我
value=“Test-1、Test-2、Test-3”
。此外,输出仅为3