2017-11-19 11 views
2

XSLTを使用してAPI XML応答ファイルから属性を選択しようとしています。変換後のXSLTでxsl:value-ofとxsl:for-eachが無視される

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:overheid="http://standaarden.overheid.nl/owms/terms/" xmlns:overheidop="http://standaarden.overheid.nl/product/terms/" xmlns:overheidvb="http://standaarden.overheid.nl/vb/terms"> 
    <xsl:strip-space elements="*"/> 
    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="/"> 
     <verkeersbesluiten> 
      <test>Hello world!</test> 
      <numberOfRecords><xsl:value-of select="searchRetrieveResponse/numberOfRecords"/></numberOfRecords> 
      <xsl:for-each select="searchRetrieveResponse/records/record/recordData/gzd/originalData/overheidop:meta/overheidop:owmskern"> 
       <besluit> 
        <test2>Hello world again!</test2> 
        <titel><xsl:value-of select="dcterms:title"/></titel> 
        <locatie><xsl:value-of select="dcterms:spatial"/></locatie> 
       </besluit>  
      </xsl:for-each> 
     </verkeersbesluiten> 
    </xsl:template> 
</xsl:stylesheet> 

結果のファイル:

http://zoekdienst.overheid.nl/sru/Search?version=1.2&operation=searchRetrieve&x-connection=oep&startRecord=1&maximumRecords=10&query=(keyword=verkeersbesluit) and (creator=groningen) and (organisationType=Provincie)は、私は、次のXSLファイルを作成:

<?xml version="1.0" encoding="UTF-8"?> 
<verkeersbesluiten xmlns:dcterms="http://purl.org/dc/terms/" xmlns:overheid="http://standaarden.overheid.nl/owms/terms/" xmlns:overheidop="http://standaarden.overheid.nl/product/terms/" xmlns:overheidvb="http://standaarden.overheid.nl/vb/terms"> 
    <test>Hello world!</test> 
    <numberOfRecords/> 
</verkeersbesluiten> 

これはされていない
これは、XMLファイルを返す要求があります私が望む結果。それは:

<?xml version="1.0" encoding="UTF-8"?> 
<verkeersbesluiten xmlns:dcterms="http://purl.org/dc/terms/" xmlns:overheid="http://standaarden.overheid.nl/owms/terms/" xmlns:overheidop="http://standaarden.overheid.nl/product/terms/" xmlns:overheidvb="http://standaarden.overheid.nl/vb/terms"> 
    <test>Hello world!</test> 
    <numberOfRecords>41</numberOfRecords> 
    <besluit> 
     <test2>Hello World again!</test2> 
     <titel>Provincie Groningen: verkeersbesluit nieuwe situatie parallelweg langs N964 tussen Winschoten en Scheemda.</titel> 
     <locatie>264455 576268$%$Oldambt</locatie> 
    </besluit> 
    <besluit> 
     <test2>Hello World again!</test2> 
     <titel>Provincie Groningen: verkeersbesluit nieuwe situatie N366, Westerstraat en A.G. Wildervanckweg in Ter Apel</titel> 
     <locatie>266425 544276$%$Vlagtwedde<</locatie> 
    </besluit> 
    ... 
</verkeersbesluiten> 

誰かが私に間違っていることを指摘してもらえますか?

ウィリー

========================================= =================

@halfbitが示唆しているように、私は名前空間srwとsru私のXSLファイルに:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:srw="http://www.loc.gov/zing/srw/" xmlns:sru="http://standaarden.overheid.nl/sru/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:overheid="http://standaarden.overheid.nl/owms/terms/" xmlns:overheidop="http://standaarden.overheid.nl/product/terms/" xmlns:overheidvb="http://standaarden.overheid.nl/vb/terms"> 
    <xsl:strip-space elements="*"/> 
    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="/"> 
     <verkeersbesluiten> 
      <test>Hello world!</test> 
      <numberOfRecords><xsl:value-of select="srw:searchRetrieveResponse/srw:numberOfRecords"/></numberOfRecords> 
      <xsl:for-each select="srw:searchRetrieveResponse/srw:records/srw:record/sru:gzd"> 
       <besluit> 
        <test2>Hello world again!</test2> 
       </besluit>  
      </xsl:for-each> 
     </verkeersbesluiten> 
    </xsl:template> 
</xsl:stylesheet> 

これはに結果:

<?xml version="1.0" encoding="UTF-8"?> 
<verkeersbesluiten xmlns:srw="http://www.loc.gov/zing/srw/" xmlns:sru="http://standaarden.overheid.nl/sru/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:overheid="http://standaarden.overheid.nl/owms/terms/" xmlns:overheidop="http://standaarden.overheid.nl/product/terms/" xmlns:overheidvb="http://standaarden.overheid.nl/vb/terms"> 
    <test>Hello world!</test> 
    <numberOfRecords>41</numberOfRecords> 
    <besluit> 
    <test2>Hello world again!</test2> 
    </besluit> 
    <besluit> 
    <test2>Hello world again!</test2> 
    </besluit> 
    ... 
</besluiten> 

これはよさそうです。私は、XSLを拡張する場合でも:ため、各句何かがうまくいかない:私はこのまわりで私の頭を取得することはできません

