1

我经常得到一些未按预期格式化的 XML,并且正在寻找自动修复它的最佳方法。不幸的是,解决方案是在我头上滑冰。

我正在研究杂志内容,并且很难处理两个特定元素。

There are <subhead> elements, and <body> elements. Even though the subhead element should always be on it's own, sometimes the proofer will accidentally nest it with a <body> node.

<subhead> nodes should be formatted as their own paragraph, wrapped in <p> and <strong> tags.

<body> nodes should just be wrapped in <p> tags.

So I could get either:
<subhead>Dogs</subhead>
<body>Dogs do not like cats.</body>
or
<body><subhead>Dogs</subhead> Dogs do not like cats.</body>

I would like either scenario to output as:
<p><strong>Dogs</strong></p>
<p>Dogs do not like cats.</p>

目前,我的代码看起来像..

<xsl:for-each select="//default:textObject/default:text/*">
<xsl:for-each select="./*">

<xsl:choose>

<xsl:when test="@name='subhead'">
<p><strong>
<xsl:apply-templates select="node()"/>
</strong></p>
</xsl:when>

<xsl:when test="@name='body'">
<p>
<xsl:apply-templates select="node()"/>
</p>
</xsl:when>

...

</xsl:choose>
</xsl:for-each>
</xsl:for-each>

我怎样才能相应地调整它来解决这个问题?

谢谢你。

4

2 回答 2

2

尝试为不可预测的传入数据结构编写 XSLT 通常是不可取的。如果出现其他嵌套错误怎么办?您的时间最好花在添加验证层上。在其最简单的形式中,这可能只是一个 DTD/Schema 表,校对者必须通过它运行他们的 XML。

尽管如此,要回答你的问题,试试这个。我假设每个body/subhead配对都在一个公共元素 ( item) 内,但你没有说。(否则,subhead标签本身在哪里,你怎么知道哪个subhead与哪个相关body- 它总是前面/后面的兄弟姐妹吗?)

XML

<root>
    <item>
        <subhead>Dogs</subhead>
        <body>Dogs do not like cats.</body>
    </item>
    <item>
        <body><subhead>Dogs</subhead> Dogs do not like cats.</body>
    </item>
</root>

XSL:

<!-- root and static content -->
<xsl:template match="/">
    <xsl:apply-templates select='root/item/body' />
</xsl:template>

<!-- iteration content - subhead/body pairings (matching 'body' nodes) -->
<xsl:template match='body'>
    <p><strong><xsl:value-of select='parent::*/subhead | subhead' /></strong></p>
    <p><xsl:value-of select='text()' /></p>
</xsl:template>

您可以在此 XMLPlayground 会话中运行它。

于 2012-06-25T20:21:10.310 回答
1

这个简短而简单的完整转换

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

 <xsl:template match="/*">
  <xsl:apply-templates select="(subhead | body/subhead)/text()"/>
  <xsl:text>&#xA;</xsl:text>
  <xsl:apply-templates select="body/text()"/>
 </xsl:template>

 <xsl:template match="subhead/text()">
  <p><strong><xsl:value-of select="."/></strong></p>
 </xsl:template>

 <xsl:template match="body/text()">
  <p><xsl:value-of select="."/></p>
 </xsl:template>
</xsl:stylesheet>

应用于以下 XML 文档时:

<t>
    <subhead>Dogs</subhead>
    <body>Dogs do not like cats.</body>
</t>

产生想要的结果:

<p><strong>Dogs</strong></p>
<p>Dogs do not like cats.</p>

当相同的转换应用于第二种类型的文档时:

<t>
    <body><subhead>Dogs</subhead> Dogs do not like cats.</body>
</t>

再次产生同样想要的正确结果:

<p><strong>Dogs</strong></p>
<p> Dogs do not like cats.</p>

解释

特定模板的结果出现在输出中的顺序不取决于匹配节点的顺序,而是取决于<xsl:apply-templates>导致选择执行模板的相应指令的顺序。

请注意

如果文档的结构真的不知道,只需在上面的代码中替换:

  <xsl:apply-templates select="(subhead | body/subhead)/text()"/>

和:

  <xsl:apply-templates select="(//subhead | //body/subhead)/text()"/>
于 2012-06-26T02:25:07.723 回答