简体   繁体   English

如何在XSLT 1.0中将数字/字母文本字符串分成两个XML元素?

[英]How can I separate a number/alpha text string into two XML elements in XSLT 1.0?

Is it possible to separate a number/alpha text string into two XML elements in XSLT 1.0? 在XSLT 1.0中是否可以将数字/字母文本字符串分成两个XML元素?

My input XML is: 我的输入XML是:

<root>
<tocsectionnumber>1Funding and investing</tocsectionnumber>
<tocsectionnumber>2Rules and articles</tocsectionnumber>
<tocsectionnumber>3Summary and conclusion</tocsectionnumber>
</root>

I would like my output to be: 我希望我的输出是:

<TocItem>
<TocItemNumber>1</TocItemNumber>
<TocItemTitle>Funding and investing</TocItemTitle>
</TocItem>
<TocItem>
<TocItemNumber>2</TocItemNumber>
<TocItemTitle>Rules and articles</TocItemTitle>
</TocItem>
<TocItem>
<TocItemNumber>3</TocItemNumber>
<TocItemTitle>Summary and conclusion</TocItemTitle>
</TocItem>

My number string could be any 1-3 digit number and the alpha string is a heading/title. 我的数字字符串可以是任何1-3位数字,而字母字符串则是标题/标题。

Any guidance is much appreciated. 任何指导是非常感谢。

Thanks. 谢谢。

You'd have to use a recursive template to count the leading digits, eg 您必须使用递归模板来计算前导数字,例如

<xsl:template name="leadingDigits">
  <xsl:param name="text" select="." />
  <xsl:param name="count" select="0" />
  <xsl:choose>
    <!-- check if the first character is a digit - when converted to number, any
         digit will equal itself, any non-digit will be NaN which does _not_
         equal itself -->
    <xsl:when test="number(substring($text, 1, 1)) =
                    number(substring($text, 1, 1))">
      <xsl:call-template name="leadingDigits">
        <xsl:with-param name="text" select="substring($text, 2)" />
        <xsl:with-param name="count" select="$count + 1" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$count" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

Now you can use that to separate the string into two parts 现在,您可以使用它来将字符串分成两部分

<xsl:template match="tocsectionnumber">
  <xsl:variable name="numDigits">
    <xsl:call-template name="leadingDigits" />
  </xsl:variable>

  <TocItem>
    <TocItemNumber>
      <xsl:value-of select="substring(., 1, $numDigits)" />
    </TocItemNumber>
    <TocItemTitle>
      <xsl:value-of select="substring(., $numDigits + 1)" />
    </TocItemTitle>
  </TocItem>
</xsl:template>

[groan] Who makes up these problems? [gro吟]谁来解决这些问题?

Anyway, try it this way: 无论如何,可以这样尝试:

XSLT 1.0 XSLT 1.0

<?xml version="1.0" encoding="UTF-8"?>
<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="/">
    <root>
        <xsl:for-each select="root/tocsectionnumber">
            <xsl:variable name="firstChar" select="substring(translate(., '0123456789', ''), 1, 1)" />
            <TocItem>
                <TocItemNumber><xsl:value-of select="substring-before(., $firstChar)"/></TocItemNumber>
                <TocItemTitle><xsl:value-of select="concat($firstChar, substring-after(., $firstChar))"/></TocItemTitle>
            </TocItem>
        </xsl:for-each>
    </root>
</xsl:template>

</xsl:stylesheet>

Note that this (or any other method) is going to bomb royally if the section title legitimately begins with a digit, eg "4. 101 Ways to Amuse Your Friends and Family". 请注意,如果部分标题合法地以数字开头,例如“ 4. 101种娱乐朋友和家人的方式”,则此(或任何其他方法)将遭到皇家轰炸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 xslt 1.0 在 xml 元素中划分字符串 - How can I divide string in xml element using xslt 1.0 我可以使用XSLT 1.0在XML元素中的文本内容周围添加<p> </ p>标签吗? - Can I add <p></p> tags around text content in XML elements using XSLT 1.0? XSLT 1.0-使用两个基于单独的XML元素的表构建XML电子表格 - XSLT 1.0 - Build an XML spreadsheet with two tables based on separate XML elements xslt 1.0-使用xslt 1.0如何使xslt适用于多个文档元素 - xslt 1.0- Using xslt 1.0 how can I make the xslt work for multiple document elements 如何使用XSLT1.0在XML文件中替换ID元素及其所有参考元素 - How can I replace an ID element AND all of its reference elements in an XML file using XSLT1.0 在XSLT 1.0中,当特定XML元素是多个具有相同名称的元素之一时,如何访问它的属性? - In XSLT 1.0, how can I access attributes of a particular XML element when it is one of multiple elements with the same name? XML / XSLT 1.0 - 如何使用XSL模板匹配一串文本? - XML / XSLT 1.0 - How to use XSL Template Match for a string of text? 如何使用 XSLT 1.0 翻译 XML 中的多个字符串文本 - how to translate multiples string text in a XML using XSLT 1.0 我如何在 xslt 1.0 / xml 中使用 else 函数 - How can i use an else function in xslt 1.0 / xml 如何在XSLT 1.0中循环字符串标记? - How can I loop on string tokens in XSLT 1.0?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM