使用 xmlstarlet 直接在另一个元素之后插入一个元素不起作用



我知道这样的问题已经回答了很多次,但至少没有一个对我有用。。。使用我的XML文件。我用更简单的XML树尝试了这些答案,但它们对我的答案有效——它们没有。这就是为什么我发布了一个问题,希望有人能启发我:-)

我的XML(Liferay的东西)如下:

<?xml version="1.0"?>
<beans>
    <bean id="liferayDataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
        <property name="targetDataSource">
            <bean class="org.springframework.aop.framework.ProxyFactoryBean">
                <property name="targetSource" ref="shardDataSourceTargetSource" />
            </bean>
        </property>
    </bean>
    <bean id="liferayHibernateSessionFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="targetSource" ref="shardSessionFactoryTargetSource" />
    </bean>
    <bean id="shardDataSource0" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
        <property name="targetDataSource">
            <bean class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean">
                <property name="propertyPrefix" value="jdbc.default." />
            </bean>
        </property>
    </bean>
    <bean id="shardDataSource1" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
        <property name="targetDataSource">
            <bean class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean">
                <property name="propertyPrefix" value="jdbc.NIGERIA." />
            </bean>
        </property>
    </bean>
    <bean id="shardDataSource2" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
        <property name="targetDataSource">
            <bean class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean">
                <property name="propertyPrefix" value="jdbc.MEDLAHO." />
            </bean>
        </property>
    </bean>
    <bean id="shardDataSourceTargetSource" class="com.liferay.portal.dao.shard.ShardDataSourceTargetSource">    
        <property name="dataSources">
            <map>
                <entry>
                    <key>
                        <value>default</value>
                    </key>
                    <ref bean="shardDataSource0" />
                </entry>
                <entry>
                    <key>
                        <value>NIGERIA</value>
                    </key>
                    <ref bean="shardDataSource1" />
                </entry>
                <entry>
                    <key>
                        <value>MEDLAHO</value>
                    </key>
                    <ref bean="shardDataSource2" />
                </entry>
            </map>
        </property>
    </bean>
    <bean id="shardSessionFactoryTargetSource" class="com.liferay.portal.dao.shard.ShardSessionFactoryTargetSource">
        <property name="shardDataSourceTargetSource" ref="shardDataSourceTargetSource" />
    </bean>
    <bean id="com.liferay.portal.kernel.dao.jdbc.MappingSqlQueryFactoryUtil" class="com.liferay.portal.kernel.dao.jdbc.MappingSqlQueryFactoryUtil">
        <property name="mappingSqlQueryFactory">
            <bean class="com.liferay.portal.dao.shard.ShardMappingSqlQueryFactoryImpl" />
        </property>
    </bean>
    <bean id="com.liferay.portal.kernel.dao.jdbc.SqlUpdateFactoryUtil" class="com.liferay.portal.kernel.dao.jdbc.SqlUpdateFactoryUtil">
        <property name="sqlUpdateFactory">
            <bean class="com.liferay.portal.dao.shard.ShardSqlUpdateFactoryImpl" />
        </property>
    </bean>
    <bean id="com.liferay.portal.kernel.dao.shard.ShardUtil" class="com.liferay.portal.kernel.dao.shard.ShardUtil">
        <property name="shard">
            <bean class="com.liferay.portal.dao.shard.ShardImpl">
                <property name="shardAdvice" ref="com.liferay.portal.dao.shard.advice.ShardAdvice" />
            </bean>
        </property>
    </bean>
    <bean id="com.liferay.portal.kernel.util.InfrastructureUtil" class="com.liferay.portal.kernel.util.InfrastructureUtil">
        <property name="dataSource" ref="liferayDataSource" />
        <property name="mailSession" ref="mailSession" />
        <property name="shardDataSourceTargetSource" ref="shardDataSourceTargetSource" />
        <property name="shardSessionFactoryTargetSource" ref="shardSessionFactoryTargetSource" />
        <property name="transactionManager" ref="liferayTransactionManager" />
    </bean>
    <bean id="com.liferay.portal.dao.shard.advice.ShardAdvice" class="com.liferay.portal.dao.shard.advice.ShardAdvice">
        <property name="shardDataSourceTargetSource" ref="shardDataSourceTargetSource" />
        <property name="shardSessionFactoryTargetSource" ref="shardSessionFactoryTargetSource" />
    </bean>
    <bean id="com.liferay.portal.dao.shard.advice.ShardCompanyAdvice" class="com.liferay.portal.dao.shard.advice.ShardCompanyAdvice">
        <property name="shardAdvice" ref="com.liferay.portal.dao.shard.advice.ShardAdvice" />
    </bean>
    <bean id="com.liferay.portal.dao.shard.advice.ShardPortletAdvice" class="com.liferay.portal.dao.shard.advice.ShardPortletAdvice">
        <property name="shardAdvice" ref="com.liferay.portal.dao.shard.advice.ShardAdvice" />
    </bean>
    <bean id="com.liferay.portal.dao.shard.advice.ShardPersistenceAdvice" class="com.liferay.portal.dao.shard.advice.ShardPersistenceAdvice">
        <property name="shardAdvice" ref="com.liferay.portal.dao.shard.advice.ShardAdvice" />
    </bean>
    <bean id="com.liferay.portal.dao.shard.advice.ShardGloballyAdvice" class="com.liferay.portal.dao.shard.advice.ShardGloballyAdvice">
        <property name="shardAdvice" ref="com.liferay.portal.dao.shard.advice.ShardAdvice" />
    </bean>
    <bean id="com.liferay.portal.dao.shard.advice.ShardIterativelyAdvice" class="com.liferay.portal.dao.shard.advice.ShardIterativelyAdvice">
        <property name="shardAdvice" ref="com.liferay.portal.dao.shard.advice.ShardAdvice" />
    </bean>
    <bean id="com.liferay.portal.dao.shard.advice.ShardParameterAdvice" class="com.liferay.portal.dao.shard.advice.ShardParameterAdvice">
        <property name="shardAdvice" ref="com.liferay.portal.dao.shard.advice.ShardAdvice" />
    </bean>
    <aop:config proxy-target-class="false">
        <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardCompanyAdvice" pointcut="bean(com.liferay.portal.service.CompanyLocalService)" />
        <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardGloballyAdvice" pointcut="execution(void com.liferay.portal.convert.messaging.ConvertProcessMessageListener.receive(..))" />
        <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardGloballyAdvice" pointcut="execution(void com.liferay.portal.events.StartupHelper.updateIndexes())" />
        <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardGloballyAdvice" pointcut="execution(void com.liferay.portal.events.StartupHelper.upgradeProcess(int))" />
        <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardGloballyAdvice" pointcut="execution(* com.liferay.portal.service.ReleaseLocalService.getBuildNumberOrCreate())" />
        <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardGloballyAdvice" pointcut="execution(void com.liferay.portal.service.ServiceComponentLocalService.upgradeDB(..))" />
        <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardGloballyAdvice" pointcut="execution(void com.liferay.portlet.journal.service.JournalArticleLocalService.checkArticles())" />
        <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardIterativelyAdvice" pointcut="execution(void com.liferay.portal.events.StartupHelper.verifyProcess(boolean, boolean))" />
        <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardParameterAdvice" pointcut="execution(* com.liferay.portal.service.AccountLocalService.getAccount(long, long))" />
        <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardParameterAdvice" pointcut="execution(* com.liferay.portal.service.GroupLocalService.getCompanyGroup(long))" />
        <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardParameterAdvice" pointcut="execution(* com.liferay.portal.service.UserLocalService.searchCount(..))" />
        <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardPersistenceAdvice" pointcut="bean(*Persistence) || bean(*Finder)" />
        <aop:advisor advice-ref="com.liferay.portal.dao.shard.advice.ShardPortletAdvice" pointcut="bean(com.liferay.portal.service.PortletLocalService)" />
    </aop:config>
