简体   繁体   English

使用XSLT为XML中的相似节点创建通用模板

[英]Create a generic template for similar nodes in XML using XSLT

If I have an XML like this: 如果我有这样的XML:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <body1>
    <text1>this is a text</text1>
    <number1>1234,7</number1>
  </body1>
  <body2>
    <text2>this is a text</text2>
    <number2>1234,7</number2>
  </body2>
  <..>
</root>

How can I create a generic template for each <body> / <text> or <number> tags making it a generic one 如何为每个<body> / <text><number>标记创建通用模板,使其成为通用模板

不确定“泛型”在这种情况下的含义-也许是:

<xsl:template match="*[starts-with(name(), 'body')]">

How can I create a generic template for each <body>/<text> or <number> tags making it a generic one 如何为每个<body>/<text><number>标签创建通用模板,使其成为通用模板

There are many ways and which are applicable depends on (all possible instances of) the XML document to be processed, of which you have shown us just one: 有很多方法适用于您要处理的XML文档(所有可能的实例),其中只有一种:

<xsl:template 
 match="*[starts-with(name(), 'body') and not(string-length(name() > 6))]
            /*[starts-with(name(), 'text') and not(string-length(name() > 6))]">

and

<xsl:template 
 match="*[starts-with(name(), 'body') and not(string-length(name() > 6))]
            /*[starts-with(name(), 'number') and not(string-length(name() > 8))]">

Or, if you have just the shown XML document, you could use even : 或者,如果只有显示的XML文档,则可以使用甚至

<xsl:template 
 match="*[starts-with(name(), 'text') and not(string-length(name() > 6))]">

and

<xsl:template 
 match="*[starts-with(name(), 'number') and not(string-length(name() > 8))]">

Or even : 甚至

<xsl:template match="/*/*/*"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM