如何使用 xslt 中分隔符的索引解析分隔字符串?

发布于 2024-10-08 22:16:00 字数 826 浏览 0 评论 0原文

我有一个分隔字符串,如下所示,

FirstName&LastName&Address1&Address2&City&State&country&

我需要根据分隔符出现的索引来拆分字符串。

比如说,如果我需要字符串中的前四个值,我应该得到输出 两个不同的变量,其中第一个变量应具有字符串 前四个值和第二个变量具有剩余的字符串。

我不想使用 exslt 或任何其他扩展。 请帮我一个简单的解决方案。

    Input:

    <xsl:variable name="index" select='4' />

<xsl:variable name="delimitedString" select='FirstName&amp;LastName&amp;Address1&amp;Address2&amp;City&amp;State&amp;country&amp;' />

Output:

<xsl:variable name="requiredString">
FirstName&amp;LastName&amp;Address1&amp;Address2&amp;
</xsl:variable>

<xsl:variable name="remainingString">
City&amp;State&amp;country&amp;
</xsl:variable>

I have a delimited string which looks like below

FirstName&LastName&Address1&Address2&City&State&country&

I need to split the string based on the index of occurrence of the delimiter.

Say, if i need the first four values in the string, i should get the output in
two different variables, where the first variable should have the string with
first four values and the second variable having the remaining string.

I don't want to use exslt or any other extensions.
Please help me with a simple solution.

    Input:

    <xsl:variable name="index" select='4' />

<xsl:variable name="delimitedString" select='FirstName&LastName&Address1&Address2&City&State&country&' />

Output:

<xsl:variable name="requiredString">
FirstName&LastName&Address1&Address2&
</xsl:variable>

<xsl:variable name="remainingString">
City&State&country&
</xsl:variable>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花辞树 2024-10-15 22:16:00

此转换

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pSplitIndex" select="4"/>

 <xsl:template match="text()" name="splitAt">
  <xsl:param name="pLeft" select="''"/>
  <xsl:param name="pRight" select="."/>
  <xsl:param name="pSplitAt" select="$pSplitIndex"/>
  <xsl:param name="pDelim" select="'&'"/>

  <xsl:choose>
    <xsl:when test=
     "not(string-length($pRight))
     or
      not(contains($pRight, $pDelim))
     ">
     <t1>
       <xsl:value-of select="concat($pLeft, $pRight)"/>
     </t1>
     <t2/>
    </xsl:when>
    <xsl:otherwise>
     <xsl:choose>
      <xsl:when test="$pSplitAt = 0">
       <t1><xsl:value-of select="$pLeft"/></t1>
       <t2><xsl:value-of select="$pRight"/></t2>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="splitAt">
         <xsl:with-param name="pLeft" select=
          "concat($pLeft, substring-before($pRight, $pDelim), $pDelim)
          "/>
         <xsl:with-param name="pRight"
          select="substring-after($pRight, $pDelim)"/>
         <xsl:with-param name="pSplitAt" select="$pSplitAt -1"/>
         <xsl:with-param name="pDelim" select="$pDelim"/>
        </xsl:call-template>
      </xsl:otherwise>
     </xsl:choose>
    </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

应用于此 XML 文档时

<t>FirstName&LastName&Address1&Address2&City&State&country&</t>

生成所需的两个字符串,这些字符串是通过在以下位置拆分 /t/text() 得到的:第四个 & 分隔符

<t1>FirstName&LastName&Address1&Address2&</t1>
<t2>City&State&country&</t2>

您可以定义一个变量,例如 vrtfSplitResult,其主体是将模板应用于包含字符串的文本节点的结果。然后,如果您可以使用 xxx:node-set() 扩展,则您要定义的两个变量是:

<xsl:variable name="requiredString" 
     select="xxx:node-set($vrtfSplitResult)/*/t1">

<xsl:variable name="remainingString" 
     select="xxx:node-set($vrtfSplitResult)/*/t2">

如果您甚至不允许使用 xxx: node-set() 扩展函数,那么您应该使用类似的命名模板:getStartingString。这与上面 splitAt 模板的逻辑非常相似:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pSplitIndex" select="4"/>

 <xsl:template match="text()">
   <xsl:variable name="vrequiredString">
    <xsl:call-template name="getStartingString"/>
   </xsl:variable>
   <xsl:variable name="vremainingString" select=
    "substring-after(.,$vrequiredString)"/>

   <t>
     <t1><xsl:value-of select="$vrequiredString"/></t1>
     <t2><xsl:value-of select="$vremainingString"/></t2>
   </t>
 </xsl:template>

 <xsl:template name="getStartingString">
  <xsl:param name="pLeft" select="''"/>
  <xsl:param name="pRight" select="."/>
  <xsl:param name="pSplitAt" select="$pSplitIndex"/>
  <xsl:param name="pDelim" select="'&'"/>

  <xsl:choose>
    <xsl:when test=
     "not(string-length($pRight))
     or
      not(contains($pRight, $pDelim))
     ">
       <xsl:value-of select="$pLeft"/>
    </xsl:when>
    <xsl:otherwise>
     <xsl:choose>
      <xsl:when test="$pSplitAt = 0">
       <xsl:value-of select="$pLeft"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="getStartingString">
         <xsl:with-param name="pLeft" select=
          "concat($pLeft, substring-before($pRight, $pDelim), $pDelim)
          "/>
         <xsl:with-param name="pRight"
          select="substring-after($pRight, $pDelim)"/>
         <xsl:with-param name="pSplitAt" select="$pSplitAt -1"/>
         <xsl:with-param name="pDelim" select="$pDelim"/>
        </xsl:call-template>
      </xsl:otherwise>
     </xsl:choose>
    </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于同一个 XML 文档时,会再次生成所需的正确结果

