格式化XML文章的特殊要求

时间:2012-03-26 01:13:07

标签: xml formatting

我的客户要求非常特别的东西。我们以下面的XML为例:

<?xml version="1.0"?>
<article>
  <paragraph>
    <w p='0'>This</w>
    <w p='1'>is</w>
    <w p='2'>the</w>
    <w p='3'>first</w>
    <w p='4'>paragraph</w>
    <w p='p'>.</w>
  </paragraph>
  <paragraph>
    <w p='0'>This</w>
    <w p='1'>is</w>
    <w p='2'>the</w>
    <w p='3'>second</w>
    <w p='4'>paragraph</w>
    <w p='p'>.</w>
  </paragraph>
</article>

我的客户希望“折叠”所有单词,但不是我们技术人员所说的折叠。他们想要的是隐藏单词节点信息,除了单词本身,所以在它们的“折叠”含义之后,它应该看起来像:

<?xml version="1.0"?>
<article>
  <paragraph>This is the first paragraph.</paragraph>
  <paragraph>This is the second paragraph.</paragraph>
</article>

我所说的只是外观,而不是潜在的内容,不应该通过这种折叠来改变。 (编辑)但是,用户仍然可以选择一个单词,并更改其属性或单词本身(以某种方式通过应用程序界面),

我的问题是,是否有现成的库(我使用的是C#)或专门针对此类要求的应用程序?

感谢。

彼得

1 个答案:

答案 0 :(得分:1)

什么都没有&#34;特别&#34;在这个要求。它是每天使用XSLT数千次完成的常规处理。

很难从您的示例中推断出转换规则,特别是要确切地知道应该插入空格的位置。您在每个单词之前插入了一个空格,除了第一个单词和刚刚包含的单词#34;。#34;。如果这确实是规则,那么您的转换将如下所示:

<xsl:transform match="paragraph">
 <paragraph><xsl:apply-templates/></paragraph>
</xsl:transform>

<xsl:template match="w[1]" priority="3">
  <xsl:value-of select="."/>
</xsl:template>

<xsl:template match="w[.='.']" priority="2">
  <xsl:value-of select="."/>
</xsl:template>

<xsl:template match="w" priority="1">
  <xsl:value-of select="concat(' ', .)"/>
</xsl:template>

在实践中,您可能需要针对数据中出现的其他内容制定其他规则。