使用Python解析XML - 命名空间

时间:2018-02-06 01:10:05

标签: python xml parsing lxml elementtree

我想使用开放数据,xml存储在这里:

http://offenedaten.frankfurt.de/dataset/912fe0ab-8976-4837-b591-57dbf163d6e5/resource/48378186-5732-41f3-9823-9d1938f2695e/download/parkdatendyn.xml

在这个论坛的帮助下我编写了代码

<?php
/*** This file handles the output on the homepage.*/
/***************** Start Project Filter  ********************/
// Force full width content (optional)
add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' );
//remove standard loop (optional)
remove_action( 'genesis_loop', 'genesis_do_loop' );
// Add our custom loop
add_action( 'genesis_loop', 'lp_filterable_portfolio' );
// Enqueue javascript
wp_enqueue_script('isotope', get_stylesheet_directory_uri() . '/js/isotope.pkgd.min.js', array('jquery'), '1.5.25', true);
wp_enqueue_script('isotope_init', get_stylesheet_directory_uri() . '/js/isotopes_init.js', array('isotope'), '', true);


/*** Get Excerpt. */
function the_excerpt_max_charlength($charlength) {
 $excerpt = get_the_excerpt();
 $charlength++;
 if ( mb_strlen( $excerpt ) > $charlength ) {
 $subex = mb_substr( $excerpt, 0, $charlength - 5 );
 $exwords = explode( ' ', $subex );
 $excut = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) );
 if ( $excut < 0 ) {
 echo mb_substr( $subex, 0, $excut );
 } else {
 echo $subex;
 }
 echo '[...]';
 } else {
 echo $excerpt;
 }
}
/**
* Output filterable items. */
function lp_filterable_portfolio( ){

 $args = array(
 'post_per_page' => 9999
 );
 $loop = new WP_Query( $args );
 $terms = get_terms( 'my_taxonomy' );
 $count=0;
 ?>
<div class="archive-description">
 <?php if( $terms ) { ?>
 <ul id="portfolio-cats" class="filter clearfix">
 <li><a href="#" class="active" data-filter="*"><span><?php _e('All', 'lp'); ?></span></a></li>
 <?php
 foreach( $terms as $term ){
 echo "<li><a href='#' data-filter='.$term->slug'><span>$term->name</span></a></li>";
 }
 ?>
 </ul><!-- /portfolio-cats --><br/><br/>
 <?php } ?>
 <?php if( have_posts() ) { ?>
 <div id="portfolio-wrap" class="clearfix filterable-portfolio">
 <div class="portfolio-content">
 <?php while( have_posts() ): the_post(); ?>
 <?php $count++; ?>
 <?php $terms = get_the_terms( get_the_ID(), 'my_taxonomy' ); ?>
 <?php if ( has_post_thumbnail($post->ID) ) { ?>
 <article class="portfolio-item col-<?php echo $count; ?> <?php if( $terms ) foreach ( $terms as $term ) { echo $term->slug .' '; }; ?>">
 <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php echo genesis_get_image( array( size => 'lp-portfolio' ) ); ?>
 <div class="portfolio-overlay">
 <h3><?php the_title(); ?></h3>
 <p><?php the_excerpt_max_charlength(50);?></p>
 </div><!-- overlay --></a>
 </article>
 <?php } ?>
 <?php endwhile; ?>
 </div><!-- /themes-content -->
 </div><!-- /themes-wrap -->
 <?php } ?>
 <?php wp_reset_postdata(); ?>
</div>
<?php
 wp_reset_postdata();
}

genesis();

这似乎适用于&#34; pa&#34;但对于&#34; pf&#34;我收到了一条失败的消息:

ParkingspaceList.append(STR((facility_ref.get(&#39; ID&#39;)))) AttributeError:&#39; NoneType&#39;对象没有属性&#39; get&#39;

任何提示?

最好的问候

TR

1 个答案:

答案 0 :(得分:1)

同名的parkingFacilityStatus元素中有parkingFacilityStatus个元素,这相当令人困惑:

<parkingFacilityStatus>
    …
    <parkingFacilityReference targetClass="ParkingFacility" id="24278[Karstadt]" version="1.0"/>
    <parkingFacilityStatus>closed</parkingFacilityStatus>
    …
</parkingFacilityStatus>

    <parkingFacilityStatus>closed</parkingFacilityStatus>

给出错误,因为该元素没有parkingFacilityReference子元素。

使用

for pf in parking_facility:
    facility_ref = pf.find('d:parkingFacilityReference', ns)
    if facility_ref is not None:
        ParkingspaceList.append(str((facility_ref.get('id'))))

它有效。

相关问题