如何编号节点标签

时间:2019-07-15 08:37:05

标签: xslt

在这个(示例)xml中,我需要给诸如<book1><book2>等的书签编号。

结果XML将被导入,因此需要该符号。结果将永远不会有超过6个书节点。我写的xslt全部命名为<book>

这是原始xml

<?xml version="1.0"?>
<catalog>
   <book>
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
    </book>
   <book>
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
   </book>
   <book>
      <author>Corets, Eva</author>
      <title>Maeve Ascendant</title>
   </book>
   <book>
      <author>Randall, Cynthia</author>
      <title>Lover Birds</title>
   </book>
   <book>
      <author>Corets, Eva</author>
      <title>Oberon's Legacy</title>
   </book>
   <book>
      <author>Corets, Eva</author>
      <title>The Sundered Grail</title>
   </book>
   <book>
      <author>Thurman, Paula</author>
      <title>Splish Splash</title>
   </book>
</catalog>

这是我已经拥有的xsl:

<xsl:stylesheet version="2.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="catalog">
        <catalog>
            <xsl:if test="//book[author='Corets, Eva']">
                <book>
                    <sample>Here comes info about Eva Corets</sample>
                </book>
            </xsl:if>
            <xsl:if test="//book[author='Ralls, Kim']">
                <book>
                    <sample>Here comes info about Kim Ralls</sample>
                </book>
            </xsl:if>
            <xsl:if test="//book[author='Thurman, Paula']">
                <book>
                    <sample>Here comes info about Paula Thurman</sample>
                </book>
            </xsl:if>
        </catalog>
    </xsl:template>
</xsl:stylesheet>

哪个输出:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <book>
    <sample>Here comes info about Eva Corets</sample>
  </book>
  <book>
    <sample>Here comes info about Kim Ralls</sample>
  </book>
  <book>
    <sample>Here comes info about Paula Thurman</sample>
  </book>
</catalog>

这是我需要的:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <book1>
    <sample>Here comes info about Eva Corets</sample>
  </book1>
  <book2>
    <sample>Here comes info about Kim Ralls</sample>
  </book2>
  <book3>
    <sample>Here comes info about Paula Thurman</sample>
  </book3>
</catalog>

2 个答案:

答案 0 :(得分:0)

这应该做。

<xsl:stylesheet version="2.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="catalog">
        <catalog>
            <xsl:for-each select="book[author='Corets, Eva']">
            <xsl:variable name="book_el" select="concat('book', position())"></xsl:variable>
               <xsl:element name="{$book_el}">
                    <xsl:copy-of select="node()"/>
                </xsl:element>
            </xsl:for-each>
        </catalog>
    </xsl:template>
</xsl:stylesheet>

答案 1 :(得分:0)

如果您只能使用XSLT 1.0,则可以使用技术调用Muenchian Grouping从书中找寻不同的作者,尽管您需要额外的工作才能按特定的顺序找他们。

尝试使用此XSLT

<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:key name="books" match="book" use="author" />

  <xsl:param name="authors">|Corets, Eva|Ralls, Kim|Thurman, Paula|</xsl:param>

  <xsl:template match="catalog">
    <catalog>
      <xsl:for-each select="book[contains($authors, concat('|', author, '|'))][generate-id() = generate-id(key('books', author)[1])]">
        <xsl:sort select="string-length(substring-before($authors, concat('|', author, '|')))" />
        <xsl:element name="book{position()}">
          <sample>
            <xsl:text>Here comes info about </xsl:text>
            <xsl:value-of select="author" />
          </sample>
          <info>
            <xsl:for-each select="key('books', author)">
              <xsl:if test="position() > 1">, </xsl:if>
              <xsl:value-of select="title" />
            </xsl:for-each>
          </info>
        </xsl:element>
      </xsl:for-each>
    </catalog>
  </xsl:template>
</xsl:stylesheet>

如果您只想按字母顺序列出xsl:sort,则可以将<xsl:sort select="author" />替换为For i= 8 To Split(sheetXls.UsedRange.Address, "$")(4)