XML - condicionales en XML

   
Vista:

condicionales en XML

Publicado por Antonio (1 intervención) el 23/11/2009 19:11:00
Buenas a todos, vamos a ver si ayudais a un desconocedor total de XML a hacer lo siguiente, sentencias condicionales. MI intencion es manipular un XML de este tipo:

Código:

<?xml version="1.0" encoding="iso-8859-1"?>
<gallery>

<image >
<description>image 1</description>
<thumb>thumb/image_1.jpg</thumb>
</image>

<image >
<description>image 2</description>
<thumb>thumb/image_2.jpg</thumb>
</image>

</gallery>

De forma que pueda cargar esos "módulos" <image> en función de una condición ¿cual? pues variables php pasadas por una URL, tipo

Código:

www.misitio.com/home.php?seccion=3

Muchas gracias por sus ideas.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:condicionales en XML

Publicado por El Lobo (25 intervenciones) el 06/09/2010 19:31:57
Usa XPath

Context for XPath Expressions
The evaluation of an XPath expression depends on the context against which the expression operates. The context consists of the node against which the expression is evaluated and its associated environment, which includes the following:

The position of the context node in the document order, relative to its siblings.
The size of the context—that is, the number of siblings of the context node plus one.
Variable bindings with which references to a variable can be resolved.
A function library.
The namespace declarations in scope for the expression.
To better appreciate the concept of context, consider a tree containing nodes. Asking for all nodes named X from the root of the tree returns one set of results, while asking for those nodes from a branch in the tree returns a different set of results. Thus, the result of an expression depends upon the context against which it executes.

XPath expressions can match specific patterns at one particular context, return the results, and perform additional operations relative to the context of the returned nodes. This gives XPath expressions extraordinary flexibility in searching throughout the document tree.

Basic XPath Expressions
The following are basic types of XPath expressions. Each type is described below.

Current context
Document root
Root element
Recursive descent
Specific element
Examples
The following examples show some basic XPath expressions. More complex expressions are possible by combining these simple expressions together and by using the various XPath operators and special characters.

Current context
An expression prefixed with a period and forward slash (./) explicitly uses the current context as the context. For example, the following expression refers to all <author> elements within the current context:
./author
Note that this is equivalent to the following:

author
Document root
An expression prefixed with a forward slash (/) uses the root of the document tree as the context. For example, the following expression refers to the <bookstore> element at the root of this document:
/bookstore
Root element
An expression that uses a forward slash followed by an asterisk (/*) uses the root element as the context. For example, the following expression finds the root element of the document:
/*
Recursive descent
An expression that uses the double forward slash (//) indicates a search that can include zero or more levels of hierarchy. When this operator appears at the beginning of the pattern, the context is relative to the root of the document. For example, the following expression refers to all <author> elements anywhere within the current document:
//author
The .// prefix indicates that the context starts at the level in the hierarchy indicated by the current context.

Specific elements
An expression that starts with an element name refers to a query of the specific element, starting from the current context node. For example, the following expression refers to the <background.jpg> element within the <images> element in the current context node:
images/background.jpg
The following expression refers to the collection of <book> elements within the <bookstore> elements in the current context node:

bookstore/book
The following expression refers to all <first.name> elements in the current context node:

first.name
Note Element names can include the period character (.). These names can be used just like any other name.
Context in the DOM
When using XPath expressions with the Microsoft XML DOM, the context is the Node object whose selectNodes method or selectSingleNode method is called.

When using XPath directly from the DOM, you define the context from a particular node. For more information, see How the DOM Defines the Context for XPath Expressions.

Context in XSLT
When using XPath directly from the XSLT, you define the context by the current node. For more information about how a context is defined, see How XSLT Defines the Context for XPath Expressions.

Other Resources
XML Path Language (XPath) Version 1.0

Operators and Special Characters
XPath expressions are constructed using the operators and special characters shown in the following table.

/ Child operator; selects immediate children of the left-side collection. When this path operator appears at the start of the pattern, it indicates that children should be selected from the root node.
// Recursive descent; searches for the specified element at any depth. When this path operator appears at the start of the pattern, it indicates recursive descent from the root node.
. Indicates the current context.
.. The parent of the current context node.
* Wildcard; selects all elements regardless of the element name.
@ Attribute; prefix for an attribute name.
@* Attribute wildcard; selects all attributes regardless of name.
: Namespace separator; separates the namespace prefix from the element or attribute name.
( ) Groups operations to explicitly establish precedence.
[ ] Applies a filter pattern.
[ ] Subscript operator; used for indexing within a collection.
+ Performs addition.
- Performs subtraction.
div Performs floating-point division according to IEEE 754.
* Performs multiplication.
mod Returns the remainder from a truncating division.

This table does not include Boolean and set operators, which are listed in Boolean, Comparison, and Set Expressions or Set Operations.

Precedence order (from highest precedence to lowest) is defined as indicated in the following table.

Precedence Character Purpose
1 ( ) Grouping
2 [ ] Filters
3 / // Path operations

The group operator, (), is applicable only at the top-level path expression. For example, (//author/degree | //author/name) is a valid grouping operation, but //author/(degree | name) is not.

The filter pattern operators ([]) have a higher precedence than the path operators (/ and //). For example, the expression //comment()[3] selects all comments with an index equal to 3 relative to the comment's parent anywhere in the document. This differs from the expression (//comment())[3], which selects the third comment from the set of all comments relative to the parent. The first expression can return more than one comment, while the latter can return only one comment.

These operators and special characters are described in detail throughout this reference.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar