草庐IT

xml - xsl :sort with imperial measurements

coder 2024-06-30 原文

我正在尝试制作一个 xslt 文件来对 InDesign 的列表进行排序。

我遇到的问题是,当尺寸为英制而不是公制时,我试图让列表按预期排序。更复杂的是,大小信息包含在一个字符串中。

这是 xslt:

<CATEGORY>
  <VERS>
    <xsl:for-each select="VFPData/g_otemp/prodid">
      <xsl:sort select="floor(translate(../desc,$vDigits,''))" />
      <xsl:sort select="translate(../desc,$vAlpha,'')" data-type="number"/>

        <xsl:if test="../inactive != 'true'">
          <code><xsl:value-of select="../prodid" /></code>       
          <name><xsl:value-of select="../desc" /></name>
          <xsl:if test="../priceout != '0.0000'">
            <price><xsl:value-of select="format-number(../priceout, '£0.00')" /></price>
          </xsl:if>
          <xsl:if test="../priceout = '0.0000'">
            <price>P.O.A</price>
          </xsl:if>
        </xsl:if>
    </xsl:for-each>
  </VERS>
</CATEGORY>

这在处理公制尺寸时效果很好,但是对于英制尺寸,您会看到这样的排序:

<VERS>
    <code>BM50-100</code>
    <name>Blue-Max Joint 16" x 12" x 1"</name>
    <price>£31.82</price>
    <code>BM50-106</code>
    <name>Blue-Max Joint 11" x 8 1/2" x 1"</name>
    <price>£24.33</price>
    <code>BM50-123</code>
    <name>Blue-Max Joint 2 1/2" x 2" x 1/2"</name>
    <price>£7.42</price>
    <code>BM50-133</code>
    <name>Blue-Max Joint 2 3/4" x 2" x 1/2"</name>
</VERS>

它应该在哪里:

<VERS>
    <code>BM50-123</code>
    <name>Blue-Max Joint 2 1/2" x 2" x 1/2"</name>
    <price>£7.42</price>
    <code>BM50-133</code>
    <name>Blue-Max Joint 2 3/4" x 2" x 1/2"</name>
    <code>BM50-106</code>
    <name>Blue-Max Joint 11" x 8 1/2" x 1"</name>
    <price>£24.33</price>
    <code>BM50-100</code>
    <name>Blue-Max Joint 16" x 12" x 1"</name>
    <price>£31.82</price>
</VERS>

大概是因为它排序的值是字符串中所有数字的组合,这意味着 '2 3/4"x 2"x 1/2"' = 234212 使其确实大于 '16 "x 12"x 1"' (16121)。

我试图将它隔离到第一个数字(所以在'Joint'和'"'之间)并且只有当第一个数字没有分数时才真正起作用。

我的另一个想法是可以让 xslt 将分数转换为小数吗?

我到底该如何使用 XSLT 对其进行排序?

最佳答案