</beans>

我想要的是拥有另一个类似bean的块,例如:

    <bean id="shardDataSource4" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
        <property name="targetDataSource">
            <bean class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean">
                <property name="propertyPrefix" value="jdbc.default." />
            </bean>
        </property>
    </bean>

就在具有id=shardDataSource0的那个之后

我还没有尝试插入包含所有子元素及其属性的完整块,我只是尝试插入一个新的bean元素作为开始。。。但我不能。

我试过很多种选择,但不能像下面这样简单:

xmlstarlet ed -L -a /beans/bean[@id='shardDataSource0'] -t elem -n bean -v "" file.xml

这不起作用:-(它什么都不做,文件没有修改,退出代码为0。

所以。。。我在这里错过了什么,伙计们???

非常非常感谢。

注意:我运行的是SLES扩展支持平台6.7版,xmlstarlet包是:xmlstarlet-1.5.0-1.el6.rf.x86_64


更新

多亏了@npostavs的回答,我成功地附加了整个bean块。正确的行如下:

xmlstarlet ed -a "/beans/bean[@id='shardDataSource0']" -t elem -n beanTMP -v ""  
    -i //beanTMP -t attr -n "id" -v "shardDataSource1" 
    -i //beanTMP -t attr -n "class" -v "org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy" 
    -s //beanTMP -t elem -n "propertyTMP" -v "" 
    -i //propertyTMP -t attr -n "name" -v "targetDataSource" 
    -s //propertyTMP -t elem -n "beanTMP2" -v "" 
    -i //beanTMP2 -t attr -n "class" -v "com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean" 
    -s //beanTMP2 -t elem -n "propertyTMP2" -i //propertyTMP2 -t attr -n "name" -v "propertyPrefix" 
    -i //propertyTMP2 -t attr -n "value" -v "jdbc.default." 
    -r //propertyTMP -v "property" 
    -r //propertyTMP2 -v "property" 
    -r //beanTMP -v "bean" 
    -r //beanTMP2 -v "bean" 
    file.xml

然而,结果如下:

<?xml version="1.0"?>
<beans>
  <bean id="shardDataSource0" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
    <property name="targetDataSource">
      <bean class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean">
        <property name="propertyPrefix" value="jdbc.default."/>
      </bean>
    </property>
  </bean>
  <bean id="shardDataSource1" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy"><property name="targetDataSource"><bean class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean"><property name="propertyPrefix" value="jdbc.default."/></bean></property></bean>
</beans>

这意味着我没有得到正确的格式。我尝试了全局选项-P-S和-ps或-pf,但都不起作用。。。。有人知道为什么会发生这种事吗?

非常感谢所有提前!!


更新2

我已经设法获得了一个正确的格式化管道,将第一行xmlstarlet设置为:xmlstarlet-fo-R,用于格式化整个文档。但我确信这不是一条路。。。所以伙计们,。。有线索吗?

---------------输入---------------

<?xml version="1.0"?>
<beans>
  <bean id="liferayHibernateSessionFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="targetSource" ref="shardSessionFactoryTargetSource"/>
  </bean>
  <bean id="shardDataSource0" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
    <property name="targetDataSource">
      <bean class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean">
        <property name="propertyPrefix" value="jdbc.default."/>
      </bean>
    </property>
  </bean>
  <bean id="shardDataSource1" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
    <property name="targetDataSource">
      <bean class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean">
        <property name="propertyPrefix" value="jdbc.NIGERIA."/>
      </bean>
    </property>
  </bean>
</beans>

------------1---------

xml ed
-a beans/bean[@id='shardDataSource0'] -t elem -n bean
-i beans/bean[not(@*)] -t attr -n id -v shardDataSource4
-i beans/bean[@id='shardDataSource4'] -t attr -n class -v org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy
-s beans/bean[@id='shardDataSource4'] -t elem -n property
-i beans/bean[@id='shardDataSource4']/property -t attr -n name -v targetDataSource
-s beans/bean[@id='shardDataSource4']/property -t elem -n bean
-i beans/bean[@id='shardDataSource4']/property/bean -t attr -n class -v com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean
-s beans/bean[@id='shardDataSource4']/property/bean -t elem -n property
-i beans/bean[@id='shardDataSource4']/property/bean/property -t attr -n name -v propertyPrefix
-i beans/bean[@id='shardDataSource4']/property/bean/property -t attr -n value -v jdbc.default. file.xml

------------2-------------

xml ed
-a beans/bean[@id='shardDataSource0'] -t elem -n xxx
-i //xxx -t attr -n id -v shardDataSource4
-i //xxx -t attr -n class -v org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy
-s //xxx -t elem -n property
-i //xxx/property -t attr -n name -v targetDataSource
-s //xxx/property -t elem -n bean
-i //xxx/property/bean -t attr -n class -v com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean
-s //xxx/property/bean -t elem -n property
-i //xxx/property/bean/property -t attr -n name -v propertyPrefix
-i //xxx/property/bean/property -t attr -n value -v jdbc.default.
-r //xxx -v bean file.xml

------------3---------

xml ed
-i beans/bean[@id='shardDataSource1'] -t elem -n bean
-i beans/bean[not(@*)] -t attr -n id -v shardDataSource4
-i beans/bean[@id='shardDataSource4'] -t attr -n class -v org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy
-s beans/bean[@id='shardDataSource4'] -t elem -n property
-i beans/bean[@id='shardDataSource4']/property -t attr -n name -v targetDataSource
-s beans/bean[@id='shardDataSource4']/property -t elem -n bean
-i beans/bean[@id='shardDataSource4']/property/bean -t attr -n class -v com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean
-s beans/bean[@id='shardDataSource4']/property/bean -t elem -n property
-i beans/bean[@id='shardDataSource4']/property/bean/property -t attr -n name -v propertyPrefix
-i beans/bean[@id='shardDataSource4']/property/bean/property -t attr -n value -v jdbc.default. file.xml

------------4-------------

xml ed -a beans/bean[@id='shardDataSource0'] -t elem -n bean
-i beans/bean[not(@*)] -t attr -n id -v shardDataSource4
-i beans/bean[@id='shardDataSource4'] -t attr -n class -v org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy
-u beans/bean[@id='shardDataSource4']
-v "<property name="targetDataSource"><bean class="com.liferay.portal.dao.jdbc.spring.DataSourceFactoryBean"><property name="propertyPrefix" value="jdbc.default." /></bean></property>" file.xml | xml unesc | xml fo

相关内容

  • 没有找到相关文章

最新更新