<t>
    <t1>FirstName&LastName&Address1&Address2&</t1>
    <t2>City&State&country&</t2>
</t>

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pSplitIndex" select="4"/>

 <xsl:template match="text()" name="splitAt">
  <xsl:param name="pLeft" select="''"/>
  <xsl:param name="pRight" select="."/>
  <xsl:param name="pSplitAt" select="$pSplitIndex"/>
  <xsl:param name="pDelim" select="'&'"/>

  <xsl:choose>
    <xsl:when test=
     "not(string-length($pRight))
     or
      not(contains($pRight, $pDelim))
     ">
     <t1>
       <xsl:value-of select="concat($pLeft, $pRight)"/>
     </t1>
     <t2/>
    </xsl:when>
    <xsl:otherwise>
     <xsl:choose>
      <xsl:when test="$pSplitAt = 0">
       <t1><xsl:value-of select="$pLeft"/></t1>
       <t2><xsl:value-of select="$pRight"/></t2>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="splitAt">
         <xsl:with-param name="pLeft" select=
          "concat($pLeft, substring-before($pRight, $pDelim), $pDelim)
          "/>
         <xsl:with-param name="pRight"
          select="substring-after($pRight, $pDelim)"/>
         <xsl:with-param name="pSplitAt" select="$pSplitAt -1"/>
         <xsl:with-param name="pDelim" select="$pDelim"/>
        </xsl:call-template>
      </xsl:otherwise>
     </xsl:choose>
    </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

when applied on this XML document:

<t>FirstName&LastName&Address1&Address2&City&State&country&</t>

produces the required two strings that result from splitting /t/text() at the fourth & delimiter:

<t1>FirstName&LastName&Address1&Address2&</t1>
<t2>City&State&country&</t2>

You can define a variable, say vrtfSplitResult whose body is the result of applying templates to the text node containing the string. Then, if you can use the xxx:node-set() extension, the two variables you want to define are:

<xsl:variable name="requiredString" 
     select="xxx:node-set($vrtfSplitResult)/*/t1">

<xsl:variable name="remainingString" 
     select="xxx:node-set($vrtfSplitResult)/*/t2">

In case you are not allowed to use even the xxx:node-set() extension function, then you should use a similar, named template: getStartingString. This has pretty much the logic of the splitAt template above:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pSplitIndex" select="4"/>

 <xsl:template match="text()">
   <xsl:variable name="vrequiredString">
    <xsl:call-template name="getStartingString"/>
   </xsl:variable>
   <xsl:variable name="vremainingString" select=
    "substring-after(.,$vrequiredString)"/>

   <t>
     <t1><xsl:value-of select="$vrequiredString"/></t1>
     <t2><xsl:value-of select="$vremainingString"/></t2>
   </t>
 </xsl:template>

 <xsl:template name="getStartingString">
  <xsl:param name="pLeft" select="''"/>
  <xsl:param name="pRight" select="."/>
  <xsl:param name="pSplitAt" select="$pSplitIndex"/>
  <xsl:param name="pDelim" select="'&'"/>

  <xsl:choose>
    <xsl:when test=
     "not(string-length($pRight))
     or
      not(contains($pRight, $pDelim))
     ">
       <xsl:value-of select="$pLeft"/>
    </xsl:when>
    <xsl:otherwise>
     <xsl:choose>
      <xsl:when test="$pSplitAt = 0">
       <xsl:value-of select="$pLeft"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="getStartingString">
         <xsl:with-param name="pLeft" select=
          "concat($pLeft, substring-before($pRight, $pDelim), $pDelim)
          "/>
         <xsl:with-param name="pRight"
          select="substring-after($pRight, $pDelim)"/>
         <xsl:with-param name="pSplitAt" select="$pSplitAt -1"/>
         <xsl:with-param name="pDelim" select="$pDelim"/>
        </xsl:call-template>
      </xsl:otherwise>
     </xsl:choose>
    </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the same XML document, the wanted, correct result is produced again:

<t>
    <t1>FirstName&LastName&Address1&Address2&</t1>
    <t2>City&State&country&</t2>
</t>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文