免责声明:这完全是疯了。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="name">
    <p>
      <xsl:apply-templates select="." mode="sort" />
    </p>
  </xsl:template>

  <!-- finds the first digit in a name and then extracts exactly thee numerical values -->
  <xsl:template match="name" mode="sort">
    <xsl:param name="rest" select="string(.)" />

    <xsl:variable name="left" select="substring($rest, 1, 1)" />
    <xsl:variable name="by" select="' x '" />

    <xsl:if test="$left">
      <xsl:choose>
        <xsl:when test="number($left)">
          <xsl:variable name="s1" select="substring-before($rest, $by)" />
          <xsl:variable name="s2" select="substring-before(substring-after($rest, $by), $by)" />
          <xsl:variable name="s3" select="substring-after($rest, concat($s1, $by, $s2, $by))" />
          <xsl:variable name="n1">
            <xsl:call-template name="sanitize"><xsl:with-param name="str" select="$s1" /></xsl:call-template>
          </xsl:variable>
          <xsl:variable name="n2">
            <xsl:call-template name="sanitize"><xsl:with-param name="str" select="$s2" /></xsl:call-template>
          </xsl:variable>
          <xsl:variable name="n3">
            <xsl:call-template name="sanitize"><xsl:with-param name="str" select="$s3" /></xsl:call-template>
          </xsl:variable>
          <xsl:value-of select="concat(
            format-number($n1, '000.00'), ' - ',
            format-number($n2, '000.00'), ' - ',
            format-number($n3, '000.00')
          ) " />
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="." mode="sort">
            <xsl:with-param name="rest" select="substring-after($rest, $left)" />
          </xsl:apply-templates>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:if>
  </xsl:template>

  <!-- converts strings of one full and one factional number ("2 1/4") to a decimal number (2.25) -->
  <xsl:template name="sanitize">
    <xsl:param name="str" />

    <xsl:variable name="bare" select="translate($str, '&quot;', '')" />
    <xsl:variable name="full">
      <xsl:choose>
        <xsl:when test="contains($bare, '/')">
          <xsl:value-of select="substring-before($bare, ' ')" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$bare" />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="frac" select="substring-after($bare, $full)" />
    <xsl:variable name="fullNum">
      <xsl:choose>
        <xsl:when test="number($full)">
          <xsl:value-of select="number($full)" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="0" />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="fracNum">
      <xsl:choose>
        <xsl:when test="$frac">
          <xsl:variable name="q" select="number(substring-before($frac, '/'))" />
          <xsl:variable name="d" select="number(substring-after($frac, '/'))" />
          <xsl:choose>
            <xsl:when test="$q and $d">
              <xsl:value-of select="$q div $d" />
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="number(concat('0.', $q))" /><!-- this is debatable -->
            </xsl:otherwise>
          </xsl:choose>
        </xsl:when>
        <xsl:otherwise><xsl:value-of select="0" /></xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:value-of select="$fullNum + $fracNum" />
  </xsl:template>
</xsl:stylesheet>

应用于

<test>
    <name>Blue-Max Joint 16" x 12" x 1"</name>
    <name>Blue-Max Joint 11" x 8 1/2" x 1"</name>
    <name>Blue-Max Joint 2 1/2" x 2" x 1/2"</name>
    <name>Blue-Max Joint 2 3/4" x 2" x 1/2"</name>
</test>

给你

<p>016.00 - 012.00 - 001.00</p>
<p>011.00 - 008.50 - 001.00</p>
<p>002.50 - 002.00 - 000.50</p>
<p>002.75 - 002.00 - 000.50</p>

您现在可以使用这些字符串进行排序。即使是三向排序也可以。

但是您需要做一些额外的工作,可能涉及 node-set() 扩展函数,才能真正使其在您的样式表中可用。

当然,只要输入格式稍有变化,这就会崩溃。


以上可以作为一个例子,说明为什么以强调值和结构的数据格式存储格式化的、不透明的字符串是最终的坏主意。

关于xml - xsl :sort with imperial measurements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19894991/

