【问题标题】:Sort the XML based on node value using <xsl:sort> and change the element name issue使用 <xsl:sort> 根据节点值对 XML 进行排序并更改元素名称问题
【发布时间】:2014-05-30 11:54:49
【问题描述】:

我尝试根据给定 xml 的值对 XML 进行排序。从请求 xml 我需要根据education_details {DR,PDR,MSC,BSC}进行排序。我有用。请参阅下面的示例。

输入 xml

<?xml version="1.0" encoding="UTF-8"?>
<root>
       <document>
         <studentname>ACM</studentname>
         <educational_details>MSC</educational_details>
       </document>
       <document>
         <studentname>ACB</studentname>
         <educational_details>BSc</educational_details>
       </document>
       <document>
         <studentname>ACP</studentname>
         <educational_details>PDR</educational_details>
       </document>
       <document>
         <studentname>ACC</studentname>
         <educational_details>DR</educational_details>
       </document>
</root>

转换 XSLT

<xsl:stylesheet version="2.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:param name="pOrder" select="'DR,PDR,MSC,BSc'" />

        <xsl:variable name="vSequence" select="tokenize($pOrder, ',')"/>

        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:template>

        <xsl:template match="root">
            <xsl:copy>
                <xsl:apply-templates select="document">
                    <xsl:sort
                        select="index-of($vSequence, educational_details)" />
                </xsl:apply-templates>
            </xsl:copy>
        </xsl:template>

    </xsl:stylesheet>

查找 xml

<?xml version="1.0" encoding="UTF-8"?>
<lookups>
    <lookup tc="13" responce="Doctor" request="DR" />   
    <lookup tc="12" responce="Post DR" request="PDR" />
    <lookup tc="30" responce="Master of Scienc" request="MSc" />    
    <lookup tc="4" responce="Bachelor of Science" request="BSc" />
</lookups>

输出xml

  <?xml version="1.0" encoding="UTF-8"?>
        <root>
               <document>
                 <studentname>ACM</studentname>
                 <stu_educational_details tc="30">Master of Scienc</stu_educational_details>
               </document>
               <document>
                 <studentname>ACB</studentname>
                 <stu_educational_details tc="4">Bachelor of Science</stu_educational_details>
               </document>
               <document>
                 <studentname>ACP</studentname>
                 <stu_educational_details tc="12">Post DR</stu_educational_details>
               </document>
               <document>
                 <studentname>ACC</studentname>
                 <stu_educational_details tc="13">Doctor</stu_educational_details>
               </document>
        </root>

预期输出

 <?xml version="1.0" encoding="UTF-8"?>
            <root>
                  <document>
                     <studentname>ACC</studentname>
                     <stu_educational_details tc="13">Doctor</stu_educational_details>
                   </document>
                   <document>
                     <studentname>ACP</studentname>
                     <stu_educational_details tc="12">Post DR</stu_educational_details>
                   </document>
                   <document>
                     <studentname>ACM</studentname>
                     <stu_educational_details tc="30">Master of Scienc</stu_educational_details>
                   </document>
                   <document>
                     <studentname>ACB</studentname>
                     <stu_educational_details tc="4">Bachelor of Science</stu_educational_details>
                   </document>
              </root>

我编写了更改元素和值名称的逻辑。

【问题讨论】:

  • 使用样式表转换的 Inout 文件不会产生输出文件的确切结构。你能展示你的实际 XSLT 吗?不过,使用样式表转换输入文件确实会提供正确的顺序......因此,要么在运行转换时覆盖 pOrder 参数,要么在实际样式表中存在另一个问题。
  • 我认为覆盖 pOrder 参数的问题。

标签: xml xslt xslt-2.0


【解决方案1】:

您发布的样式表是我建议作为对您之前问题的回应的样式表,它会进行排序,但不会产生您想要的重命名和替换。

重命名很容易添加:

<xsl:stylesheet version="2.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:param name="pOrder" select="'DR,PDR,MSC,BSc'" />

        <xsl:variable name="vSequence" select="tokenize($pOrder, ',')"/>

        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:template>

        <xsl:template match="root">
            <xsl:copy>
                <xsl:apply-templates select="document">
                    <xsl:sort
                        select="index-of($vSequence, educational_details)" />
                </xsl:apply-templates>
            </xsl:copy>
        </xsl:template>

        <xsl:template match="educational_details">
          <stu_educational_details>
            <xsl:value-of select="."/>
          </stu_educational_details>
        </xsl:template>

    </xsl:stylesheet>

至于添加属性和替换"DR"之类的代码,您至少需要告诉我们tc数字之类的数据和"Doctor"之类的替换字符串来自哪里。通常人们会使用查找文档并简单地使用键来查找值。

这是一个带有用于查找的键的编辑:

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

        <xsl:param name="lookup-url" select="'test2014053002.xml'"/>

        <xsl:variable name="lookup-doc" select="doc($lookup-url)"/>

        <xsl:param name="pOrder" select="'DR,PDR,MSC,BSc'" />

        <xsl:variable name="vSequence" select="tokenize($pOrder, ',')"/>

        <xsl:key name="codes" match="lookup" use="@request"/>

        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:template>

        <xsl:template match="root">
            <xsl:copy>
                <xsl:apply-templates select="document">
                    <xsl:sort
                        select="index-of($vSequence, educational_details)" />
                </xsl:apply-templates>
            </xsl:copy>
        </xsl:template>

        <xsl:template match="educational_details">
          <stu_educational_details tc="{key('codes', ., $lookup-doc)/@tc}">
            <xsl:value-of select="key('codes', ., $lookup-doc)/@responce"/>
          </stu_educational_details>
        </xsl:template>

    </xsl:stylesheet>

请注意,目前您的样本与往常一样使用不同的拼写(responce 而不是response)以及request 属性值和一些样本数据中不同的字母大小写,您可能需要对拼写进行规范化或您需要添加 &lt;xsl:key name="codes" match="lookup" use="upper-case(@request)"/&gt; 和例如key('codes', upper-case(.), $lookup-doc).

【讨论】:

  • 正确。我已经使用查找 xml 编辑了问题。你能检查一下吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-21
  • 2021-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多