首页 文章

从节点列表中获取最短文本列表

提问于
浏览
1

我想获得具有最短文本的节点的项目和类型的列表 . 使用xslt / xpath .

我有以下示例xml:

<a>
  <b>
    <item>short</item>
    <item>longest</item>
    <item type="x">longest text</item>
    <item type="x">short text</item>
    <item type="y">text</item>
  </b>
</a>

这就是我到目前为止所拥有的

<xsl:template match="ns:item">
  <xsl:variable name="type">
    <xsl:choose>
       <xsl:when test="@type">
         <xsl:value-of select="@type" />
       </xsl:when>
       <xsl:otherwise>default</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:if test="position() = 1">{</xsl:if>
  "<xsl:value-of select='$type' />" : "<xsl:value-of select='.' />"
  <xsl:if test="position() != last()">,</xsl:if>
  <xsl:if test="position() = last()">}</xsl:if>  
</xsl:template>

如何从结果中过滤除最短文本之外的所有文本?

输出应该如下所示:

{ "default": "short",
 "x" : "short text",
 "y" : "text" }

由于我偶尔只使用xslt / xpath而且仅用于非常简单的事情,我希望有人可以帮助我 .

2 回答

  • 0

    我会分组(与Muenchian分组http://www.jenitennison.com/xslt/grouping/index.xml),然后在每组中取最短的项目(只需按长度排序并取第一项):

    <xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="1.0">
    
      <xsl:output method="text"/>
    
      <xsl:key name="k1" match="item" use="string(@type)"/>
    
      <xsl:template match="/">
        <xsl:text>{ </xsl:text>
        <xsl:apply-templates 
          select="a/b/item[generate-id() = generate-id(key('k1', string(@type))[1])]"/>
        <xsl:text> }</xsl:text>
      </xsl:template>
    
      <xsl:template match="item">
        <xsl:if test="position() &gt; 1"><xsl:text>,&#10;</xsl:text></xsl:if>
        <xsl:for-each select="key('k1', string(@type))">
          <xsl:sort select="string-length(.)" data-type="number"/>
          <xsl:if test="position() = 1">
            <xsl:variable name="type">
              <xsl:choose>
                <xsl:when test="@type">
                  <xsl:value-of select="@type"/>
                </xsl:when>
                <xsl:otherwise>default</xsl:otherwise>
              </xsl:choose>
            </xsl:variable>
            <xsl:text>"</xsl:text>
            <xsl:value-of select="$type"/>
            <xsl:text>" : "</xsl:text>
            <xsl:value-of select="."/>
            <xsl:text>"</xsl:text>
          </xsl:if>
        </xsl:for-each>
      </xsl:template>
    
    </xsl:stylesheet>
    

    随着输入

    <a>
      <b>
        <item>short</item>
        <item>longest</item>
        <item type="x">longest text</item>
        <item type="x">short text</item>
        <item type="y">text</item>
      </b>
    </a>
    

    结果是

    { "default" : "short",
    "x" : "short text",
    "y" : "text" }
    
  • 0

    XSLT 2.0 solution

    <xsl:stylesheet version="2.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output omit-xml-declaration="yes" indent="yes"/>
        <xsl:variable name="vQ">"</xsl:variable>
     <xsl:template match="/a/b">
        <xsl:text>{ </xsl:text>
         <xsl:for-each-group select="item" group-by="string(@type)">
          <xsl:variable name="vShortest" select=
          "current-group()[string-length(.) eq min(current-group()/string-length(.))]
          " />
          <xsl:variable name="vNotLast" select="not(position() eq last())"/>
          <xsl:variable name="vType" select=
          "'default'[not($vShortest/@type)],$vShortest/@type"/>
          <xsl:value-of select=
          "concat($vQ,$vType,$vQ,':', $vQ,$vShortest,$vQ, ',&#xA;'[$vNotLast])"/>
         </xsl:for-each-group>
          <xsl:text> }</xsl:text>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document

    <a>
        <b>
            <item>short</item>
            <item>longest</item>
            <item type="x">longest text</item>
            <item type="x">short text</item>
            <item type="y">text</item>
        </b>
    </a>
    

    the wanted result is produced:

    { "default":"short",
    "x":"short text",
    "y":"text" }
    

相关问题