有关xml - xsl :sort with imperial measurements的更多相关文章

  1. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  2. ruby-on-rails - 如何在 Rails 3 中禁用 XML 解析 - 2

    我想禁用HTTP参数的自动XML解析。但我发现命令仅适用于Rails2.x,它们都不适用于3.0:config.action_controller.param_parsers.deleteMime::XML(application.rb)ActionController::Base.param_parsers.deleteMime::XMLRails3.0中的等价物是什么? 最佳答案 根据CVE-2013-0156的最新安全公告你可以将它用于Rails3.0。3.1和3.2ActionDispatch::ParamsParser::

  3. ruby - 如何使用 Nokogiri::XML::Builder 生成动态标签? - 2

    我正在遍历数组中的一组标签名称,我想使用构建器打印每个标签名称,而不是求助于“我认为:builder=Nokogiri::XML::Builder.newdo|xml|fortagintagsxml.tag!tag,somevalendend会这样做,但它只是创建名称为“tag”的标签,并将标签变量作为元素的文本值。有人可以帮忙吗?这个看起来应该比较简单,我刚刚在搜索引擎上找不到答案。我可能没有以正确的方式提问。 最佳答案 尝试以下操作。如果我没记错的话,我添加了一个根节点,因为Nokogiri需要一个。builder=Nokogi

  4. ruby - 如何让 Nokogiri 解析并返回 XML 文档? - 2

    这是一些奇怪的例子:#!/usr/bin/rubyrequire'rubygems'require'open-uri'require'nokogiri'print"withoutread:",Nokogiri(open('http://weblog.rubyonrails.org/')).class,"\n"print"withread:",Nokogiri(open('http://weblog.rubyonrails.org/').read).class,"\n"运行此返回:withoutread:Nokogiri::XML::Documentwithread:Nokogiri::

  5. ruby - 模式加载时出现 Nokogiri::XML::Schema SyntaxError - 2

    我正在尝试加载SAML协议(protocol)架构(具体来说:https://www.oasis-open.org/committees/download.php/3407/oasis-sstc-saml-schema-protocol-1.1.xsd),但在执行此操作之后:schema=Nokogiri::XML::Schema(File.read('saml11_schema.xsd'))我得到这个输出:Nokogiri::XML::SyntaxErrorException:Element'{http://www.w3.org/2001/XMLSchema}element',att

  6. ruby-on-rails - 来自 cucumber 的 HTTP POST XML 内容 - 2

    我正在尝试通过POST将XML内容发送到一个简单的Rails项目中的Controller(“解析”)方法(“索引”)。它不是RESTful,因为我的模型名称不同,比如“汽车”。我在有效的功能测试中有以下内容:deftest_index...data_file_path=File.dirname(__FILE__)+'/../../app/views/layouts/index.xml.erb'message=ERB.new(File.read(data_file_path))xml_result=message.result(binding)doc=REXML::Document.ne

  7. ruby - 如何使用 XPath 和 Nokogiri 获取 XML 节点的内容 - 2

    我有这样的代码:@doc=Nokogiri::HTML(open(url)@doc.xpath(query).eachdo|html|putshtml#howgetcontentofanodeend我如何获取节点的内容而不是像这样: 最佳答案 这是READMEfile中的概要示例为Nokogiri展示了一种使用CSS、XPath或混合的方法:require'nokogiri'require'open-uri'#GetaNokogiri::HTML:Documentforthepagewe’reinterestedin...doc=N

  8. ruby - 使用 Ruby 向网络服务器发送 XML 请求 - 2

    恐怕我没有太多通过网络服务器发布文档(例如XML)的经验,所以如果我对HTTP的理解不足,我深表歉意。我在127.0.0.1上的ruby​​应用程序中设置了一个基本的MongrelWeb服务器端口2000.(服务器)。我在同一台计算机上运行一个单独的Ruby应用程序。(客户)。我需要客户端向服务器发送XML文档。我曾尝试使用Net::HTTP来执行此操作,但我找不到一个明确的示例来告诉我应该做什么。我试过了,但遇到了错误。我已将请求分解,使其尽可能基本:http=Net::HTTP.new("127.0.0.1",2000)http.post('file','query=foo')#x

  9. ruby - 如何使用 Sinatra 提供 XML 文档? - 2

    我有一些XML文档,我想从Sinatra服务器获取这些文档。我做了一些搜索,但找不到任何具体的东西。我确实找到了构建器gem,但我不想从头开始构建文档。我试着做这样的事情get'/'xml='Myname90'bodyxmlend但这会在它周围添加HTML标签。这可能是我所缺少的非常基本的东西。你能给我指出正确的方向吗? 最佳答案 这对于Sinatra来说非常简单:get'/'docontent_type'text/xml'"Luis99"end在获取“/”时,响应将是XML"Luis99"使用正确的content_type。

  10. ruby - 尝试使用 nokogiri 获取 xml 文件中 cdata 标签内的内容 - 2

    我已经看到了几件事,但到目前为止似乎没有任何效果。我正在使用nokogirionrails3ruby​​1.9.2通过url解析xml。xml的片段如下所示:我正在尝试解析它以获取与NewsLineText关联的文本r=node.at_xpath('.//newslinetext')ifnode.at_xpath('.//newslinetext')s=node.at_xpath('.//newslinetext').textifnode.at_xpath('.//newslinetext')t=node.at_xpath('.//newslinetext').contentifnod

随机推荐