Rule-based restructuring of nodes

后端 未结 1 1601
滥情空心
滥情空心 2021-01-27 23:59

Supposed I have the following XML file with nodes I would like to rearrange based on rules:

   
  Some text

        
相关标签:
1条回答
  • 2021-01-28 00:15

    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>
    

    to get the following result:

    <?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>
    

    Edit:

    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>
    
    0 讨论(0)
提交回复
热议问题