如何在单个 for-eachin XSLT 中选择多个节点 [英] How to select multiple nodes in single for-eachin XSLT

查看:23
本文介绍了如何在单个 for-eachin XSLT 中选择多个节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习 XSLT,但我以示例为最佳.我想执行一个简单的模式到模式转换.如何仅通过一次传递执行此转换(我当前的解决方案使用两次传递并丢失了客户的原始订单)?

I am trying to learn XSLT but I work best by example. I want to perform a trivial schema to schema transformation. How do I perform this transformation in only one pass (my current solution uses two passes and loses the original order of customers)?

来自:

<?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>

我已经以糟糕的方式解决了这个问题(我丢失了客户的订单,我认为我必须多次解析文件:

I already solved this a bad way (I lose the order of customers and I think that I have to parse the file multiple times:

<?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 构造,我只需要使用一个解析(每个解析一个)?

Please can someone help me out with the correct XSLT construct where I only have to use a single parse (only one for-each)?

谢谢,

克里斯

推荐答案

尽可能避免使用 是一个很好的 XSLT 实践强>.

It is a good XSLT practice to avoid using <xsl:for-each> as much as possible.

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

<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. 仅使用模板和 .

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

The use of the identity rule and its overriding wherever necessary. This is one of the most fundamental XSLT design pattern.

这篇关于如何在单个 for-eachin XSLT 中选择多个节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