代码之家  ›  专栏  ›  技术社区  ›  Chris

如何为每个XSLT选择单个中的多个节点

  •  6
  • Chris  · 技术社区  · 15 年前

    我试图学习XSLT,但我的工作最好以身作则。我想执行一个简单的模式到模式转换。如何仅在一次过程中执行此转换(我当前的解决方案使用两次过程并丢失客户的原始订单)?

    来自:

    <?xml version="1.0" encoding="UTF-8"?>
    <sampleroot>
    
    <badcustomer>
        <name>Donald</name>
        <address>Hong Kong</address>
        <age>72</age>
    </badcustomer>
    
    <goodcustomer>
        <name>Jim</name>
        <address>Wales</address>
        <age>22</age>
    </goodcustomer>
    
    <goodcustomer>
        <name>Albert</name>
        <address>France</address>
        <age>51</age>
    </goodcustomer>
    
    </sampleroot>
    

    到:

    <?xml version="1.0" encoding="UTF-8"?>
    <records>
    
    <record id="customer">
        <name>Donald</name>
        <address>Hong Kong</address>
        <age>72</age>
        <customertype>bad</customertype>
    </record>
    
    <record id="customer">
        <name>Jim</name>
        <address>Wales</address>
        <age>22</age>
        <customertype>good</customertype>
    </record>
    
    <record id="customer">
        <name>Albert</name>
        <address>France</address>
        <age>51</age>
        <customertype>good</customertype>
    </record>
    
    </records>
    

    我已经解决了这个A 坏的 方式(我失去了客户的订单,我认为我必须多次分析该文件:

    <?xml version='1.0'?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
        <xsl:output method="xml" indent="yes"/>
        <xsl:template match="/sampleroot">
    
        <records>
    
            <xsl:for-each select="goodcustomer">
                <record id="customer">
                    <name><xsl:value-of select="name" /></name>
                    <address><xsl:value-of select="address" /></address>
                    <age><xsl:value-of select="age" /></age>
                    <customertype>good</customertype>
                </record>
            </xsl:for-each>
    
            <xsl:for-each select="badcustomer">
                <record id="customer">
                    <name><xsl:value-of select="name" /></name>
                    <address><xsl:value-of select="address" /></address>
                    <age><xsl:value-of select="age" /></age>
                    <customertype>bad</customertype>
                </record>
            </xsl:for-each>
    
        </records>
        </xsl:template>
    </xsl:stylesheet>
    

    如果我只需要使用一个解析(每个解析只有一个),请有人能帮我找到正确的XSLT结构吗?

    谢谢,

    克里斯

    1 回复  |  直到 15 年前
        1
  •  7
  •   Dimitre Novatchev    15 年前

    避免使用 <xsl:for-each> 尽可能多地 .

    下面是一个简单的解决方案,使用这个原理 :

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="/*">
      <records>
        <xsl:apply-templates/>
      </records>
     </xsl:template>
    
     <xsl:template match="badcustomer | goodcustomer">
      <record>
       <xsl:apply-templates/>
       <customertype>
         <xsl:value-of select="substring-before(name(), 'customer')"/>
       </customertype>
      </record>
     </xsl:template>
    </xsl:stylesheet>
    

    做笔记 :

    1. 仅模板和 <xsl:apply-templates> 被使用。

    2. 身份规则的使用及其在必要时的覆盖。这是最基本的XSLT设计模式之一。