English 中文(简体)
解析xml以跳过标记
原标题:parsing xml to skip tags
  • 时间:2011-02-10 03:16:41
  •  标签:
  • xslt
  • xpath

我有以下样式表来跳过标记XYZ_1、XYZ_2。

  • How do i get it to work for XYZ_*
  • Also, in the output the tags that are skipped have empty lines, how do i suppress them.

提前谢谢。

<?xml version="1.0" ?>

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

<xsl:output method="html" indent="no" encoding="utf-8" omit-xml-declaration="yes" />

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

<xsl:template match="XYZ_1" />
<xsl:template match="XYZ_2" />

</xsl:stylesheet>

以下是示例XML

<?xml version="1.0" encoding="UTF-8"?>
<tags>
    <tag>
        <tag1>TagName1</tag1>
        <XYZ_1>
            <name>1.pdf</name>
        </XYZ_1>
        <XYZ_2>
            <c_name>chart1.gif</c_name>
        </XYZ_2>
    </tag>
</tags>

输出结果是

<?xml version="1.0" encoding="UTF-8"?>
<tags>
    <tag>
        <tag1>TagName1</tag1>





    </tag>
</tags>
最佳回答

使用

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

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

 <xsl:template match="*[starts-with(name(),  XYZ_ )]"/>
</xsl:stylesheet>

应用于所提供的XML文档时

<?xml version="1.0" encoding="UTF-8"?>
<tags>
    <tag>
        <tag1>TagName1</tag1>
        <XYZ_1>
            <name>1.pdf</name>
        </XYZ_1>
        <XYZ_2>
            <c_name>chart1.gif</c_name>
        </XYZ_2>
    </tag>
</tags>

生成所需的正确结果

<tags>
   <tag>
      <tag1>TagName1</tag1>
   </tag>
</tags>

解释

  1. 标识规则(除非被覆盖)按原样复制每个节点

  2. 唯一覆盖的模板是匹配元素,其name()以()“XYZ_”开头,并且该模板的正文为空——这将有效地从输出中删除任何匹配的元素。

  3. <;xsl:step-space elements=“*”/>指令指示XSLT处理器解析XML文档,忽略文档中任何仅限空白的文本节点。因此,转换中看不到只有空白的节点,因此不会将此类节点复制到输出中。这消除了不需要的空白,报告称这是第二个问题。

问题回答

我相信您会使用开头,如下所示:

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

这应该将模板应用于以指定的四个字符开头的任何元素。

从提供的关于你问题第二部分的信息来看,不确定。你能发布一个正在分析的文件的样本吗?





相关问题
When test hanging in an infinite loop

I m tokenising a string with XSLT 1.0 and trying to prevent empty strings from being recognised as tokens. Here s the entire function, based on XSLT Cookbook: <xsl:template name="tokenize"> ...

quick xslt for-each question

Let s say I have an XML document that has this: <keywords> <keyword>test</keyword> <keyword>test2</keyword> <keyword>test3</keyword> <keyword>test4</...

XSLT Transform XML with Namespaces

I m trying to transform some XML into HTML using XSLT. Problem: I can t get it to work. Can someone tell me what I m doing wrong? XML: <ArrayOfBrokerage xmlns:i="http://www.w3.org/2001/...

XSLT output to HTML

In my XSLT file, I have the following: <input type="button" value= <xsl:value-of select="name">> It s an error as it violates XML rule. What I actually want is having a value from an ...

Mangling IDs and References to IDs in XML

I m trying to compose xml elements into each other, and the problem I am having is when there s the same IDs. Basically what I need to do is mangle all the IDs in an xml file, as well as the ...

Sharepoint 2007 Data view Webpart custom parameters

I m sort of new to the custom parameters that can be setup on a DataView Webpart. There are 6 options: - None - Control - Cookie - Form - QueryString - Server Variable I think that None, Cookie and ...

热门标签