使用 XSLT 1.0 为提取的 XSLT 标记添加分隔符 [英] Add separator to extracted XSLT tags with XSLT 1.0

查看:27
本文介绍了使用 XSLT 1.0 为提取的 XSLT 标记添加分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从输入文件中提取标签,并希望在标签之间添加普通分隔符(然后 xml 不再有效).给定文件:

I am extracting tags from an input file and want to add plain separators between the tags (the xml is then not valid any more). Given file:

<?xml version="1.0" encoding="UTF-8"?>
<A:A xsi:schemaLocation="urn:A A.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:A="urn:A">
    <A:B>
        <C xmlns="urn:C">
            <D>foo</D>
        </C>
    </A:B>
    <A:B>
        <C xmlns="urn:C">
            <D>bar</D>
        </C>
    </A:B>
</A:A>

想要的结果:

<?xml version="1.0" encoding="UTF-8"?>
<C xmlns="urn:C">
    <D>foo</D>
</C>
#SEPARATOR#
<C xmlns="urn:C">
    <D>bar</D>
</C>

我坚持要得到想要的结果.我的变压器:

I am stuck to get the desired result. My transformer:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="/">
      <xsl:apply-templates select="*/*/node()"/>
  </xsl:template>
  
  <xsl:template match="*">
      <xsl:element name="{name()}" namespace="{namespace-uri()}">
          <xsl:apply-templates select="@* | node()"/>
      </xsl:element>
  </xsl:template>

</xsl:stylesheet>

生产

<?xml version="1.0" encoding="utf-16"?>
        <C xmlns="urn:C">
            <D>foo</D>
        </C>
    
        <C xmlns="urn:C">
            <D>bar</D>
        </C>

任何想法如何添加 #SEPARATOR# 行?

Any ideas how to add the #SEPARATOR# line?

推荐答案

这个例子太做作了.也许你可以这样做:

The example is too contrived. Perhaps you could do:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/*">
    <xsl:apply-templates select="*/*"/>
</xsl:template>

<xsl:template match="*">
    <xsl:element name="{name()}" namespace="{namespace-uri()}">
        <xsl:apply-templates/>
    </xsl:element>
    <xsl:if test="position()!=last()">&#10;#SEPARATOR#&#10;</xsl:if>
</xsl:template>
    
</xsl:stylesheet>

问题是当元素 C 或其任何后代有多个子元素时,结果应该是什么.

The question is what should the result be when element C or any of its descendants has more than one child.

这篇关于使用 XSLT 1.0 为提取的 XSLT 标记添加分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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