递归地应用XSLT

时间:2018-10-19 04:20:36

标签: java c# xml xslt

我正在尝试使用xslt从另一个xml创建一个xml。但是我试图使其递归工作时遇到问题 这是原始的xml:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <sites>
      <site name="name001" enabled="true" />
      <site name="name003" enabled="false">456</site>
      <env name="name004" enabled="true" />
   </sites>
   <templates>
      <template name="example" SSL="true">
         <props>
            <others>
               <other name="abc001">true</other>
               <other name="abc002">
                  <options>
                     <option name="xyz001">567</option>
                     <option name="xyz001">987</option>
                  </options>
               </other>
            </others>
         </props>
      </template>
      <type name="test999" enabled="true">
         <props>
            <others>
               <other name="name001" enabled="true">WEBSITE1</other>
               <other name="abc001" />
            </others>
            <install name="xyz">example001</install>
         </props>
      </type>
      <type name="www">
         <props>
            <otherProps>
               <otherProp name="user">anonymous</otherProp>
               <otherProp name="pass" enabled="true" />
               <otherProp name="url" />
            </otherProps>
            <install name="name001">test</install>
         </props>
      </type>
   </templates>
</root>

我想应用xslt来检索类似的xml,但仅使用具有conf =“ true”属性的元素。即使父母没有conf =“ true属性”,也必须对其进行检索 像这样:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <sites>
      <site name="name001" enabled="true" />
      <env name="name004" enabled="true" />
   </sites>
      <type name="test999" enabled="true">
         <props>
            <others>
               <other name="name001" enabled="true">WEBSITE1</other>
         </props>
      </type>
      <type name="www">
         <props>
            <otherProps>
               <otherProp name="pass" enabled="true" />
            </otherProps>
         </props>
      </type>
   </templates>
</root>

预先感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我非常确定您提到了启用属性,该属性必须为true才能输出一些数据。基本上,您需要复制具有该属性的每个元素或具有该属性的后代。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fx="this" exclude-result-prefixes="xs"
    version="1.0">
    <xsl:strip-space elements="*"/> <!-- Removes whitespaces in the output -->
    <xsl:template match="node()"> <!-- Matches every node and checks if it should be printed -->
        <xsl:if test="descendant-or-self::node()[@enabled='true']">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:if>
    </xsl:template>
    <xsl:template match="@*|text()"> <!-- We did the check in the node template already so we just want to copy everything -->
        <xsl:copy/>
    </xsl:template>
</xsl:stylesheet>

使用输入:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <sites>
      <site name="name001" enabled="true" />
      <site name="name003" enabled="false">456</site>
      <env name="name004" enabled="true" />
   </sites>
   <templates>
      <template name="example" SSL="true">
         <props>
            <others>
               <other name="abc001">true</other>
               <other name="abc002">
                  <options>
                     <option name="xyz001">567</option>
                     <option name="xyz001">987</option>
                  </options>
               </other>
            </others>
         </props>
      </template>
      <type name="test999" enabled="true">
         <props>
            <others>
               <other name="name001" enabled="true">WEBSITE1</other>
               <other name="abc001" />
            </others>
            <install name="xyz">example001</install>
         </props>
      </type>
      <type name="www">
         <props>
            <otherProps>
               <otherProp name="user">anonymous</otherProp>
               <otherProp name="pass" enabled="true" />
               <otherProp name="url" />
            </otherProps>
            <install name="name001">test</install>
         </props>
      </type>
   </templates>
</root>

我得到输出:

<root>
   <sites>
      <site name="name001" enabled="true"/>
      <env name="name004" enabled="true"/>
   </sites>
   <templates>
      <type name="test999" enabled="true">
         <props>
            <others>
               <other name="name001" enabled="true">WEBSITE1</other>
            </others>
         </props>
      </type>
      <type name="www">
         <props>
            <otherProps>
               <otherProp name="pass" enabled="true"/>
            </otherProps>
         </props>
      </type>
   </templates>
</root>