【问题标题】:XSLT works but i think i can make it simpler. Need AssistanceXSLT 有效,但我认为我可以让它更简单。需要帮助
【发布时间】:2021-07-13 09:30:27
【问题描述】:

我是一个新手和 xslt,我为我的 xml 到 XML 转换创建了一个 xslt,它对名为 catLineNum 的字段进行排序。

XSLT 代码

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    xmlns:map="http://www.w3.org/2005/xpath-functions/map"
    xmlns:array="http://www.w3.org/2005/xpath-functions/array"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:output method="xml" indent="yes"/>


    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
   
    
    <xsl:template match="category">
    <category><xsl:value-of select= "category"/></category>
    
    <xsl:variable name="min" select="min(//catLineNum)" />
    <catLineNum><xsl:value-of select="$min" /></catLineNum>
    
        <xsl:copy>
           
            <xsl:apply-templates select="categoryDescription">
                <xsl:sort select="catLineNum" data-type="number" />
            </xsl:apply-templates>
            
        </xsl:copy>
        
    </xsl:template>




</xsl:stylesheet>

输入xml

<?xml version='1.0' encoding='UTF-8'?>
<root>
  <model>
    <models>
      <key>18322022AMECHANICAL &amp; PERFORMANCE</key>
      <models>
        <category>
          <category>MECHANICAL &amp; PERFORMANCE</category>
          <categoryDescription>
            <categoryDescription>with Valvematic Technology, CVT</categoryDescription>
            <catLineNum>002</catLineNum>
          </categoryDescription>
          <categoryDescription>
            <categoryDescription>-15-in Styled Steel Wheels</categoryDescription>
            <catLineNum>003</catLineNum>
          </categoryDescription>
          <categoryDescription>
            <categoryDescription>-1.8L 4-cyl Engine</categoryDescription>
            <catLineNum>001</catLineNum>
          </categoryDescription>
        </category>
      </models>
    </models>
  </model>
</root>

输出

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <model>
      <models>
         <key>18322022AMECHANICAL &amp; PERFORMANCE</key>
         <models>
            <category>MECHANICAL &amp; PERFORMANCE</category>
            <catLineNum>1</catLineNum>
            <category>
               <categoryDescription>
                  <categoryDescription>-1.8L 4-cyl Engine</categoryDescription>
                  <catLineNum>001</catLineNum>
               </categoryDescription>
               <categoryDescription>
                  <categoryDescription>with Valvematic Technology, CVT</categoryDescription>
                  <catLineNum>002</catLineNum>
               </categoryDescription>
               <categoryDescription>
                  <categoryDescription>-15-in Styled Steel Wheels</categoryDescription>
                  <catLineNum>003</catLineNum>
               </categoryDescription>
            </category>
         </models>
      </models>
  </model>
</root>

问题是如果我不包含这部分代码

输出中将缺少类别字段。我觉得还有另一种方法可以对值进行排序并包含该类别字段。 感谢您的帮助。

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    使用 Saxon JS 2 或 Saxon HE 10 或任何 Saxon 9.8 或更高版本的 PE 或 EE,您也可以使用更高阶函数支持

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="3.0"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      exclude-result-prefixes="#all"
      expand-text="yes">
      
      <xsl:strip-space elements="*"/>
      <xsl:output indent="yes"/>
    
      <xsl:mode on-no-match="shallow-copy"/>
      
      <xsl:template match="models/category">
        <xsl:copy>
          <xsl:copy-of select="category"/>
          <catLineNum>{min(categoryDescription/catLineNum)}</catLineNum>
          <category>
            <xsl:apply-templates select="sort(categoryDescription, (), function($c) { xs:integer($c/catLineNum) })"/>
          </category>
        </xsl:copy>
      </xsl:template>
      
    </xsl:stylesheet>
    

    【讨论】:

      【解决方案2】:

      或者更深一层:

      <?xml version="1.0" encoding="UTF-8"?>
      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:math="http://www.w3.org/2005/xpath-functions/math"
        xmlns:map="http://www.w3.org/2005/xpath-functions/map"
        xmlns:array="http://www.w3.org/2005/xpath-functions/array"
        exclude-result-prefixes="#all"
        version="3.0">
        
        <xsl:mode on-no-match="shallow-copy"/>
        
        <!-- Not needed, since you are using <xsl:mode on-no-match="shallow-copy"/>
        <xsl:template match="@*|node()">
          <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
          </xsl:copy>
        </xsl:template>
        -->  
        
        <xsl:template match="category[category]">
          <xsl:copy>
            <xsl:copy-of select="category"/>
            <catLineNum>
              <xsl:value-of select="min(categoryDescription/catLineNum)" />
            </catLineNum>
            
            <xsl:apply-templates select="categoryDescription">
              <xsl:sort select="catLineNum" data-type="number" />
            </xsl:apply-templates>
            
          </xsl:copy>    
        </xsl:template>
        
      </xsl:stylesheet>
      

      【讨论】:

        【解决方案3】:

        怎么样:

        <xsl:stylesheet version="3.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" indent="yes"/>
        
        <xsl:mode on-no-match="shallow-copy"/>
        
        <xsl:template match="models/models">
            <xsl:copy>
                <xsl:copy-of select="category/category"/>
                <catLineNum>
                    <xsl:value-of select="min(category/categoryDescription/catLineNum)" />
                </catLineNum>
                <category>
                    <xsl:apply-templates select="category/categoryDescription">
                        <xsl:sort select="catLineNum" data-type="number"/>
                    </xsl:apply-templates>
                </category>
            </xsl:copy>
        </xsl:template>
        
        </xsl:stylesheet>
        

        【讨论】:

          猜你喜欢
          • 2011-10-30
          • 2022-01-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-11-30
          • 2022-01-04
          • 2022-11-24
          相关资源
          最近更新 更多