xml-xslt xml上的可变匹配数

时间:2015-03-23 20:06:13

标签: xml xslt

我是xslt的新手,我正在试图弄清楚如何在xml中处理可变数量的元素。我正在尝试将此示例数据转换为Json。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" 
    href="/webservices/catalog/xsl/searchRetrieveResponse.xsl"?>

<searchRetrieveResponse xmlns:oclcterms="http://purl.org/oclc/terms/" 
                        xmlns:dc="http://purl.org/dc/elements/1.1/" 
                        xmlns:diag="http://www.loc.gov/zing/srw/diagnostic/" 
                        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<records>
<record>
<dc:contributor>Mitchell, E. Michael, 1920-2009,</dc:contributor>
<dc:contributor>Jacobi, Lotte, 1896-1990,</dc:contributor>
<dc:creator>Salinger, J. D. (Jerome David), 1919-2010,</dc:creator>
<dc:title>The Catcher in the Rye </dc:title>
</record>
<record>
<dc:creator>Salinger, J. D. (Jerome David), 1919-2010,</dc:creator>
<dc:title>Franny and Zooey</dc:title>
</record>
<record>
<dc:contributor>Jerry jome</dc:contributor>
<dc:contributor>Mitchell</dc:contributor>
<dc:contributor>E. Michael</dc:contributor>
<dc:creator>Salinger, J. D. (Jerome David), 1919-2010,</dc:creator>
<dc:title>nine stories</dc:title>
</record>
</records>
</searchRetrieveResponse>

`

这是我的XSLT样式表 -

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns:dc="http://purl.org/dc/elements/1.1/">
  <xsl:template match="/">
    {
      "bibliography":[
        <xsl:for-each select="searchRetrieveResponse/records/record">
          {
            "contributor":" <xsl:value-of select="recordData/oclcdcs/dc:contributor" />",
            "creator":"<xsl:value-of select="recordData/oclcdcs/dc:creator" />",
            "title":"<xsl:value-of select="recordData/oclcdcs/dc:title" />"
          },
        </xsl:for-each>
      ]
    }
    </xsl:template>
</xsl:stylesheet>

预期输出应该是这样的,其中贡献者应该是可以具有多个值或没有值的元素数组。

{
  "bibliography":[ 
    {
      "contributor":["Mitchell, E. Michael, 1920-2009,","Jacobi, Lotte, 1896-1990,"],
      "creator":"Salinger, J. D. (Jerome David), 1919-2010,",
      "title":"The Catcher in the Rye "
    },                    
    {
      "creator":"Salinger, J. D. (Jerome David), 1919-2010,",
      "title":"Franny and Zooey"
    },
    {
      "contributor":["Jerry jome","Mitchell","E. Michael"], 
      "creator":"Salinger, J. D. (Jerome David), 1919-2010,",
      "title":"nine stories"
    }
  ]
}

2 个答案:

答案 0 :(得分:0)

我会让你玩空白来让事情看起来像你想要的那样(<xsl:text>可能证明是有用的),但这将是我第一次通过它:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:dc="http://purl.org/dc/elements/1.1/">

    <xsl:template match="/">
        { "bibliography":
            [
                <xsl:apply-templates
                    select="searchRetrieveResponse/records/record"/>
            ]
        }
    </xsl:template>

    <xsl:template match="record">
        {
            "contributor":
            [
                <xsl:apply-templates select="dc:contributor"/>
            ]
            "creator":"<xsl:value-of select="dc:creator"/>",
            "title":"<xsl:value-of select="dc:title"/>"
        }
        <xsl:if test="following-sibling::record">,</xsl:if>
    </xsl:template>

    <xsl:template match="dc:contributor">
        "<xsl:value-of select="."/>"
        <xsl:if test="following-sibling::dc:contributor">,</xsl:if>
    </xsl:template>

</xsl:stylesheet>

答案 1 :(得分:0)

我使用xsl得到了输出。

`              {             &#34;书目&#34;:[                                              {

                        [
                            <xsl:for-each select="recordData/oclcdcs/dc:contributor">
                                " <xsl:value-of select="." />",
                                </xsl:for-each>
                        ],
                        "creator":"<xsl:value-of select="recordData/oclcdcs/dc:creator" />",
                        "title":"<xsl:value-of select="recordData/oclcdcs/dc:title" />",
                        "issn":"<xsl:value-of select="recordData/oclcdcs/dc:identifier" />"
                    },
                </xsl:for-each>
            ]
    }
</xsl:template>

`