<?xml version="1.0" encoding="UTF-8"?> 
<verkeersbesluiten xmlns:srw="http://www.loc.gov/zing/srw/" xmlns:sru="http://standaarden.overheid.nl/sru/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:overheid="http://standaarden.overheid.nl/owms/terms/" xmlns:overheidop="http://standaarden.overheid.nl/product/terms/" xmlns:overheidvb="http://standaarden.overheid.nl/vb/terms"> 
    <test>Hello world!</test> 
    <numberOfRecords>41</numberOfRecords> 
</verkeersbesluiten> 

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:srw="http://www.loc.gov/zing/srw/" xmlns:sru="http://standaarden.overheid.nl/sru/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:overheid="http://standaarden.overheid.nl/owms/terms/" xmlns:overheidop="http://standaarden.overheid.nl/product/terms/" xmlns:overheidvb="http://standaarden.overheid.nl/vb/terms"> 
<xsl:strip-space elements="*"/> 
<xsl:output method="xml" indent="yes"/> 

    <xsl:template match="/"> 
     <verkeersbesluiten> 
      <test>Hello world!</test> 
      <numberOfRecords><xsl:value-of select="srw:searchRetrieveResponse/srw:numberOfRecords"/></numberOfRecords> 
      <xsl:for-each select="srw:searchRetrieveResponse/srw:records/srw:record/sru:gzd"> 
       <besluit> 
        <test2>Hello world again!</test2> 
       </besluit>  
      </xsl:for-each> 
     </verkeersbesluiten> 
    </xsl:template> 
</xsl:stylesheet> 

これはになります。私は間違って何をしていますか?

+0

このxml入力ファイルを確認して警告があります。 * "ドキュメント内のこの場所から参照されるスキーマにはエラーがあります。" *は 'xsi:schemaLocation =" http://www.loc.gov/zing/srw/ srw-types.xsd "と' xsi:schemaLocation = "http://standaarden.overheid.nl/sru gzd.xsd" – derloopkat

+0

XML入力は、デフォルトの名前空間として 'http:// www.loc.gov/zing/srw /'を使用しますが、 'select '属性は空の名前空間を参照します(' searchRetrieveResponse'など)。ネームスペース( 'xmlns:srw =" http://www.loc.gov/zing/srw/ "')を宣言し、パス内の要素( 'srw:searchRetrieveResponse'など)をあなたのXSLで修飾することができますファイル。 – halfbit

+0

@halfbitが提案する名前空間を追加し、これらの変更を反映するためにオリジナルの投稿を変更しました。結果は改善しましたが、私はまだ私が期待したものを得ていません。多分別の提案がありますか? – Willy

答えて

0

私のために、次の作品:

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:srw="http://www.loc.gov/zing/srw/" 
    xmlns:sru="http://standaarden.overheid.nl/sru" 
    xmlns:dcterms="http://purl.org/dc/terms/" 
    xmlns:overheid="http://standaarden.overheid.nl/owms/terms/" 
    xmlns:overheidop="http://standaarden.overheid.nl/product/terms/" 
    xmlns:overheidvb="http://standaarden.overheid.nl/vb/terms"> 

    <xsl:strip-space elements="*"/> 
    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="/"> 
    <verkeersbesluiten> 
     <test>Hello world!</test> 
     <numberOfRecords><xsl:value-of select="srw:searchRetrieveResponse/srw:numberOfRecords"/></numberOfRecords> 
     <xsl:for-each select="srw:searchRetrieveResponse/srw:records/srw:record/srw:recordData/sru:gzd/sru:originalData/overheidop:meta/overheidop:owmskern"> 
     <besluit> 
      <test2>Hello world again!</test2> 
      <titel><xsl:value-of select="dcterms:title"/></titel> 
      <locatie><xsl:value-of select="dcterms:spatial"/></locatie> 
     </besluit> 
     </xsl:for-each> 
    </verkeersbesluiten> 
    </xsl:template> 
</xsl:stylesheet> 

それはsru名前空間(それが最後のスラッシュを持っていません - すべての文字をカウント)

  • に最新のXSL異なり
  • パス内のsru:gzdの前には、srw:recordDataがありません。

ところで:あなたはfor-each

<xsl:for-each select="srw:searchRetrieveResponse//overheidop:owmskern"> 

を短縮することができます(期待recordごとに1つだけowmskern)XML入力許可の構造ならばこれは少し遅くなるが、より読みやすく、あるかもしれない - あなたと経験豊かで、書き込み/保守のエラープラニングが少ないオフレコ


:XSLTでは、いくつかは、これはXSLTプロセッサが、とにかくやっていることであるため、入力XMLを横断するためfor-eachを使用しない、代わりに、そのような複数はるかに簡単、より多くの焦点を合わせたテンプレートを、書きます:

<!-- only traverse, do not copy anything by default --> 
    <xsl:template match="@*|node()"> 
    <xsl:apply-templates select="@*|node()"/> 
    </xsl:template> 

    <xsl:template match="srw:searchRetrieveResponse"> 
    <verkeersbesluiten> 
     <test>Hello world!</test> 
     <numberOfRecords><xsl:value-of select="srw:numberOfRecords"/></numberOfRecords> 
     <xsl:apply-templates/> 
    </verkeersbesluiten> 
    </xsl:template> 

    <xsl:template match="overheidop:owmskern"> 
    <besluit> 
     <test2>Hello world again!</test2> 
     <titel><xsl:value-of select="dcterms:title"/></titel> 
     <locatie><xsl:value-of select="dcterms:spatial"/></locatie> 
    </besluit> 
    </xsl:template> 
+0

これは素晴らしいことです!どうもありがとうございました! – Willy

関連する問題