XSLT用于拆分逗号分隔的XML列表

时间:2014-05-12 13:45:32

标签: xml xslt

我正在尝试将下面的逗号分隔标签列表拆分为单个元素。 XML源中的元素和属性名称将始终相同。我使用1.0,所以我希望有1.0解决方案。基于this similar example我认为以下XSL可以工作:

<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output indent="yes"/>
      <xsl:template match="pbcoreCollection">
        <pbcoreCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
             <pbcoreDescriptionDocument>
                 <xsl:call-template name="tokenize">
                 <xsl:with-param name="text" select="instantiationannotation"/>
                 <xsl:with-param name="elemName" select="'instantitionAnnotation'"/>
                </xsl:call-template>
            </pbcoreDescriptionDocument>
        </pbcoreCollection>
    </xsl:template> 
    <xsl:template name="tokenize">
        <xsl:param name="text"/>
        <xsl:param name="elemName"/>
        <xsl:param name="sep" select="', '"/>
        <xsl:choose>
            <xsl:when test="contains($text, $sep)">
                <xsl:element name="{$elemName}">
                    <xsl:value-of select="substring-before($text, $sep)"/>
                </xsl:element>
                <!-- recursive call -->
                <xsl:call-template name="tokenize">
                    <xsl:with-param name="text" select="substring-after($text, $sep)" />
                    <xsl:with-param name="elemName" select="$elemName" />
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:element name="{$elemName}">
                    <xsl:value-of select="$text"/>
                </xsl:element>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

但它会产生结果

<?xml version="1.0"?>
<pbcoreCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <pbcoreDescriptionDocument>
    <instantitionAnnotation/>
  </pbcoreDescriptionDocument>
</pbcoreCollection>

我原来的XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<pbcoreCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
   <pbcoreDescriptionDocument>
        <pbcoreInstantiation>
            <instantiationAnnotation annotationType="CMS tag">congress, guns, gun_policy, catholic_schools, social_media, </instantiationAnnotation>
        </pbcoreInstantiation>
   </pbcoreDescriptionDocument>
</pbcoreCollection>

我希望看起来像这样:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<pbcoreCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
   <pbcoreDescriptionDocument>
        <pbcoreInstantiation>
            <instantiationAnnotation annotationType="CMS tag">congress</instantiationAnnotation>
            <instantiationAnnotation annotationType="CMS tag">gun</instantiationAnnotation>
            <instantiationAnnotation annotationType="CMS tag">gun_policy</instantiationAnnotation>
            <instantiationAnnotation annotationType="CMS tag">catholic_schools</instantiationAnnotation>
            <instantiationAnnotation annotationType="CMS tag">social_media</instantiationAnnotation>
        </pbcoreInstantiation>
     </pbcoreDescriptionDocument>
</pbcoreCollection>

1 个答案:

答案 0 :(得分:0)

  

在这种情况下,是的,XML源中的元素和属性名称   永远都是一样的。

在这种情况下,您可以简化为:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
    <pbcoreCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
       <pbcoreDescriptionDocument>
            <pbcoreInstantiation>
                <xsl:call-template name="tokenize">
                    <xsl:with-param name="text" select="pbcoreCollection/pbcoreDescriptionDocument/pbcoreInstantiation/instantiationAnnotation"/>
                </xsl:call-template>
            </pbcoreInstantiation>
       </pbcoreDescriptionDocument>
    </pbcoreCollection>
</xsl:template>

<xsl:template name="tokenize">
    <xsl:param name="text"/>
    <xsl:param name="sep" select="', '"/>
    <xsl:choose>
        <xsl:when test="contains($text, $sep)">
            <instantiationAnnotation annotationType="CMS tag">
                <xsl:value-of select="substring-before($text, $sep)"/>
            </instantiationAnnotation>
            <!-- recursive call -->
            <xsl:call-template name="tokenize">
                <xsl:with-param name="text" select="substring-after($text, $sep)" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <instantiationAnnotation annotationType="CMS tag">
                <xsl:value-of select="$text"/>
            </instantiationAnnotation>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

注意:如果您的输入确实带有一个尾随的“,”分隔符,则可能需要更多工作,如示例所示。