1. Search user guide

PDFコンテンツ

日本語翻訳に関する免責事項

このページの翻訳はAIによって自動的に行われました。可能な限り正確な翻訳を心掛けていますが、原文と異なる表現や解釈が含まれる場合があります。正確で公式な情報については、必ず英語の原文をご参照ください。

PDFコンテンツから属性値を抽出するためのサンプルのJavaScript文書エクストラクターをいくつかご用意しています。各例にはPDFのHTML構造の抽出と対応するドキュメントエクストラクターが含まれています。

サンプル1

この例は、条件付き関数を使って属性値を取得するシンプルなJavaScriptエクストラクタの作成方法を示しています。

PDFは*https://archive.doc.sitecore.com/xp/en/legacy-docs/web-forms-for-marketers-8.0.pdf*で入手可能です。簡潔にまとめたHTML構造は以下の通りです。

...

Web Forms for Marketers 8.0 Rev: September 13, 2018

Web Forms for Marketers 8.0

All the official Sitecore documentation.

Add an ASCX control to the page In the Web Forms for Marketers module, you can convert and export a form to an .ascx file and then add it to your website as an ASCX control. For developers, this can make it easier to develop their custom form control.

To add an ASCX control to the page:

1. Using a text editor, in the \\layouts folder of your Sitecore installation, create a new default.aspx page and insert the following code:

<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

</form>

</body>

</html>

....

以下は、このJavaScript PDFから名前タイプウェブサイト説明、要 著者last_modified属性を抽出するためのドキュメントエクストラクターのサンプルです。

function extract(request, response) { $ = response.body;

return { 'name': $('div.page

(0)').text().trim().substring(0, 40) || 'No Name', 'type': 'pdf', 'website':'Sitecore Documentation', 'description' : $('div.page
(0)').text().trim().substring(0, 100) || 'No Description', 'author': $('metaname="dc
"').attr('content') || 'No Author', 'last_modified': $('metaname="dcterms
"').attr('content') || 'No Last Modified Date' }; }

この関数は以下のロジックを使って属性値を取得します:

  • name- 最初のdivタグのテキストをpageのclassでトリミングします。その後、最初の40文字のみを使用します。テキストがなければ、値をNo Nameに設定します。
  • type - 固定値( pdf)を使用する。
  • website - 固定値、 Sitecore Documentationを用いる。
  • description- クラスpageの最初の
    タグのテキストを、最初の100文字のみ使用します。テキストがなければ、値をNo Descriptionに設定します。
  • author- 最初のmetaタグの内容値をdc
    。テキストがなければ、値をNo Authorに設定します。
  • last_modified- 最初のmetaタグのテキストをdcterms
    。テキストがなければ、値をNo Last Modified Dateに設定します。

サンプル2

この例は、多くのネスト関数を持つ複雑なJavaScriptエクストラクタを作成し、望む属性値を取得する方法を示しています。また、PDFがいる親ページを追跡するためのparent_url属性の抽出方法も定義しています。

PDFは*https://www.sitecore.com/customers/associations/us-masters-swimming*で「 ケーススタディをダウンロード」をクリックしてご覧いただけます。簡潔にまとめたHTML構造は以下の通りです:

...

Industry: Associations • Founded: 1970 • Employees: 17

Headquarters: Boca Raton, Florida, USA • usms.org

....

Sitecore is the global leader in experience management software that combines content management, commerce, and customer insights. The Sitecore® Experience Cloud™ empowers marketers

...

以下は、このPDFからidtypelast_modified名前descriptionparent_url属性を抽出するためのドキュメントエクストラクタJavaScriptサンプルです:

function extract(request, response) { const translate_re = /&(nbsp|amp|quot|lt|gt);/g;

function decodeEntities(encodedString) { return encodedString.replace(translate_re, function(match, entity) { return translateentity; }).replace(/&#(\d+);/gi, function(match, numStr) { const num = parseInt(numStr, 10); return String.fromCharCode(num); }); }

function sanitize(text) { return text ? decodeEntities(String(text).trim()) : text; }

$ = response.body; url = request.url; id = url.replace(/.:/&?=%/g, '_'); name = sanitize($('name').text()); description = $('body').text().substring(0, 7000);

$p = request.context.parent.response.body; if (name.length <= 4 && $p) { name = $p('name').text(); }

parentUrl = request.context.parent.request.url; last_modified = request.context.parent.documents0.data.last_modified;

return { 'id': id, 'type': "pdf", 'parent_url': parentUrl 'last_modified': last_modified, 'name': name, // 'description': description,

}; }

この関数は以下のロジックを使って属性値を取得します:

  • id - URLの特殊文字をアンダースコア(_)に置き換える。
  • type - 固定値( pdf)を使用する。
  • parent_url - 親コンテキスト内で(request.context.parent) request オブジェクトにアクセスする。次に、 url パラメータにアクセスする。
  • last_modified- 親コンテキスト(request.context.parent)内で最初のドキュメント配列(documents0)にアクセスする。次に、URLのlast_modified属性のデータオブジェクトにアクセスする
  • name - name HTML要素または親文書の name HTML要素のいずれかを使用する。以下のように:
    • まず、 HTML要素のテキストを消毒してください。
    • 次に、サニティ化された名前が短すぎるかどうかを確認するために、長さが4文字以下かを確認します。
    • 名前が短すぎて、かつ親文書に定義された本文($p)がある場合は、親文書の name タグを使いましょう。
  • description - HTML要素のテキストをサニテリングし、最初の7000文字に制限する。
この記事を改善するための提案がある場合は、 お知らせください!