基于规则的节点重组 [英] Rule-based restructuring of nodes
问题描述
假设我有以下 XML 文件,其中包含我想根据规则重新排列的节点:
Supposed I have the following XML file with nodes I would like to rearrange based on rules:
<root>
<subsection key="KeyR">Some text</subsection>
<subsection key="KeyC">Some text</subsection>
<subsection key="KeyE">Some text</subsection>
<subsection key="KeyG">Some text</subsection>
<subsection key="KeyH">Some text</subsection>
<subsection key="KeyI">Some text</subsection>
<subsection key="KeyF">Some text</subsection>
<subsection key="KeyJ">Some text</subsection>
<subsection key="KeyL">Some text</subsection>
<subsection key="KeyA"/>
<subsection key="KeyM">Some text</subsection>
<subsection key="KeyN">Some text</subsection>
<subsection key="KeyO">Some text</subsection>
<subsection key="KeyS">Some text</subsection>
<subsection key="KeyP">Some text</subsection>
<subsection key="KeyQ">Some text</subsection>
<subsection key="KeyD">Some text</subsection>
<subsection key="KeyB"/>
<subsection key="KeyT">Some text</subsection>
<subsection key="KeyK">Some text</subsection>
<subsection key="KeyZ">Some text</subsection>
</root>
和重排规则如下:
section01
KeyA
KeyM
KeyZ
section02
KeyL
KeyN
KeyP
section03
..
..
section04
..
这些规则将子节的键分配给新的父节.因此将产生以下 XML 文件:
These rules assign the key of a subsection to a new parend section. So that the following XML file would result:
<root>
<section1>
<subsection key="KeyA"/>
<subsection key="KeyM">Some text</subsection>
<subsection key="KeyZ">Some text</subsection>
</section1>
<section2>
<subsection key="KeyL">Some text</subsection>
<subsection key="KeyN">Some text</subsection>
<subsection key="KeyP">Some text</subsection>
</section2>
<section3>
...
</section3>
...
</root>
XSL 转换是合适的方法吗?这样的转变看起来如何?什么是规则的适当体现,以便规则可以轻松维护?
Would be an XSL transformation the appropriate means? How would such a transformation look? What would be an appropriate manifestation for the rules so that the rules can be maintained easily?
推荐答案
如果您有一个 rules.xml 文件(例如)与 XSLT 样式表文件位于同一目录中,则(例如)格式如下:
If you could have a rules.xml file living (for example) in the same directory as the XSLT stylesheet file, in (for example) the following format:
rules.xml
<?xml version="1.0" encoding="UTF-8"?>
<rules>
<section id="01">
<key>KeyA</key>
<key>KeyM</key>
<key>KeyZ</key>
</section>
<section id="02">
<key>KeyL</key>
<key>KeyN</key>
<key>KeyP</key>
</section>
</rules>
然后您可以将以下样式表应用于您的输入:
you could then apply the following stylesheet to your input:
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:key name="sub" match="subsection" use="@key" />
<xsl:variable name="root" select="/" />
<xsl:template match="/">
<root>
<xsl:for-each select="document('rules.xml')/rules/section">
<xsl:variable name="keys" select="key" />
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:for-each select="$root">
<xsl:copy-of select="key('sub', $keys)"/>
</xsl:for-each>
</xsl:copy>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
得到如下结果:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<section id="01">
<subsection key="KeyA"/>
<subsection key="KeyM">Some text</subsection>
<subsection key="KeyZ">Some text</subsection>
</section>
<section id="02">
<subsection key="KeyL">Some text</subsection>
<subsection key="KeyN">Some text</subsection>
<subsection key="KeyP">Some text</subsection>
</section>
</root>
<小时>
要禁止输入文档中没有匹配子节的节规则,请尝试:
To suppress section rules that do not have matching subsections in the input document, try:
<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:key name="sub" match="subsection" use="@key" />
<xsl:variable name="root" select="/" />
<xsl:template match="/">
<root>
<xsl:for-each select="document('file2.xml')/rules/section">
<xsl:variable name="id" select="@id" />
<xsl:variable name="keys" select="key" />
<xsl:for-each select="$root">
<xsl:if test="key('sub', $keys)">
<section id="{$id}">
<xsl:copy-of select="key('sub', $keys)"/>
</section>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
这篇关于基于规则的节点重组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!